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
GatorGator 

Trigger Insert/Update CustomObject Record upon OpportunityLineItemSchedule changes

End goal is to have a report type that includes the following data-

- Opportunity info

- info from a custom object underneath the opportunity called CustomObjectUnderOpportunity (master-detail relationship with opportunity)

- info from all OpportunityLineItemSchedules under that opportunity

 

 

I believe I need a trigger to make a new object -- Let's call it CustomLineItemSchedule -- that can create a record for each combination.

 ie: 2 custom object records under opp, 3 opportunitylineitemschedules = 6 new link objects created

 

Problem Part 1:

I'm trying to extract the Revenue and ScheduleDate fields from any OpportunityLineItemSchedule record that is created/changed/deleted and drop those values into two fields in CustomLineItemSchedule.

 

I'm aware that you cannot run a trigger directly off of the OpportunityLineItemSchedule, but I believe there is a way to do this by comparing the LastModifiedDate of each OpportunityLineItemSchedule record against the OpportunityLineItem.LastModifiedDate.

 

Part 2: 

Trigger does some combinatorics to create 2*3= 6 new CustomLineItemSchedule records rather than just 3 (one for each opportunitylineitemschedule).  Trigger would have to put a couple fields from the CustomObjectUnderOpportunity record into the new object.

 

 

This allows me to create an 'Opportunity and CustomLineItemSchedule' report with all the fields I need.

 

Does anyone have any suggestions?  GREATLY appreciated. Hope this was explained well...

GatorGator

Anyone?  Could REALLY use some advice here.... Thanks in advance.

fridofrido

basically what you need is not possible.  Cannot set a trigger from a schedule.

 

I have had HUGE problems with the schedules part lately.  Trying to insert but hit soql limits and also cannot update current record, etc.  Any help?

 

This is current code:-

 

trigger allStartDate on OpportunityLineItem (after update) 
{
//trigger set if check box "All Schedules Follow Start Date" is checked

                                              
List <OpportunityLineItemSchedule> lis = new List <OpportunityLineItemSchedule>(); 

for (OpportunityLineItem oppli : Trigger.new)
{
List <OpportunityLineItemSchedule> opplistart = [select id, OpportunityLineItemId from OpportunityLineItemSchedule 
where OpportunityLineItemId = '006e0000002ZJco'];  //:Trigger.newMap.get(oppli.Id).id];

List <OpportunityLineItemSchedule> oToAdd = new List <OpportunityLineItemSchedule>();

Integer i = 0;
        while( i <  Trigger.newMap.get(oppli.Id).Monthly_Instalments__c)
        {
         oToAdd.add(new OpportunityLineItemSchedule(OpportunityLineItemId = opplistart[i].OpportunityLineItemId,
                Type = 'Revenue', Revenue = Trigger.newMap.get(oppli.Id).UnitPrice, ScheduleDate = Trigger.newMap.get(oppli.Id).Schedule_Date__c + (i * 32)));
                //lis[ii].ScheduleDate = lis[i].ScheduleDate.toStartOfMonth();//Trigger.newMap.get(oppli.Id).Schedule_Date__c;        
        //lis.add(oToAdd);          
                i++;      
        }
upsert oToAdd;        
}

}