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
Guru Prasad 31Guru Prasad 31 

how to run trigger for particular Users

Can you please provide some code
Parag Bhatt 10Parag Bhatt 10
Hi Guru Prasad,
You can run specific user
trigger ContactTriggers on Contact (after insert, after update) {
User specificuser = new User();
try{
specificuser = [Select Id From User Where Name = 'a user'];
}catch(Exception e){
}
if(Trigger.isAfter && specificuser.Id!=null && Trigger.new[0].LastModifiedById != specificuser.Id) {
//now code here
}

}
It will work as expected :)

Please let us know if this will help you.

Thanks,
Parag Bhatt
 
Guru Prasad 31Guru Prasad 31
This my code how to restrict to fire trigger only for particular profiles can give some code.
trigger FindDuplicateContact on Contact (before insert) {
    

 
 List<id> accId = new List<id>();
  for(contact cc : trigger.new){
    if(cc.AccountId != null){
    accId.add(cc.AccountId);    
      }
   }
 List<contact> ccList = new List<contact> ();
 
   
  List<Account> accLst = [select id,(select id from contacts) from account where id IN : accId] ;
for(account accObj : accLst ){
   for(contact c : accObj.contacts){     
   ccList.add(c);
   }
     }
  
for(contact oCon:trigger.new)   
 if(ccList.size() > 0 ){
     oCon.addError('you can not add more then once contact for this account'); 
       
     }
   }
Gaurish Gopal GoelGaurish Gopal Goel
Hi Guru,

Follow these steps:
1. Query that particular User or Users using this code
Map<Id,User> users = new Map<Id,User>([SELECT Id FROM User WHERE Name LIKE '%John%']);//change the where clause accordingly.
2. Now use this condition in the trigger
if(users.keyset().contains(userInfo.getUserId()))
{
       //WRITE YOUR TRIGGER CODE HERE
}

In this way the trigger will check the currently logged in user and execute the code if the user is one of the chosen users. If this answer solves your problem then mark it as the solution to help others. Thanks.
mukesh guptamukesh gupta
Hi Guru,

Please use this code for user profile:-
 
trigger FindDuplicateContact on Contact (before insert) {
   

Id profileId=userinfo.getProfileId();
String profileName=[Select Id,Name from Profile where Id=:profileId].Name;
system.debug('ProfileName'+profileName); 

if(profileName = 'System Administator'){
 
 List<id> accId = new List<id>();
  for(contact cc : trigger.new){
    if(cc.AccountId != null){
    accId.add(cc.AccountId);    
      }
   }
 List<contact> ccList = new List<contact> ();
 
   
  List<Account> accLst = [select id,(select id from contacts) from account where id IN : accId] ;
for(account accObj : accLst ){
   for(contact c : accObj.contacts){     
   ccList.add(c);
   }
     }
  
for(contact oCon:trigger.new)   
 if(ccList.size() > 0 ){
     oCon.addError('you can not add more then once contact for this account'); 
       
     }
   }
}

if you have any issue please let me know.

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh