You need to sign in to do that
Don't have an account?
Nishigandha Jadhav
not covering add error method inside if statement
trigger usertrigger on User (before update) {
//Set<ID> username = new Set<ID>{'0050z000007XtMB'};
//Map<ID,User> usernames = new Map<ID,User>([SELECT ID, Name FROM User WHERE Name IN :username]);
Map<ID,User> usernames = new Map<ID,User>([SELECT ID, Name FROM User WHERE ID in ('0050z000007XtMB' ,'00560000004Mpmp') ]);//(Checking for user id)
for(User o:trigger.new){
if(Trigger.oldMap.get(o.id).email!=o.email && usernames.containsKey(o.id) ){
o.email.addError('you cannot edit the email');
}
else if(Trigger.oldMap.get(o.id).username!=o.username && usernames.containsKey(o.id)){
o.username.addError('you cannot change the username');
}
else if(Trigger.oldMap.get(o.id).isActive!=o.isActive && usernames.containsKey(o.id)){
o.isActive.addError('You cannot deactivate the user');
}
else if(Trigger.oldMap.get(o.id).profileID !=o.profileID && usernames.containsKey(o.id) ){
o.profileId.addError('you cannot edit the profile');}
}
}
//Set<ID> username = new Set<ID>{'0050z000007XtMB'};
//Map<ID,User> usernames = new Map<ID,User>([SELECT ID, Name FROM User WHERE Name IN :username]);
Map<ID,User> usernames = new Map<ID,User>([SELECT ID, Name FROM User WHERE ID in ('0050z000007XtMB' ,'00560000004Mpmp') ]);//(Checking for user id)
for(User o:trigger.new){
if(Trigger.oldMap.get(o.id).email!=o.email && usernames.containsKey(o.id) ){
o.email.addError('you cannot edit the email');
}
else if(Trigger.oldMap.get(o.id).username!=o.username && usernames.containsKey(o.id)){
o.username.addError('you cannot change the username');
}
else if(Trigger.oldMap.get(o.id).isActive!=o.isActive && usernames.containsKey(o.id)){
o.isActive.addError('You cannot deactivate the user');
}
else if(Trigger.oldMap.get(o.id).profileID !=o.profileID && usernames.containsKey(o.id) ){
o.profileId.addError('you cannot edit the profile');}
}
}
Best Answer chosen by Nishigandha Jadhav
Raj Vakati
First Remove the hardcoded user id from the trigger and try something like below