Wednesday 3 May 2017

1. Account is inserted we need to insert "Entitlement"
2. Contact is inserted we need to insert "Entitlement Contact"
3. Entitlement Contact is a junction between "Contact" & "Entitlement"
4. In Contact we need to give the account related Entitlement Id dynamically.

private void createEntitlementontacts(ClsWrappers.TriggerContext trgCtx) {
     
               if (trgCtx.isInsert || trgCtx.isUpdate)
               {
                               Set<Id> accIds = new Set<Id>();
                               Map<Id,Id> accEntlIdMap = new Map<Id,Id>();
                               List<EntitlementContact> entctlist = new List<EntitlementContact>();
                             
                               for(sobject so : trgCtx.newList)
                               {
                                              Contact con = (Contact)so;
                                              if(con.accountId != null){
                                                             accIds.add(con.accountId);
                                              }
                               }
                               if(accIds.size() > 0){
                                              for(Account acc : [select id, name, (select id, name from Entitlements) from Account where Id in: accIds])
                                              {
                                                             if(acc.Entitlements.size() > 0){
                                                                             accEntlIdMap.put(acc.Id,acc.Entitlements[0].Id);
                                                              }
                                              }
                               }
                               for(sobject so : trgCtx.newList)
                               {
                                              Contact con = (Contact)so;
                                              EntitlementContact en = new EntitlementContact();
                                              if(accEntlIdMap.containsKey(con.accountId)){
                                                              en.EntitlementId = accEntlIdMap.get(con.accountId);
                                                              en.ContactId = con.id;
                                                              entctlist.add(en);
                                              }
                               }
                               if(entctlist.size() > 0){
                                              System.debug('entctlist: '+entctlist);
                                              insert entctlist;
                               }
               }
}

No comments:

Post a Comment

Batch Apex

1. What are transaction limits in apex? Total number of SOQL queries issued1 - 100 Total number of records retrieved by SOQL queries - 50...