You need to sign in to do that
Don't have an account?
regarding unit test
Hi i am new to salesforce
i have a custom object named donation__c i wrote a trigger on donaton__c for calculate the days and denpend on that i am updating the date on his parent object field next_due_date__c
this is my trigger
trigger caldays on Donation__c (after insert, after update) {
public decimal i;
for(donation__c s : trigger.new)
{
donation__c dd = [select id,Project_Type__c,amount__c,contact__c,amount_type__c,Date_Of_Donation__c from donation__c where id = :s.id];
if(dd.amount_type__c == 'USD' && dd.amount__c != null)
{
i = dd.amount__c / 0.361643856164384;
}
else if(dd.amount_type__c == 'INR' && dd.amount__c != null)
{
i = dd.amount__c / 1.095890410958904;
}
if(dd.Project_Type__c == 'Sponsor A Child' )
{
long l1 = i.round();
id a = dd.contact__c;
contact c = [ select id,Next_Due_Date__c from contact where id = :a];
c.Next_Due_Date__c = dd.Date_Of_Donation__c + l1;
update c;
}
}
}
and i am trying to write test case for this trigger
@istest
private class caldaystest{
static testmethod void unittest()
{
decimal i;
donation__c d = new donation__c();
d.Date_Of_Donation__c = system.today() ;
d.amount_type__c = 'USD' ;
//d.Project_Type__c = 'Sponsor A Child';
d.amount__C = 250;
insert d;
if(d.Project_Type__c == 'Sponsor A Child' )
{
id a = d.contact__c;
long l1 = i.round();
contact c = [ select id,Next_Due_Date__c from contact where id = '003J000000eqnHh'];
c.Next_Due_Date__c = d.Date_Of_Donation__c + l1 ;
upsert c;
}
donation__c d1 = new donation__c();
d1.Date_Of_Donation__c = system.today() ;
d1.amount_type__c = 'INR' ;
d1.Project_Type__c = 'Sponsor A Child';
d1.amount__C = 250;
insert d1;
if(d1.Project_Type__c == 'Sponsor A Child' )
{
// id a1 = d1.contact__c;
long l1 = i.round();
contact c1 = [ select id,Next_Due_Date__c from contact where id = '003J000000eqnHh'];
c1.Next_Due_Date__c = d1.Date_Of_Donation__c + l1 ;
upsert c1;
}
}
}
like this its covering up to 84 percent but getting error like this
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, caldays: execution of AfterInsert
can any one help me solve this
The line that is causing the problem is the following:
The variable 'a' is the value of the Contact__c field on the Donation__ c record being inserted, but you haven't populated that so the value will be null. You are then executing a select that assumes there will always be exactly one result, but as the id value you are searching for is null, there are no matches.
You should first defend against the lack of a contact field, something like:
You should also store the updated contacts in a list and then update those in one action outside of your loop, or you will hit governor limits when processing more than 100 objects at once.
Finally, your test class should create a contact and specify the id of that in the donation record, e.g.
I'm not quite sure why you are querying back contact based on hardcoded ids and executing the same code as the trigger.
All Answers
@isTest(SeeAllData=true)
but i got erroe like this
Time Started 9/18/2013 3:34 AM Class caldaystest Method Name unittest Pass/Fail Fail Error Message System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, caldays: execution of AfterInsert
caused by: System.QueryException: List has no rows for assignment to SObject
Trigger.caldays: line 19, column 1: [] Stack Trace Class.caldaystest.unittest: line 28, column 1
remove that seealldata part and the best way is
insert a contact first in test class and assign the same in the donation creation part...
The line that is causing the problem is the following:
The variable 'a' is the value of the Contact__c field on the Donation__ c record being inserted, but you haven't populated that so the value will be null. You are then executing a select that assumes there will always be exactly one result, but as the id value you are searching for is null, there are no matches.
You should first defend against the lack of a contact field, something like:
You should also store the updated contacts in a list and then update those in one action outside of your loop, or you will hit governor limits when processing more than 100 objects at once.
Finally, your test class should create a contact and specify the id of that in the donation record, e.g.
I'm not quite sure why you are querying back contact based on hardcoded ids and executing the same code as the trigger.
Thank u very much