Skip to main content

I am an admin trying to help solve for an Apex trigger issue that is giving us a very hard time. I cannot even replicate the issue on command for troubleshooting purposes. Below is a developer's description of the issue along with supplemental screenshots.   

 

4 years ago we have created a Trigger "BeforeUpdate" for the opportunities.

When a specific changes is made, we request all the Opportunity Line Items associated and put them into a Map:  Map<Opportunity,List<OpportunityLineItem>> mapOppsOlisToSendJira = new Map<Opportunity,List<OpportunityLineItem>>();  This map is the Input parameter of another method "setAndSendJiraOpportunityDescription".

 

Until 3 months ago the trigger and apex class were fine, in the trigger I put key and values into the map. And after that the method "setAndSendJiraOpportunityDescription" browses the keySet() and for each Opportunity, I get the associated Opp Line Items.  But since 3 months ago we have occasional errors where for my Opportunity I lose the associated values. In the trigger where I put the values, I have a debug log to check it. And it's all right.

 

But in the method "setAndSendJiraOpportunityDescription", when I check again, I have "null" as values. In the complete log, we can see the variable assignment therefore we still have the Opp Line Item. And in the first line of this method, in the debug log, we lost them.

 

Map<Opportunity,List<OpportunityLineItem>> mapOppsOlisToSendJira = new Map<Opportunity,List<OpportunityLineItem>>();

List<Id> oppsIdsToSendJira = new List<Id>();

for (Opportunity o : Trigger.new) {

String id = o.id;

Opportunity old = Trigger.oldMap.get(o.id);

if (!old.StageName.contains('IO sent') && o.StageName.contains('IO sent')) {

oppsIdsToSendJira.add(id);

}

System.debug('TG003_beforeUpdateOpp oppsIdsToSendJira :' + oppsIdsToSendJira);

List<OpportunityLineItem> olis = [SELECT Id, Country__c, TotalPrice, Target__c, POI__c, Devices__c,OS__c, Creative_format__c, UnitPrice, Sales_price__c, Region__c, Quantity,Tracker_Line_Name__c, End_Date__c, Start_date__c, OpportunityId, Product2Id, Product2.IsCPM__c, Product2.Name, Product2.Compliance__c,Product2.IsAddon__c, Product2.Add_on_type__c, Product2.Integration__c, CurrencyIsoCode, Description, TECH_ExternalKey__c FROM OpportunityLineItem WHERE OpportunityId in :oppsIdsToSendJira ORDER BY CreatedDate ASC];

for(Opportunity o : Trigger.new) {

if(oppsIdsToSendJira.contains(o.Id)) {

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

for(OpportunityLineItem oli : olis) {

if(oli.OpportunityId == o.id) {

olisConcerned.add(oli);

}

}

mapOppsOlisToSendJira.put(o, olisConcerned);

}

}

System.debug('TG003_beforeUpdateOpp mapOppsOlisToSendJira :' + mapOppsOlisToSendJira);

if(mapOppsOlisToSendJira.size() > 0) {

S4M001_Opportunity.setAndSendJiraOpportunityDescription(mapOppsOlisToSendJira, true);

}

 

public static void setAndSendJiraOpportunityDescription (Map<Opportunity,List<OpportunityLineItem>> mapOpsOlis, Boolean isCreationIssuejira) {

system.debug('setAndSendJiraOpportunityDescription opp begin: ' + mapOpsOlis);

system.debug('setAndSendJiraOpportunityDescription !isCreationIssuejira : ' + isCreationIssuejira);

S4M_UtilBypass.bypassedTriggers.put('setOpportunityDescription','Avoid infinity and recall TriggerafterUpdateOppLI');

Set<Opportunity> opps = mapOpsOlis.keySet();

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

Set<String> newTrackerLineNames = new Set<String>();

for (Opportunity opp : opps) {

system.debug('setAndSendJiraOpportunityDescription opp : ' + opp);

List<OpportunityLineItem> olis = mapOpsOlis.get(opp);

system.debug('setAndSendJiraOpportunityDescription olis : ' + olis);

if(opp.StageName == 'Campaign validated' || opp.StageName == 'IO sent') {

opp.Send_To_Jira__c = true;

}

opp.Description = 'Campaign \r Salesforce Record ID:' + opp.Id;

4 respuestas
  1. Ruchit Patel (Cognizant) Forum Ambassador
    11 nov 2022, 06:50

    Hi @Michael Weintraub I verified debug log and it seems issue is because of that NULL value we are getting in MAP.

    As a best practice, we should keep NULL check in condition before using it

    If you can share your whole setAndSendJiraOpportunityDescription method here or drop me an email.

    I may be able to give you an enhanced version of the code following all the best practices.

0/9000