Showing posts with label Trigger. Show all posts
Showing posts with label Trigger. Show all posts

Friday, 26 September 2014

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. }

Friday, 12 September 2014

How to delete child record in Lookup relationship Salesforce


Some time you may have a requirement whenever Parent is deleted the related Child record should be deleted in Lookup Relationship.

By default, the "lookup" relationship field only having the following options.



In this case you can go for trigger on Parent object with after delete event.

Here is the code,

  1. trigger DeleteContact on Account (after delete)
  2. {
  3.     List<Contact> contacts = [SELECT AccountId FROM Contact WHERE AccountId IN:Trigger.OldMap.keyset()];
  4.     delete contacts;
  5. }

If your have custom object relationship means use Lookup_Field__r.Id in the place of AccountId in the above query.

Wednesday, 27 August 2014

How to fire trigger only on Selected Day in salesforce


To achieve that trigger only works on selected day means,

Formatting Date

  1. Date d = System.Today();
  2. // you need to format date to date/time in order to format.
  3. DateTime dt = DateTime.newInstance(d.year(), d.month(), d.day());
  4. String todayDay = dt.format('EEEE');
  5. if(todayDay != 'Saturday' || todayDay !='Sunday')
  6. {
  7.     // Process your logic.
  8. }

Formatting Date/Time 

  1. DateTime dt = System.now();
  2. String todayDay = dt.format('EEEE');
  3. if(todayDay != 'Saturday' || todayDay !='Sunday')
  4. {
  5.     // Process your logic.
  6. }

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...