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
Conor Bradley 5Conor Bradley 5 

apex trigger suggestions/help!!

Hi, we are a publishing company that sell adverts into different magazine 'Issues'(custom SF Object)

I have an apex trigger called EndOfSeriesIssue that takes the latest Issue from opportunityLineItem and puts it to a look up field on opportunity called last_issue__c, based on Issue_publication_date__c

This works fine. However there is a problem when two issues have the same Issue_publication_date__c. Could anyone tell me how to modify my trigger to deal with this problem? Trigger below:


trigger EndOfSeriesIssue on Opportunity (before update) {
    
    for (Opportunity o : Trigger.new) {
        
        OpportunityLineItem[] LineItemArray =
       [select Issue__c, Issue_publication_date__c from OpportunityLineItem where OpportunityId = :o.id ORDER BY Issue_publication_date__c DESC];
       if (LineItemArray.size() > 0) {
           
           o.last_issue__c = LineItemArray[0].Issue__c;
           
       }else{
           
            o.last_issue__c = null;   
           
      }
   }
}
Best Answer chosen by Conor Bradley 5
Niraj Kr SinghNiraj Kr Singh
Hi Conor Bradley,

There is problem with your code.
You have written SOQL inside FOR loop which will cause Exception : "System.LimitException: Too many SOQL queries: 101", when you will do bulk update for more that 100 opportunities.

I have simplified and bulkified the trigger, Plz try it once
trigger EndOfSeriesIssue on Opportunity (before update) {
    
    if(trigger.isBefore && trigger.isUpdate) {
        List<OpportunityLineItem> oliList = [SELECT Issue__c, Issue_publication_date__c, OpportunityId FROM OpportunityLineItem WHERE OpportunityId In:trigger.newMap.keySet() ORDER BY Issue_publication_date__c DESC];
        Map<Id, String> oppIdVsLatestIssueMap = new Map<Id, String>();
        if(oliList != null) {
            for(OpportunityLineItem objOLI : oliList) {
                if(!oppIdVsLatestIssueMap.containsKey(objOLI.OpportunityId)) { //Add only one OppId and its Issue which is having latest Issue_publication_date
                    oppIdVsLatestIssueMap.put(objOLI.OpportunityId, objOLI.Issue__c);
                }
            }
            
            //Updated latest issue to opportunities
            if(!oppIdVsLatestIssueMap.isEmpty()) {
                for(Id filterdOppId : oppIdVsLatestIssueMap.keySet()) {
                    trigger.newMap.get(filterdOppId).last_issue__c = oppIdVsLatestIssueMap.get(filterdOppId);
                }
            }
            
        }
    }
}
Note: Mightbe be some small update needed from ur side.

Plz let me know if it works for you.

Mark it ur Ans if works to help others.

Thanks
Niraj
 

All Answers

Shubham NandwanaShubham Nandwana
Hi Conor,
If you want the latest issue from opportunityLineItem then you can use a dateTime type field to sort in descending order, this way you will always get the latest record. Create a Issue_publication_dateTime__c field in OpportunityLineItem object.
While creating a new field in sobject choose Date/Time as data Type.
Refer to below link for datetime methods in apex.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_datetime.htm 

Please select this as best answer if it helps or reply if you wanted something else.

Shubham Nandwana.
AppPerfect Corp.
salesforce@appperfect.com
408-252-4100
http://www.appperfect.com/services/salesforce/
Salesforce Development & Operations Experts
Niraj Kr SinghNiraj Kr Singh
Hi Conor Bradley,

There is problem with your code.
You have written SOQL inside FOR loop which will cause Exception : "System.LimitException: Too many SOQL queries: 101", when you will do bulk update for more that 100 opportunities.

I have simplified and bulkified the trigger, Plz try it once
trigger EndOfSeriesIssue on Opportunity (before update) {
    
    if(trigger.isBefore && trigger.isUpdate) {
        List<OpportunityLineItem> oliList = [SELECT Issue__c, Issue_publication_date__c, OpportunityId FROM OpportunityLineItem WHERE OpportunityId In:trigger.newMap.keySet() ORDER BY Issue_publication_date__c DESC];
        Map<Id, String> oppIdVsLatestIssueMap = new Map<Id, String>();
        if(oliList != null) {
            for(OpportunityLineItem objOLI : oliList) {
                if(!oppIdVsLatestIssueMap.containsKey(objOLI.OpportunityId)) { //Add only one OppId and its Issue which is having latest Issue_publication_date
                    oppIdVsLatestIssueMap.put(objOLI.OpportunityId, objOLI.Issue__c);
                }
            }
            
            //Updated latest issue to opportunities
            if(!oppIdVsLatestIssueMap.isEmpty()) {
                for(Id filterdOppId : oppIdVsLatestIssueMap.keySet()) {
                    trigger.newMap.get(filterdOppId).last_issue__c = oppIdVsLatestIssueMap.get(filterdOppId);
                }
            }
            
        }
    }
}
Note: Mightbe be some small update needed from ur side.

Plz let me know if it works for you.

Mark it ur Ans if works to help others.

Thanks
Niraj
 
This was selected as the best answer
Conor Bradley 5Conor Bradley 5
Hi @Niraj

Thanks for this, I was wondering how to bulkify this code. When I tried this code I got this error: 'Method does not exist or incorrect signature: void containKey() from the type Map<Id,String>' on line 8. Do you know why this is?
Conor Bradley 5Conor Bradley 5
HI @Shubham

Issue_publication_date__c is a date field so shouldn't really hav a time. But I see the logic behind this solution so thank you, I will test with date/time field.

 
Niraj Kr SinghNiraj Kr Singh
Hi Conor Bradley,

i  have updated the above code. Plz try once again it will work.
While posting, that method parameter was got deleted. That's way error was coming.

Thanks
Niraj
Niraj Kr SinghNiraj Kr Singh
Hi Conor Bradley,

Is this solved your problem?
Conor Bradley 5Conor Bradley 5
Hi Niraj

That has solved this issue. Thanks so much for your help.

Conor