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
mng0827mng0827 

System.NullPointerException: Script-thrown exception

Hi,

I'm trying to create a trigger which updates the Account owner based on the detail object criteria. The account assignment should be in a round robin fashion so that accounts will be distributed equally.

However, I get this error upon editing the detail object:

System.NullPointerException: Script-thrown exception

Below is my code:

trigger AssignNewClient on Master_Tracker__c (before insert) {
    List<Id> AccountId = new list <id>();
    for(Master_Tracker__c mt: Trigger.new) 
    {
        if((mt.Product_Code__c == 'Advisory Services')
        &&(mt.Agreement_Signed_Date__c != null)
        &&(mt.Date_Payment_Info_Received__c != null)
        &&(mt.Renewal__c = false)){
        AccountId.add(mt.Account_Name__c);  
    }
        {
       
            Integer x = Integer.valueof(mt.Date_Payment_Info_Received__c);
            Integer y = math.mod(x, 5);
            Account a = new Account();
            if(y == 0)
            {
                //When Mod = O update the owner to User 1 ID 005d0000001mr2u
                //new org ID 005d0000001mr2u
                a.OwnerId = '005d0000001mr2u';
            }
           
           
            else if(y == 1)
            {
                //When Mod = 2 update the owner to User 2 ID 005d0000001nHMY  
                a.OwnerId = '005d0000001nHMY';
            }
           
            else if(y == 2)
            {
                //When Mod = 3 update the owner to User 3 ID 005d0000002KBv1
                a.OwnerId = '005d0000002KBv1';
            }  
           
            else if(y == 3)
            {
               //When Mod = 4 update the owner to User 4 ID 005d0000001mr1u
               a.OwnerId = '005d0000001mr1u';
            }
           
            else if(y == 4)
            {
               //When Mod = 5 update the owner to User 5 ID 005d0000002LDuq
               a.OwnerId = '005d0000002LDuq';
            }
        }
    }
}
Shashikant SharmaShashikant Sharma
You are accessing many fields in this code like :

mt.Product_Code__c may be put a null cehck to this so code will be . Similarly if error comes again put check to other fields accessed in the code. If you still see the issue then share the error message.
if((mt.Product_Code__c != null && mt.Product_Code__c == 'Advisory Services')
        &&(mt.Agreement_Signed_Date__c != null)
        &&(mt.Date_Payment_Info_Received__c != null)
        &&(mt.Renewal__c = false)){
mng0827mng0827
I've re-written the code but doesn't seem to work after saving it without errors. The account owner does not update as expected.I created a test class and found that line 10: AccountIds.add(mt.Account_Name__c); is not covered. I'm not sure what the problem is but here is the new code I am using:

trigger AssignNewClient on Master_Tracker__c (before insert) {
    Map<Id,Account> updatedowner = new Map<Id,Account>();
    List<Id> AccountIds = new list <id>();
        for(Master_Tracker__c mt: Trigger.new)
    {
        if((mt.Product_Code__c == 'Advisory Services')
        &&(mt.Agreement_Signed_Date__c != null)
        &&(mt.Date_Payment_Info_Received__c != null)
        &&(mt.Renewal__c = false)){
        AccountIds.add(mt.Account_Name__c); 
    }
    updatedowner = new Map<Id,Account>([SELECT id,Name FROM Account WHERE ID IN :AccountIds]);
        {
      
            Integer x = Integer.valueof(mt.BID__c);
            Integer y = math.mod(x, 5);
            Account a = new Account();
            if(y == 0)
            {
                //When Mod = O update the owner to User 1 ID 005d0000001mr2u
                //new org ID 005d0000001mr2u
                a.OwnerId = '005d0000001mr2u';
            }
          
          
            else if(y == 1)
            {
                //When Mod = 2 update the owner to User 2 ID 005d0000001nHMY 
                a.OwnerId = '005d0000001nHMY';
            }
          
            else if(y == 2)
            {
                //When Mod = 3 update the owner to User 3 ID 005d0000002KBv1
                a.OwnerId = '005d0000002KBv1';
            } 
          
            else if(y == 3)
            {
               //When Mod = 4 update the owner to User 4 ID 005d0000001mr1u
               a.OwnerId = '005d0000001mr1u';
            }
          
            else if(y == 4)
            {
               //When Mod = 5 update the owner to User 5 ID 005d0000002LDuq
               a.OwnerId = '005d0000002LDuq';
            }
           
            update updatedowner.values();
        }
    }
}