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
SFDCRRSFDCRR 

trigger test coverage- struggling

i hv a child record updating parent reocrd trigger .

 

trigger is :

 

trigger childtrigger on child ( before insert )
 {
child__c  c = Trigger.new[0];
parentobject p=[Select Id,Assigned__c from parent__c where Id=:c.Request__c ];
p.Assigned__c = c.Assigned__c;
    
update p;   
}

 

 

i m struggling to write test coverage for it . 

 

approach to test covage. 

 

create a child for existing parent in the sandbox ; and assign the value and update the update in parent; is that approach .

 

my test failour says it is System.NullPointerException: Attempt to de-reference a null objectClass.crr.Assignedupdate_Test: line 30, column 5 External entry point

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
WesNolte__cWesNolte__c

Hey

 

 

Try this,

 

 parentobject p = new parentObject();

insert p;

 

 child__c c = new child__c(Request__c=p.id);

insert c;

 

Cheers,

Wes 

All Answers

SFDCRRSFDCRR
Any one can help or say something ..plz
WesNolte__cWesNolte__c

Hey

 

 

Try this,

 

 parentobject p = new parentObject();

insert p;

 

 child__c c = new child__c(Request__c=p.id);

insert c;

 

Cheers,

Wes 

This was selected as the best answer
SFDCRRSFDCRR

first thanks so much for your response.

 

so you mean above code i will use in test method 

WesNolte__cWesNolte__c

Yeah that's right:) The reason you're getting an error is because your test code is trying to do the following:

 

insert child object;

fire before insert trigger(no parent object exists so an error is thrown).

insert parent object; // we don't get here because of previous error

 

So all you have to do is shuffle the order of your test code.

 

Wes

SFDCRRSFDCRR
It worked thanks thanks so much