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
Sunny9222Sunny9222 

need to update a field in another object using apex trigger

There is a checkbox field in contact object named "Invalid email" as soon as it is checked. the active checkbox filed in USER needs to be unchecked. I am trying to create a trigger in contact but it is not working


trigger UpdateUserActivation on Contact (After update) {
    if(shouldIRun.canIRun() == false){
    CheckUserActivation.UserActivationUpdate(trigger.newMap.keySet());
    }
    /*string useremail ;
    //List<User> userrec = new List<User>();
    List<User> listofuser = new List<User>();
    for(Contact c: trigger.new){
        
    
    if(c.invalidemail == true){
        List<User> userrec = [Select id, email, isActive from user where contactId =c.id: ];
        if(userrec.size()>0){
            for(User u: userrec ){
                User usr = new User();
                usr.id = u.id;
                usr.isActive = false;
                listofuser.add(usr);
            }
            }
            update listofuser;
        }
    }  */  
}
VamsiVamsi
Hi,

Do you have contactId field on User ? Also what is the relationship between User and contact ? 
 
Sunny9222Sunny9222
No I dont have a contactId field on user
 
VamsiVamsi
Then following query doesn't return any :  contact List<User> userrec = [Select id, email, isActive from user where contactId =c.id: ]; 

If invalidemail == true on contact then for which user records you would like to update i.e I mean you want to make active to false

 
Muzammil BajariaMuzammil Bajaria
You need to have some sort of relationship between conatact and user. name of contacts and users are always same or keep changing ?