You need to sign in to do that
Don't have an account?
Richa Paliwal 18
Test class to to test the trigger that updates parent record when a child record is inserted or updated.
I am getting on 50% coverage on this trigger below:
Note: Sales order is a child object of opportunity and all I am odoing is updating a custom field on parent when a child is inserted or updated.
trigger ContractSourceUpdateOnOpp on Sales_Order__c (after insert, after update) {
if(Trigger.isInsert){
try {
for (Sales_Order__c SO : Trigger.new){
Opportunity Op = [SELECT Id, Contract_Source__c FROM Opportunity WHERE Id = :SO.Opportunity__c];
List<Sales_Order__c> l_so = [SELECT Id, Contract_Source__c FROM Sales_Order__c WHERE Opportunity__c = :Op.Id];
for(Sales_Order__c cs_so : l_so) {
Op.Contract_Source__c = cs_so.Contract_Source__c;
}
//Op.Contract_Source__c = ContractSource;
update Op;
}
} catch (Exception e) {
System.debug(e);
}
}
if(Trigger.isUpdate){
try {
for (Sales_Order__c SO : Trigger.old){
Opportunity Op = [SELECT Id, Contract_Source__c FROM Opportunity WHERE Id = :SO.Opportunity__c];
system.debug('Contract Source on opp##' +Op.Contract_Source__c);
system.debug('Id@@' +Op.Id);
List<Sales_Order__c> l_so = [SELECT Id, Contract_Source__c FROM Sales_Order__c WHERE Opportunity__c = :Op.Id];
for(Sales_Order__c cs_so : l_so) {
Op.Contract_Source__c = cs_so.Contract_Source__c;
system.debug('Contract Source##' +Op.Contract_Source__c);
}
system.debug('contract source updated');
update Op;
system.debug('Opp##');
}
} catch (Exception e) {
System.debug(e);
}
}
}
Here is my test class:
@istest
public class TestContractSourceUpdateOnOpp {
static testmethod void validateContractSource()
{
Test_SharedNewAccount.createAccount('Test Account');
// Test Creation of the Shared Account
Account acct = [SELECT Id FROM Account WHERE Name = 'Test Account' LIMIT 1];
System.Assert(acct != null, 'Account was not inserted properly!');
//Create a Opportunity
Opportunity Op = new Opportunity(ACCOUNTID = acct.Id, Name = 'TestOp',Anticipatged_1st_Test_Season__c = 'Fall 2017',ForecastCategoryName = 'Prospect & Discovery',Imp_Case_Created__c = null ,Closed_Won_Reason__c = 'Relationships', Closed_Won_Detail__c = 'Relationship with Influencer', Region__c = 'East Enrollments' , Type = 'Renewal',Test_Alignment_Option__c = 'Other (Specify in Notes)' , StageName = 'Prospect', License_Start_Date__c = date.newInstance(2018,03,01), License_End_Date__c = date.newInstance(2018,03,31), CloseDate = Date.newInstance(2018,03,30), MAP_Growth_MAP_Skills_Implementation__c = 'Standard');
insert Op;
system.debug('Insert opportunity' + Op.Id);
system.debug('Insert opportunity' + Op.contract_Source__c);
Sales_Order__c SO = new Sales_Order__c(Name = 'Test Sales Order', Opportunity__c = Op.Id, Account__c = acct.id, Contract_Source__c = 'Test Contract Source');
insert SO;
system.debug('Sales order inserted' +SO);
Test.StartTest();
{
SO.Contract_Source__c = 'Test Source';
update SO;
system.debug('Try updating Sales order with Contract Status'+SO.Contract_Source__c);
system.debug('opp contract source' + Op.Contract_Source__c);
System.assert(Op.Contract_Source__c == 'Test Source');
system.debug('Catch the validation'+Op);
}
Test.StopTest();
}
}
this debug line shows 'Null'.
Can someone please suggest what am I doing wrong?
Note: Sales order is a child object of opportunity and all I am odoing is updating a custom field on parent when a child is inserted or updated.
trigger ContractSourceUpdateOnOpp on Sales_Order__c (after insert, after update) {
if(Trigger.isInsert){
try {
for (Sales_Order__c SO : Trigger.new){
Opportunity Op = [SELECT Id, Contract_Source__c FROM Opportunity WHERE Id = :SO.Opportunity__c];
List<Sales_Order__c> l_so = [SELECT Id, Contract_Source__c FROM Sales_Order__c WHERE Opportunity__c = :Op.Id];
for(Sales_Order__c cs_so : l_so) {
Op.Contract_Source__c = cs_so.Contract_Source__c;
}
//Op.Contract_Source__c = ContractSource;
update Op;
}
} catch (Exception e) {
System.debug(e);
}
}
if(Trigger.isUpdate){
try {
for (Sales_Order__c SO : Trigger.old){
Opportunity Op = [SELECT Id, Contract_Source__c FROM Opportunity WHERE Id = :SO.Opportunity__c];
system.debug('Contract Source on opp##' +Op.Contract_Source__c);
system.debug('Id@@' +Op.Id);
List<Sales_Order__c> l_so = [SELECT Id, Contract_Source__c FROM Sales_Order__c WHERE Opportunity__c = :Op.Id];
for(Sales_Order__c cs_so : l_so) {
Op.Contract_Source__c = cs_so.Contract_Source__c;
system.debug('Contract Source##' +Op.Contract_Source__c);
}
system.debug('contract source updated');
update Op;
system.debug('Opp##');
}
} catch (Exception e) {
System.debug(e);
}
}
}
Here is my test class:
@istest
public class TestContractSourceUpdateOnOpp {
static testmethod void validateContractSource()
{
Test_SharedNewAccount.createAccount('Test Account');
// Test Creation of the Shared Account
Account acct = [SELECT Id FROM Account WHERE Name = 'Test Account' LIMIT 1];
System.Assert(acct != null, 'Account was not inserted properly!');
//Create a Opportunity
Opportunity Op = new Opportunity(ACCOUNTID = acct.Id, Name = 'TestOp',Anticipatged_1st_Test_Season__c = 'Fall 2017',ForecastCategoryName = 'Prospect & Discovery',Imp_Case_Created__c = null ,Closed_Won_Reason__c = 'Relationships', Closed_Won_Detail__c = 'Relationship with Influencer', Region__c = 'East Enrollments' , Type = 'Renewal',Test_Alignment_Option__c = 'Other (Specify in Notes)' , StageName = 'Prospect', License_Start_Date__c = date.newInstance(2018,03,01), License_End_Date__c = date.newInstance(2018,03,31), CloseDate = Date.newInstance(2018,03,30), MAP_Growth_MAP_Skills_Implementation__c = 'Standard');
insert Op;
system.debug('Insert opportunity' + Op.Id);
system.debug('Insert opportunity' + Op.contract_Source__c);
Sales_Order__c SO = new Sales_Order__c(Name = 'Test Sales Order', Opportunity__c = Op.Id, Account__c = acct.id, Contract_Source__c = 'Test Contract Source');
insert SO;
system.debug('Sales order inserted' +SO);
Test.StartTest();
{
SO.Contract_Source__c = 'Test Source';
update SO;
system.debug('Try updating Sales order with Contract Status'+SO.Contract_Source__c);
system.debug('opp contract source' + Op.Contract_Source__c);
System.assert(Op.Contract_Source__c == 'Test Source');
system.debug('Catch the validation'+Op);
}
Test.StopTest();
}
}
this debug line shows 'Null'.
Can someone please suggest what am I doing wrong?
Firstly you need to update your trigger because query in for loop is not the part of best practice.
You can go with the below code:
trigger ContractSourceUpdateOnOpp on Sales_Order__c (after insert, after update) {
if((Trigger.isInsert && Trigger.isAfter) || (Trigger.isUpdate && Trigger.isAfter)){
Set<Id> accIdSet = new Set<Id>;
for (Sales_Order__c SO : Trigger.new){
if(SO.Opportunity__c != NULL){
accIdSet.add();
}
}
List<Opportunity> OppList = new List<Opportunity>();
if(accIdSet.size()>0){
OppList = [SELECT Id, Contract_Source__c FROM Opportunity WHERE Id =: SO.accIdSet LIMIT 10000];
}
if(OppList.size() > 0){
for(Opportunity oppObj : OppList){
for(Sales_Order__c soObj : Trigger.new)
{
if(oppObj.Id == soObj.Opportunity__c)
{
oppObj.Contract_Source__c = soObj.Contract_Source__c;
}
}
}
update OppList;
}
}
}
//Here the test class
@istest
public class TestContractSourceUpdateOnOpp {
static testmethod void validateContractSource()
{
Account accObj = new Account(Name = 'Test Account');
insert accObj;
Opportunity Op = new Opportunity();
if(accObj!=NULL){
Op.ACCOUNTID = acct.Id;
Op.Name = 'TestOp;
Op.Anticipatged_1st_Test_Season__c = 'Fall 2017';
Op.ForecastCategoryName = 'Prospect & Discovery';
Op.Imp_Case_Created__c = null ;
Op.Closed_Won_Reason__c = 'Relationships';
Op.Closed_Won_Detail__c = 'Relationship with Influencer';
Op.Region__c = 'East Enrollments' ;
Op.Type = 'Renewal';
Op.Test_Alignment_Option__c ='Other (Specify in Notes)' ;
Op.StageName = 'Prospect';
Op.License_Start_Date__c = date.newInstance(2018,03,01);
Op.License_End_Date__c = date.newInstance(2018,03,31);
Op.CloseDate = Date.newInstance(2019,06,30);
Op.MAP_Growth_MAP_Skills_Implementation__c = 'Standard';
insert Op;
}
Sales_Order__c SO = new Sales_Order__c(Name = 'Test Sales Order', Opportunity__c = Op.Id, Account__c = acct.id, Contract_Source__c = 'Test Contract Source');
insert SO;
system.debug('Sales order inserted' +SO);
Test.StartTest();
SO.Contract_Source__c = 'Test Source';
update SO;
Test.StopTest();
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
All Answers
Firstly you need to update your trigger because query in for loop is not the part of best practice.
You can go with the below code:
trigger ContractSourceUpdateOnOpp on Sales_Order__c (after insert, after update) {
if((Trigger.isInsert && Trigger.isAfter) || (Trigger.isUpdate && Trigger.isAfter)){
Set<Id> accIdSet = new Set<Id>;
for (Sales_Order__c SO : Trigger.new){
if(SO.Opportunity__c != NULL){
accIdSet.add();
}
}
List<Opportunity> OppList = new List<Opportunity>();
if(accIdSet.size()>0){
OppList = [SELECT Id, Contract_Source__c FROM Opportunity WHERE Id =: SO.accIdSet LIMIT 10000];
}
if(OppList.size() > 0){
for(Opportunity oppObj : OppList){
for(Sales_Order__c soObj : Trigger.new)
{
if(oppObj.Id == soObj.Opportunity__c)
{
oppObj.Contract_Source__c = soObj.Contract_Source__c;
}
}
}
update OppList;
}
}
}
//Here the test class
@istest
public class TestContractSourceUpdateOnOpp {
static testmethod void validateContractSource()
{
Account accObj = new Account(Name = 'Test Account');
insert accObj;
Opportunity Op = new Opportunity();
if(accObj!=NULL){
Op.ACCOUNTID = acct.Id;
Op.Name = 'TestOp;
Op.Anticipatged_1st_Test_Season__c = 'Fall 2017';
Op.ForecastCategoryName = 'Prospect & Discovery';
Op.Imp_Case_Created__c = null ;
Op.Closed_Won_Reason__c = 'Relationships';
Op.Closed_Won_Detail__c = 'Relationship with Influencer';
Op.Region__c = 'East Enrollments' ;
Op.Type = 'Renewal';
Op.Test_Alignment_Option__c ='Other (Specify in Notes)' ;
Op.StageName = 'Prospect';
Op.License_Start_Date__c = date.newInstance(2018,03,01);
Op.License_End_Date__c = date.newInstance(2018,03,31);
Op.CloseDate = Date.newInstance(2019,06,30);
Op.MAP_Growth_MAP_Skills_Implementation__c = 'Standard';
insert Op;
}
Sales_Order__c SO = new Sales_Order__c(Name = 'Test Sales Order', Opportunity__c = Op.Id, Account__c = acct.id, Contract_Source__c = 'Test Contract Source');
insert SO;
system.debug('Sales order inserted' +SO);
Test.StartTest();
SO.Contract_Source__c = 'Test Source';
update SO;
Test.StopTest();
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi