Wednesday 25 February 2015

Automatically Follow & Unfollow Records using Apex Trigger in Salesforce

          I had a scenario that I need to automatically follow the Records after created, As well if the Owner is changed for the particular record, then Old Owner should be removed or Unfollow the records automatically.  This one I have done with the trigger logic.

          In the below trigger, I have not checked any criteria for Follow and UnFollow Records. By default, when record is created, automatically owner will follow the record. As well, if owner changed then old owner will be unfollow the record automatically. If you need conditions, you can alter the below code.

          EntitySubscription object is used for the Follow records. Here is the trigger and handler code,

Trigger:

  1. trigger FollowRecordOpportunityTrigger on Opportunity (after insert, after update)
  2. {
  3.     if(Trigger.isInsert)
  4.     {
  5.         FollowRecordHandler.followRecord(Trigger.newMapnull'insert');
  6.     }
  7.     if(Trigger.isUpdate)
  8.     {
  9.         FollowRecordHandler.followRecord(Trigger.newMap, Trigger.oldMap'update');
  10.     }
  11. }

Apex Class:

  1. public class FollowRecordHandler
  2. {
  3.     public static void followRecord(Map<Id, Opportunity> newOppMap, Map<Id, Opportunity> oldOppMap, String operation)
  4.     {
  5.         List<EntitySubscription> entitySubToBeInsert = new List<EntitySubscription>();
  6.         List<Id> entitySubIdsToBeDelete = new List<Id>();
  7.        
  8.         if(operation.equalsIgnoreCase('insert'))
  9.         {
  10.             for(Opportunity currOpp : newOppMap.values())
  11.             {
  12.                 EntitySubscription follow = new EntitySubscription(parentId = currOpp.id, subscriberid =currOpp.ownerid);
  13.                 entitySubToBeInsert.add(follow);
  14.             }
  15.            
  16.         }
  17.        
  18.         else if(operation.equalsIgnoreCase('update'))
  19.         {
  20.             for(Opportunity currOpp : newOppMap.values())
  21.             {
  22.                 if(oldOppMap.get(currOpp.Id).OwnerId != currOpp.OwnerId)
  23.                 {
  24.                     EntitySubscription follow = new EntitySubscription(parentId = currOpp.id, subscriberid =currOpp.ownerid);
  25.                     entitySubToBeInsert.add(follow);
  26.                    
  27.                     entitySubIdsToBeDelete.add(oldOppMap.get(currOpp.Id).OwnerId);
  28.                 }
  29.             }
  30.         }
  31.        
  32.         if(!entitySubToBeInsert.isEmpty())
  33.         {
  34.             insert entitySubToBeInsert;
  35.         }
  36.        
  37.         if(!entitySubIdsToBeDelete.isEmpty())
  38.         {
  39.             List<EntitySubscription> entitySubDelete = new List<EntitySubscription>();
  40.             for(EntitySubscription currEntity : [select id from EntitySubscription where subscriberid =:entitySubIdsToBeDelete])
  41.             {
  42.                 EntitySubscription ent = new EntitySubscription();
  43.                 ent.ID = currEntity.Id;
  44.                 entitySubDelete.add(ent);
  45.             }
  46.             Delete entitySubDelete;
  47.         }
  48.     }
  49. }

Saturday 21 February 2015

Dynamically Retrieve All fields in SOQL without Mentioning the Field Name in Salesforce


Here is the way to query the all field’s values without mentioning the Field name.


  1. // Specify the Object Name to retrieve the List of field names.
  2. Map<String, Schema.SObjectField> fieldMap = Account.sObjectType.getDescribe().fields.getMap();
  3.        
  4. String fieldName = '';
  5. String accountQuery;
  6.        
  7. for(Schema.SObjectField currentField : fieldMap.Values())
  8. {
  9.      Schema.DescribeFieldResult fieldResult = currentField.getDescribe();  
  10.      fieldName += fieldResult.getName() + ',';
  11. }
  12.        
  13. fieldName = fieldName.substring(0, fieldName.length()-1);
  14. // Build a Dynamic Query String.
  15. accountQuery = 'SELECT ' + fieldName + ' FROM Account';
  16. List<Account> accList = Database.query(accountQuery);


Reference:


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