+ Start a Discussion
Devendra Hirulkar 3Devendra Hirulkar 3 

Error Shows after running the "test class"

hello 
i have write a test class class that shows the following error 

(System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, offer: execution of AfterInsert

caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AddCSR: execution of BeforeInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.AddCSR: line 4, column 1: []

Trigger.offer: line 10, column 1: [])

what is problem with my test class
here i show my trigger and test class

trigger:-
trigger copypro  on Subsc__c  (after insert,after update) 
{
    //Id recordTypeId = [Select Id From RecordType Where DeveloperName = 'User Group Membership'].Id;
    Id recordTypeId = Schema.SObjectType.Subsc__c.getRecordTypeInfosByName().get('User Group Membership').getRecordTypeId();

    List<Subsc__c> subscList = new List<Subsc__c>();

    for (Subsc__c s : Trigger.new){
        if(s.RecordTypeId == recordTypeId)
            subscList.add(s);
    }

    Map<ID, Account> Acc = new Map<ID, Account>(); //Making it a map instead of list for easier lookup
    List<Id> listIds = new List<Id>();        
    set<ID>cObjectID = new set<ID>();   //Making a set of Product ID's
    Map<ID, Account> updateMap = new Map<ID, Account>();
  
    for (Subsc__c s : subscList)
    {
        listIds.add(s.Company_Name__c);
      
        if(s.Product__c     != null)
        {
            cObjectID.add(s.Product__c    );//takes the Lookup Record &     Add that ID's in cObjectID set
        }
    }
    if(!cObjectID.isEmpty()){
        
        Map<ID,Product2> cObjectMap = new Map<ID,Product2>([select Id,Name from Product2 where Id IN: cObjectID]);
        Acc = new Map<Id, Account>([SELECT id, Product_Name__c,(SELECT ID,Product__c FROM Subscs__r) FROM Account WHERE ID IN :listIds]);
        
        for(Subsc__c s : subscList)
        {            
            if(cObjectMap.get(s.Product__c    ).Name != Null)
            {
                // fill the country name on Opportunity with Country Name on Country_Object__c
                String pro= cObjectMap.get(s.Product__c    ).Name;
                Account myacc = acc.get(s.Company_Name__c);
                if(myacc != null){ //always check for nulls to avoid null pointer exceptions
                    myacc.Product_Name__c =pro;
                    updateMap.put(myacc.Id,myacc);
                }
            }
        }
        update updateMap.values();
    }
   
}

the above is my trigger
and
the below is my test class
test class:-
@istest
public class updateaccount 
{
     static testMethod void verifyProductUpdation()
    {
       
        Account a=new Account(Name='ABC');
        insert a;
        
        a=[Select Id,Name from Account where Id =:a.Id];
        System.assertEquals(null, a.Product_Name__c);
        
        Product2 p=new Product2(Name='XYZ');
        insert p;
        test.startTest();
        Subsc__c sub=new Subsc__c(Name='CBZ',Company_Name__c=a.Id,Product__c=p.Id);
        insert sub;
        a.Product_Name__c=p.Id;
        update a;
        a=[Select Id,Product_Name__c from Account where Id=:a.Id];
        
        System.assertEquals(p.Id,a.Product_Name__c);
        test.stopTest();
    }   
}
thanks
Devendra 
Best Answer chosen by Devendra Hirulkar 3
John PipkinJohn Pipkin

Your first query needs to be changed to: ​

a=[Select Id,Name,Product_Name__c from Account where Id =:a.Id];
System.assertEquals(null, a.Product_Name__c);

All Answers

John PipkinJohn Pipkin
It looks like the issue is not with this trigger and test class, but another trigger.
Trigger.AddCSR: line 4, column 1: []

Trigger.offer: line 10, column 1: [])

That error indictates that "Trigger.offer" is calling "Trigger.AddCSR" and the AddCSR method is referencing a null object. It's a very common exception and it is best practice to always check for null values in your code. Example on how to fix:
 
Trigger example on Account(before update){

	for(Account a :Trigger.new){
		Opportunity o = [Select Id from Opportunity where Id='I need coffee now'];
		// Obviously this query will return nothing because the ID doesn't exist
		a.RandomField__c = o.Id; 
		//because we are referencing the opportunity that returned null, it
		//will throw a "attempt to de-reference a null object" error.
	}
}

Instead, try this:
Trigger example on Account(before update){

	for(Account a :Trigger.new){
		Opportunity o = [Select Id from Opportunity where Id='I need coffee now'];
		// Obviously this query will return nothing because the ID doesn't exist
		if(o != null)
			a.RandomField__c = o.Id; 
		//now that we have a null check, the code will not break if your query
		//does not return anything
	}
}

There are several way to get this type of error, but hopefully I've outlined the point. If you set a variable to something that is returned null and then use dot notation to reference it, this error will be thrown. 

Hope that helps
Devendra Hirulkar 3Devendra Hirulkar 3
sir here is my test class what is problem with this please tell me and correct it

@istest
public class updateaccount 
{
     static testMethod void verifyProductUpdation()
    {
       
        Account a=new Account(Name='ABC');
        insert a;
        
        a=[Select Id,Name from Account where Id =:a.Id];
        System.assertEquals(null, a.Product_Name__c);
        
        Product2 p=new Product2(Name='XYZ');
        insert p;
        test.startTest();
        Subsc__c sub=new Subsc__c(Name='CBZ',Company_Name__c=a.Id,Product__c=p.Id);
        insert sub;
        a.Product_Name__c=p.Id;
        update a;
        a=[Select Id,Product_Name__c from Account where Id=:a.Id];
         if(p.Id!= null)

        System.assertEquals(p.Id,a.Product_Name__c);
        test.stopTest();             
    }   
}
John PipkinJohn Pipkin
Your test class is not the issue. It is your trigger/class called "AddCSR" that is causing the error. Please post that code if you need further help with this
Devendra Hirulkar 3Devendra Hirulkar 3
@istest
public class updateaccount 
{
     static testMethod void verifyProductUpdation()
    {
       
        Account a=new Account(Name='ABC');
        insert a;
        
        a=[Select Id,Name from Account where Id =:a.Id];
        System.assertEquals(null, a.Product_Name__c);
        
        Product2 p=new Product2(Name='XYZ');
        insert p;
        test.startTest();
        Subsc__c sub=new Subsc__c(Name='CBZ',Company_Name__c=a.Id,Product__c=p.Id);
        insert sub;
           if(p != null)
           {
        a.Product_Name__c=p.Id;
        update a;
            }
        a=[Select Product_Name__c from Account where Id=:a.Id];
        
        System.assertEquals(null,a.Product_Name__c);
        test.stopTest();
    }   
}
error in this trigger is
System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account.Product_Name__c
John PipkinJohn Pipkin

Your first query needs to be changed to: ​

a=[Select Id,Name,Product_Name__c from Account where Id =:a.Id];
System.assertEquals(null, a.Product_Name__c);
This was selected as the best answer
Devendra Hirulkar 3Devendra Hirulkar 3

Hellow sir 
its work  just one change need that is 
System.assertEquals(sub.Product__c,a.Product_Name__c); beacause it null exception shows


Sir another question is
i want to improve my trigger and apex coding  so sir how to do this means sir which book or site i used for study and practices 
my trigger and apex coding is not so good 
i know its foolish que but please tell me sir

and very thanks for your responce
Devendra
John PipkinJohn Pipkin
Devendra, 

Here's a great resource: https://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm

Please mark the best answer to solve this question. 

Good luck!