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
Jay PatelJay Patel 

SELF_REFERENCE_FROM_TRIGGER -- Error while updating Quote item Records when Quote is updated

Hi there!! -- I'm little new to apex code development and looks like making a basic mistake in apex trigger. 

 

The use case is -- if one of the quote header dicount fields are updated, i need to update the quote items with the discount. However when i try to update using the apex trigger i get self reference error.

 

Can someone please suggest what am i doing wrong here?

 

Thanks in advance.

Jay.

 

Apex Code

************************************************************************************** 

 

trigger QuoteItemDiscountUpdate on Quote (before update) {

 

 

if (Trigger.isUpdate)
{
for (Quote NewQuote: Trigger.new)
{
Quote OldQuote = Trigger.oldMap.get(NewQuote.ID);
system.debug('QuoteId - ' + OldQuote.ID );
//system.debug(NewQuote.Contractual_Discount__c);
//system.debug(OldQuote.Contractual_Discount__c );

if (NewQuote.Contractual_Discount__c != OldQuote.Contractual_Discount__c || NewQuote.DAN_Discount__c != OldQuote.DAN_Discount__c || NewQuote.CAT_A_Discount__c != OldQuote.CAT_A_Discount__c || NewQuote.CAT_B_Discount__c != OldQuote.CAT_B_Discount__c || NewQuote.CAT_C_Discount__c != OldQuote.CAT_C_Discount__c || NewQuote.CAT_D_Discount__c != OldQuote.CAT_D_Discount__c || NewQuote.CAT_G_Discount__c != OldQuote.CAT_G_Discount__c || NewQuote.CAT_J_Discount__c != OldQuote.CAT_J_Discount__c || NewQuote.CAT_M_Discount__c != OldQuote.CAT_M_Discount__c || NewQuote.CAT_N_Discount__c != OldQuote.CAT_N_Discount__c || NewQuote.CAT_O_Discount__c != OldQuote.CAT_O_Discount__c || NewQuote.CAT_Q_Discount__c != OldQuote.CAT_Q_Discount__c)
{
system.debug('Discount Values changed...Update Quote Items -- Start');

list<QuoteLineItem> QuoteLineItemToUpdate= new list<QuoteLineItem>();

// field was updated, do some magic here
QuoteLineItem[] QuoteList = [SELECT QuoteId, Id, Quote_DAN_Discount__c, Quote_Contractual_Discount__c, Total_Discount__c, Discount, CAT_Discount_Percent__c FROM QuoteLineItem where quoteId = :OldQuote.ID];

for(QuoteLineItem sQuoteItem : QuoteList)
{
system.debug('Before - ' + sQuoteItem.Discount );
sQuoteItem.Discount = sQuoteItem.Total_Discount__c;
system.debug('After - ' + sQuoteItem.Discount );
//QuoteLineItemToUpdate.add(sQuoteItem);
}
update QuoteList;
system.debug('Discount Values changed...Update Quote Items - End');
}
}
}

}

Jay PatelJay Patel

This is resolved now. I changed just the code a bit and made the trigger to be after update and It worked. Thanks.

 

trigger QuoteDiscountUpdate on Quote (after update) {

if (Trigger.isUpdate)
{
for (Quote NewQuote: Trigger.new)
{
Quote OldQuote = Trigger.oldMap.get(NewQuote.ID);
system.debug('QuoteId - ' + OldQuote.ID );
//system.debug(NewQuote.Contractual_Discount__c);
//system.debug(OldQuote.Contractual_Discount__c );

if (NewQuote.Contractual_Discount__c != OldQuote.Contractual_Discount__c || NewQuote.DAN_Discount__c != OldQuote.DAN_Discount__c || NewQuote.CAT_A_Discount__c != OldQuote.CAT_A_Discount__c || NewQuote.CAT_B_Discount__c != OldQuote.CAT_B_Discount__c || NewQuote.CAT_C_Discount__c != OldQuote.CAT_C_Discount__c || NewQuote.CAT_D_Discount__c != OldQuote.CAT_D_Discount__c || NewQuote.CAT_G_Discount__c != OldQuote.CAT_G_Discount__c || NewQuote.CAT_J_Discount__c != OldQuote.CAT_J_Discount__c || NewQuote.CAT_M_Discount__c != OldQuote.CAT_M_Discount__c || NewQuote.CAT_N_Discount__c != OldQuote.CAT_N_Discount__c || NewQuote.CAT_O_Discount__c != OldQuote.CAT_O_Discount__c || NewQuote.CAT_Q_Discount__c != OldQuote.CAT_Q_Discount__c)
{
system.debug('Discount Values changed...Update Quote Items -- Start');

list<QuoteLineItem> QuoteLineItemToUpdate= new list<QuoteLineItem>();

// field was updated, do some magic here
QuoteLineItem[] QuoteList = [SELECT QuoteId, Id, Quote_DAN_Discount__c, Quote_Contractual_Discount__c, Total_Discount__c, Discount, CAT_Discount_Percent__c FROM QuoteLineItem where quoteId = :OldQuote.ID];
system.debug('Discount Values changed...Update Quote Items - End');

for(QuoteLineItem sQuoteItem : QuoteList)
{
system.debug('Before - ' + sQuoteItem.Discount );
sQuoteItem.Discount = sQuoteItem.Total_Discount__c;
system.debug('After - ' + sQuoteItem.Discount );
//QuoteLineItemToUpdate.add(sQuoteItem);
update sQuoteItem ;
}
//update QuoteList;

}
}
}

}

Laxman RaoLaxman Rao

Hi Jay,

 

A small advice from my side, do not perform dml operation inside the for loop.You might hit the governor limits.

 

you have written this statement "update sQuoteItem;" inside the for loop.