Skip to main content
Jaymarc San Luis (musQueteer) 님이 #Apex에 질문했습니다

Hi, can somebody help me in my trigger?

I need to create a Opportunity Line Item after I update my Opportunity.

I do get this error:

CreateOppLineItems: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 0066E000006s2Pi) is currently in trigger CreateOppLineItems, therefore it cannot recursively update itself: [] Trigger.CreateOppLineItems: line 102, column 1

Here is my code:

trigger CreateOppLineItems on Opportunity (before update) {

for(Opportunity opp : Trigger.New) {

List<OpportunityLineItem> olil = new List <OpportunityLineItem>();

String availableText = '';

if (opp.Requested_Available_Versions__c == null && opp.Requested_available_digital_version__c != null) {

availableText = opp.Requested_available_digital_version__c;

} else if (opp.Requested_Available_Versions__c != null && opp.Requested_available_digital_version__c == null) {

availableText = opp.Requested_Available_Versions__c;

} else if (opp.Requested_Available_Versions__c != null && opp.Requested_available_digital_version__c != null) {

availableText = opp.Requested_Available_Versions__c + '\n' + opp.Requested_available_digital_version__c;

}

if (availableText.contains('[') && opp.StageName == 'Initial Email Sent') {

String prodStatus = '';

String prodLogs = '';

Integer totalAvailable = 0;

Integer totalFailed = 0;

// Loop Requested Available Versions

String ravString = opp.Requested_Available_Versions__c;

if (ravString != null) {

List<String> ravStringList = ravString.split('\\n');

for (String s : ravStringList) {

totalAvailable+=1;

String prodId = s.substringBetween('[',']');

Product2[] objProdList = [SELECT Id, ProductId__c, IsActive FROM Product2 WHERE IsActive = true AND ProductId__c = :prodId];

String pid = '';

if (objProdList.size()>0) {

Product2 objProd = objProdList[0];

pid = objProd.Id;

}

if (pid != '' || pid != null) {

OpportunityLineItem[] objOliList = [SELECT Id FROM OpportunityLineItem WHERE Product2Id = :pid AND OpportunityId = :opp.Id];

String oliid = '';

if (objOliList.size()>0) {

OpportunityLineItem objOli = objOliList[0];

oliid = objOli.Id;

}

if (oliid != '' || oliid != null) {

OpportunityLineItem olinew = new OpportunityLineItem(Product2Id = pid,

OpportunityId = opp.Id,

ServiceDate = System.today(),

Quantity = 1,

TotalPrice = 0);

olil.add(olinew);

}

} else {

totalFailed+=1;

}

}

}

// Loop Requested Available Digital Versions

String radvString = opp.Requested_available_digital_version__c;

if (radvString != null) {

List<String> radvStringList = radvString.split('\\n');

for (String s : radvStringList) {

totalAvailable+=1;

String prodId = s.substringBetween('[',']');

Product2[] objProdList = [SELECT Id, ProductId__c, IsActive FROM Product2 WHERE IsActive = true AND ProductId__c = :prodId];

String pid = '';

if (objProdList.size()>0) {

Product2 objProd = objProdList[0];

pid = objProd.Id;

}

if (pid != '' || pid != null) {

OpportunityLineItem[] objOliList = [SELECT Id FROM OpportunityLineItem WHERE Product2Id = :pid AND OpportunityId = :opp.Id];

String oliid = '';

if (objOliList.size()>0) {

OpportunityLineItem objOli = objOliList[0];

oliid = objOli.Id;

}

if (oliid != '' || oliid != null) {

OpportunityLineItem olinew = new OpportunityLineItem(Product2Id = pid,

OpportunityId = opp.Id,

ServiceDate = System.today(),

Quantity = 1,

TotalPrice = 0);

olil.add(olinew);

}

} else {

totalFailed+=1;

}

}

}

if (totalFailed == 0) {

prodStatus = '01-Successfully Created';

} else {

if (totalFailed == totalAvailable) {

prodStatus = '03-Failed';

prodLogs = 'Opportunity Product failed to be created.';

} else {

prodStatus = '02- Partially Created';

prodLogs = 'Opportunity Product created with the Product Id and Opportunity product failed to be created with the Product Id.';

}

}

//opp.Opportunity_Product_Status__c = prodStatus;

//opp.Log__c = prodLogs;

}

insert olil;

}

}

답변 3개
  1. 2019년 7월 17일 오전 7:07
    Hi Jaymarc,

    Greetings to you!

    As the error says, you're trying to update the opportunity recursively by creating new line items. This happens automatically, as the Amount field has to be updated when adding new line items. No "recursion blocker" will fix your problem, because the update happens automatically by the system.

    To fix the problem, make sure you're only recursively updating the opportunity in an after update trigger:

    trigger CreateOppLineItems on Opportunity (after update) {

    You will likely need to make further changes to your trigger, but this one change will at least resolve the current error message.

    I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

    Thanks and Regards,

    Deepali Kulshrestha.
0/9000