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
notsosmartnotsosmart 

Cross object update trigger test coverage failure

I have a fully functioning cross object update trigger but my tests fail with:

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateAccountPartsSurveyDate: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject Trigger.UpdateAccountPartsSurveyDate: line 6, column 1: 

 

The Trigger:

 

trigger UpdateAccountPartsSurveyDate on TIMBASURVEYS__Recipient__c (after update, after insert) {

Account myAccount = new Account();
TIMBASURVEYS__Recipient__c Recipient = trigger.new[0];

myAccount = [SELECT
Id
,Name
,Date_of_Last_Parts_Survey_Sent__c
FROM
Account
WHERE
Id = :Recipient.TIMBASURVEYS__RelatedAccount__c
];
// Truncate Date
Datetime dt = Recipient.TIMBASURVEYS__DateSendInvitation__c;
Date dateval = date.newInstance(dt.year(), dt.month(), dt.day());

if (myAccount.Date_of_Last_Parts_Survey_Sent__c == null || myAccount.Date_of_Last_Parts_Survey_Sent__c < dateval)
{
myAccount.Date_of_Last_Parts_Survey_Sent__c = dateval;
update myAccount;
}

}

 

The Test Class:

 

@isTest private class TestSurveyAcctDateTrigger {

static testMethod void TestSurveyAcctDateTrigger () {
Account account = new Account();
TIMBASURVEYS__Recipient__c recipient = new TIMBASURVEYS__Recipient__c();
recipient.Name = 'This is a trigger test';
recipient.TIMBASURVEYS__DateSendInvitation__c = date.PARSE('07/11/2012');
insert recipient;
}
}

Best Answer chosen by Admin (Salesforce Developers) 
HariDineshHariDinesh

Hi,

 

Here the problem is with test data. In the test method you are not inserting account and not relating account with TIMBASURVEYS__Recipient__c.

So you are facing problem like as you mention above.

Whenever writing test method make sure to give proper test data to cover all the scenario.

As per my understanding the below test method will cover the trigger and will not give any problems.

 

@isTest private class TestSurveyAcctDateTrigger 
{
static testMethod void TestSurveyAcctDateTrigger () 
{
Account acc = new Account(name='myacc',......give all the required fields);
insert acc;
TIMBASURVEYS__Recipient__c recipient = new TIMBASURVEYS__Recipient__c();
recipient.Name = 'This is a trigger test';
recipient.TIMBASURVEYS__RelatedAccount__c =acc.id;
recipient.TIMBASURVEYS__DateSendInvitation__c = date.PARSE('07/11/2012');
insert recipient;
}
}

 If still getting problem check the data is there any other fields are missing and give values to those which definatly solve your problem.

 

 

All Answers

HariDineshHariDinesh

Hi,

 

Here the problem is with test data. In the test method you are not inserting account and not relating account with TIMBASURVEYS__Recipient__c.

So you are facing problem like as you mention above.

Whenever writing test method make sure to give proper test data to cover all the scenario.

As per my understanding the below test method will cover the trigger and will not give any problems.

 

@isTest private class TestSurveyAcctDateTrigger 
{
static testMethod void TestSurveyAcctDateTrigger () 
{
Account acc = new Account(name='myacc',......give all the required fields);
insert acc;
TIMBASURVEYS__Recipient__c recipient = new TIMBASURVEYS__Recipient__c();
recipient.Name = 'This is a trigger test';
recipient.TIMBASURVEYS__RelatedAccount__c =acc.id;
recipient.TIMBASURVEYS__DateSendInvitation__c = date.PARSE('07/11/2012');
insert recipient;
}
}

 If still getting problem check the data is there any other fields are missing and give values to those which definatly solve your problem.

 

 

This was selected as the best answer
notsosmartnotsosmart

Thanks so much.