function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ankit Gupta SFDCLearnerAnkit Gupta SFDCLearner 

Any one please elaborate, how we can run a trigger according to a user. I heard it from some friends, that we can call trigger in respect to users with the help of Custom settings, could any one will give an example in Simple | Understandable language.

Any one please elaborate, how we can run a trigger according to a user. I heard it from some friends, that we can call trigger in respect to users with the help of Custom settings, could any one will give an example in Simple | Understandable language.
RKSalesforceRKSalesforce
Hi Ankit,

Lets say you want to write a trigger on Product Object and that should be executed for some specific users. Then You can achieve with the help of custom setting. Custom Setting Name = User_Emails__c. Create records of custom setting by going to custom setting --> Manage. Now create records for all users in the customs setting by putting Email address of user in Email field.
Trigger Product2RecordCount on Product2(After Insert, After Update, After Delete, After UnDelete){
    //Map of User Email and  User_Emails__c record
    Map<String, User_Emails__c> allEmails = User_Emails__c.getAll();
    
    //To check in the map if current logged in users email address is available in the custom setting records 
    if(allEmails.containsKey(UserInfo.getUserEmail()) ){ 
        List<ID> locationIds = New List<ID>();
        
        If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
            For(Product2 c: Trigger.New){
                if(c.Location__c  != null){
                    locationIds.add(c.Location__c);
                }  
            }
        }
        If(Trigger.IsDelete){
            For(Product2 c: Trigger.Old){
                locationIds.add(c.Location__c);
            }
        }
         
        List<Location__c> LocationListToUPdate = New List<Location__c>();
         
        For(Location__c lct: [Select Current_Lot_Inventory__c, (Select ID FROM Products__r) FROM Location__c WHERE ID = :locationIds]){
            lct.Current_Lot_Inventory__c= lct.Products__r.size();
            LocationListToUPdate.add(lct);
        }
         
         
        try{
            Update LocationListToUPdate;
        }
         Catch(Exception E){
            System.Debug('Error Message: ' + e.getMessage());
        }
    }
}



Please let me know if you need any help. Mark as best answer if helped.


Regards,
Ramakant
 
Ankit Gupta SFDCLearnerAnkit Gupta SFDCLearner
Thanks Ramakant,

Actually I am new for custom settings, could you please help me for creation of a custom settings. Means which type of custom settings should I have to create. i.e List or hierarchy.


Thanks in advance
RKSalesforceRKSalesforce
Hi Ankit ,

You need to create List Custom Setting.

Regards,
Ramakant