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
}
}
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');
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.
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.
You can run specific user It will work as expected :)
Please let us know if this will help you.
Thanks,
Parag Bhatt
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');
}
}
Follow these steps:
1. Query that particular User or Users using this code 2. Now use this condition in the trigger
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.
Please use this code for user profile:-
if you have any issue please let me know.
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh