Monday 29 September 2014

Usage of this keyword in salesforce

This keyword used to represent the current instance of the class.

Example:
  1. public class ThisKeyWordExample
  2. {
  3.     String str  = 'Test Me';
  4.    
  5.     public void strMethod(String str)
  6.     {
  7.         System.debug('--Parameter Value--'+str);
  8.         System.debug('--Current Instance Value--'+this.str);
  9.     }
  10.    
  11. }

The above example has One Instance Variable & One Local Variable or Method parameter with same name(i.e. str).

str = Refers parameter value.
this.str = Refers the current instance variable value.

To execute this class go to Developer Console, paste the below line then click execute.

  1. ThisKeyWordExample thisExample = new ThisKeyWordExample();
  2. thisExample.strMethod('Test You');

Reference: this keyword

Friday 26 September 2014

How to select only one checkbox value in visualforce page using javascript

Here is the code to select only one checkbox value in visualforce page by accessing DOM ID
Using the javascript and onchange event.
  1. <apex:page >
  2.     <apex:form >
  3.         <script>
  4.         function confirmDisbaled(ifchecked, id1 ,id2,id3) {
  5.             document.getElementById(id1).disabled = ifchecked;
  6.             document.getElementById(id2).disabled = ifchecked;
  7.             document.getElementById(id3).disabled = ifchecked;
  8.         }
  9.         </script>
  10.         <apex:pageBlock >
  11.             <apex:actionRegion >
  12.                 <apex:outputPanel id="panel1">
  13.                     <apex:pageBlockSection title="PST" id="PST">
  14.                        
  15.                         <apex:inputCheckbox label="Global" id="gbl" onchange="return confirmDisbaled(this.checked, '{!$Component.lcl}','{!$Component.cntry}');"/>
  16.                         <apex:inputCheckbox label="Local" id="lcl" onchange="return confirmDisbaled(this.checked, '{!$Component.gbl}','{!$Component.cntry}');"/>
  17.                         <apex:inputCheckbox label="Country" id="cntry" onchange="return confirmDisbaled(this.checked, '{!$Component.lcl}','{!$Component.gbl}');"/>
  18.                        
  19.                     </apex:pageBlockSection>
  20.                 </apex:outputPanel>
  21.             </apex:actionRegion>
  22.         </apex:pageBlock>
  23.     </apex:form>
  24. </apex:page>

How to Automatically Sync new Quote with Opportunity in Apex Trigger

Here is the way to Automatically Sync new Quote with Opportunity in Apex Trigger. Before that some of the information about Quote and Opportunity Field Details that are,

In Quote object there is a field called IsSyncing, This is a Read Only field in salesforce. If you run the below snippet in developer console the field result such as isCreatable, isUpdatable is false. So you can’t create/update this field value.

1.    Schema.DescribeFieldResult dfr = Schema.sObjectType.Quote.fields.IsSyncing;
2.    System.debug(dfr);

In Opportunity object there is a field called SyncedQuoteID, If you run the below snippet in developer console the field result such as isCreatable, isUpdatable is True. I.e. you can do insert and update on this field.

1.    Schema.DescribeFieldResult dfr = Schema.sObjectType.Opportunity.fields.SyncedQuoteId;
2.    System.debug(dfr);

But if you update the Opportunity field SyncedQuoteID in a trigger, you will get the below error.

“The opportunity SyncedQuote field is read only within a trigger”

For security reason by default in salesforce trigger you can’t update the SyncedQuoteID field even if you move the update coding in to separate class.

To overcome this problem you need to go for @future, By using this it will go with the separate transaction and the DML operation allowed. The below code snippet will useful to this problem.


Apex Trigger



  1. trigger QuoteAutoSync on Quote (after insert)
  2. {
  3.     Map<Id, Id> quoteMap = new Map<Id, Id>();
  4.     for(Quote currentQuote : Trigger.New)
  5.     {
  6.         quoteMap.put(currentQuote.Id, currentQuote.OpportunityId);
  7.     }
  8.     QuoteAutoSyncUtil.syncQuote(quoteMap);
  9. }

Apex Class


  1. public class QuoteAutoSyncUtil
  2. {
  3.     @future
  4.     public static void syncQuote(Map<Id, Id> quoteMap)
  5.     {
  6.         Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>();
  7.         for(Id currentQuote : quoteMap.keyset())
  8.         {
  9.             Opportunity opp = new Opportunity();
  10.             opp.Id = quoteMap.get(currentQuote);
  11.             opp.SyncedQuoteId = currentQuote;
  12.             oppMap.put(opp.Id, opp);
  13.         }
  14.         update oppMap.values();
  15.     }
  16. }

Test Class


  1. @isTest
  2. private class TestQuoteAutoSync
  3. {
  4.     static testMethod void insertQuote()
  5.     {
  6.         Opportunity opp = new Opportunity();
  7.         opp.Name = 'Test Opportunity';
  8.         opp.StageName = 'Prospecting';
  9.         opp.CloseDate = system.today();
  10.         insert opp;
  11.        
  12.         Quote quo = new Quote();
  13.         quo.Name = 'Test Quote';
  14.         quo.OpportunityId = opp.Id;        
  15.        
  16.         Test.startTest();
  17.         insert quo;
  18.         Test.stopTest();
  19.        
  20.         Opportunity o = [select SyncedQuoteId from opportunity where id=:opp.Id];
  21.         system.assert(o.SyncedQuoteId != null);
  22.                
  23.     }
  24. }

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...