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
ankushankush 

Code not Validating


The Old Code and new code functionality is same to have to update the checkbox on contact based on active on user.
Old one does not have code to make active and new one has that one and also instead of using trigger.new[0].Id I modified to trigger.new in new code it is working fine but when I upload to prod and try to validate getting the below error for other classes.
List has no rows for assignment for SObject on contactactivecheckboxclass1.updatect1 line 6
List has no rows for assignment for SObject on contactactivecheckboxclass.updatect line 6


Can you guide me to rectify this.
Old Code trigger:
//This is a trigger that is used to check whether the user is active and updates the related Contact
trigger Checkuseractive on User (after update) {
   
    //Below soql query gets the IsActive and ContactId of the user
 user u=[select id,Isactive,contactid from user where id=:trigger.new[0].id limit 1];
    Id cid=u.ContactId;
    if(trigger.new[0].IsActive==false)
    {
        contactactivecheckboxclass.Updatecnt(cid);
    }
}
Old Code Class
//Future class for updating Contact Active Checkbox field
public class contactactivecheckboxclass {
@future
    //Method that takes the contact id as parameter from Trigger and checks if user is active on user Object and updates the related contact accordingly.
    public static void updatecnt(id x){
    contact c=[select Is_user_Active__c from Contact where id=:x limit 1];
    c.Is_User_Active__c=false;
    if(!test.isrunningtest()){
        database.update(c);
         }
   } 
}
New Code trigger:
//This is a trigger that is used to check whether the user is active and updates the related Contact
trigger Checkuseractive on User (after update) {
   
    //Below soql query gets the IsActive and ContactId of the user
 list<user> u=[select id,Isactive,contactid from user where id=:trigger.new];
    for(user usr:u){
    Id cid=usr.ContactId;
    if(usr.IsActive==false)
    {
        contactactivecheckboxclass.Updatecnt(cid);
        
    }
        else if(usr.IsActive==true)
    {
        contactactivecheckboxclass1.Updatecnt1(cid);
        
    }
        }
}
New Code Class1
//Future class for updating Contact Active Checkbox field
public class contactactivecheckboxclass {
@future
    //Method that takes the contact id as parameter from Trigger and checks if user is active on user Object and updates the related contact accordingly.
    public static void updatecnt(id x){
    contact c=[select Is_user_Active__c from Contact where id=:x limit 1];
        system.debug('The contact is' + c);
    c.Is_User_Active__c=false;
    if(!test.isrunningtest()){
        database.update(c);
         }
   } 
    
    
}
New Code Class2
//Future class for updating Contact Active Checkbox field
public class contactactivecheckboxclass1 {
@future
    //Method that takes the contact id as parameter from Trigger and checks if user is active on user Object and updates the related contact accordingly.
     public static void updatecnt1(id x){
    contact c=[select Is_user_Active__c from Contact where id=:x limit 1];
    c.Is_User_Active__c=true;
    if(!test.isrunningtest()){
        database.update(c);
         }
   } 
}
StephenKennyStephenKenny
Hi Ankush

The problem is that your query is not returning any values:
contact c=[select Is_user_Active__c from Contact where id=:x limit 1];

There are no contacts beoing returned in the query above, which is causing the error message. I suspect that the user in question does not have a contact associated therfore the cId that is being passed to your class is null.

Please remember to mark this thread as solved with the answer that best helps you.

Regards
Stephen