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
domdickdomdick 

Help on OLI Trigger???

Hello,

 

I am trying to right a trigger to if ServiceDate (from OpportunityLineItem object) change then count ScheduleDate from OpportunityLineItemSchedule object and upate the results on custom field 'RevenueSchedule__c' on OLI standard object.

 

the code may not proper to use. can someone help to get it right please?

 

Many Thanks,

 

trigger revenueUpdate on OpportunityLineItem (after insert, after update) {

    //List<Id> OLIIds = New List<Id>();
    public Double res {get; set;}
    
    List<OpportunityLineItem>updateField = new List<OpportunityLineItem>();
    Set<Id> pbeIds = new Set<Id>();
    for (OpportunityLineItem oli : Trigger.new)
    {
        OpportunityLineItem oldOLI = Trigger.oldMap.get(oli.Id);
        if(oli.ServiceDate != oldOLI.ServiceDate){
            pbeIds.add(oldOLI.Id);
        }
    }
    LIST<AggregateResult> groupedResults  = [Select count(ScheduleDate)OLIsch FROM OpportunityLineItemSchedule where OpportunityLineItemId In: pbeIds ];//Group by OpportunityLineItemId];
    LIST<OpportunityLineItem> Opls = [Select RevenueSchedule__c from OpportunityLineItem where Id In: pbeIds];
    Map<id, OpportunityLineItem> OLINum = new Map<id, OpportunityLineItem>();
    for (OpportunityLineItem oli : Trigger.new){
        OLINum.put(oli.Id,oli);
    }
    for(Integer i=0; i<groupedResults.size(); i++){
        Object numberof = groupedResults[i].get('OLIsch');
        //updateField.RevenueSchedule__c = groupedResults[i].get('OLIsch');
        res =Double.ValueOf(groupedResults[i].get('OLIsch'));
        
    }
    //update updateField;
     Opls.RevenueSchedule__c = res; 
}

 

SammyComesHereSammyComesHere

all i can say rite now is you are trying to call an update on the object which initially fired the trigger. Recursive calling is what you are trying . please use a static counter as 1 and update statement in the end to succeed. 

 

Didnt got time to look at the code but you need to write update statement in the end to update the changes.

 

if(TriggerRecusionCounter.Count ==1)
{
your code
Opls.RevenueSchedule__c = res; // code from your line
update opls
}

public class TriggerRecursionCounter
{
public static Count ==1
}
domdickdomdick

It didn't work!

Any other possible solution to my initial post?

 

Thanks,