1

I expect this has been described before but have been unable to find it. I am entering OpportunityLineItemSchedule in Apex and would like wipe out the default ones, which are created based on Close Date (meaningless to our users!). I would like to create the 12 month schedule based on today. I know I need to iterate over the list 12x adding 1 month to the date.

Help or pointers to examples of this would be appreciated.

My variables are:

Date today = system.today();
date SchedStart = today.toStartOfMonth();
Date OneYear = SchedStart+365;
Date EndofSched = OneYear.toStartOfMonth();
Decimal InitialSchedAmount = 0;

Answer - Here is what worked:

        Date today = system.today();
        date SchedStart = today.toStartOfMonth();
        Date OneYear = SchedStart+365;
        Date EndofSched = OneYear.toStartOfMonth();
        Decimal InitialSchedAmount = 0;

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

        for (Integer i=0;i<12; i++){
            OpportunityLineItemSchedule OSS = new OpportunityLineItemSchedule (
            OpportunityLineItemId=ELI.id,
            Revenue = 0,
            Type = 'Revenue',
            ScheduleDate = SchedStart.addMonths(i));
            RSSDatesFixed.add(OSS);
        }
        insert RSSDatesFixed;
1

I think, it will be like this:

Date today = system.today();
date SchedStart = today.toStartOfMonth();

List<Date> lstDate = new List<Date>();
for(Integer i=1;i<13; i++)
{
    SchedStart = SchedStart.addMonths(i);
    lstDate.add(SchedStart);
}

Update

According to your updated code, it will look like this:

Date today = system.today();
date SchedStart = today.toStartOfMonth();

List <OpportunityLineItemSchedule> RSSDatesFixed = new List <OpportunityLineItemSchedule>();
for(Integer i=1;i<13; i++)
{
    OpportunityLineItemSchedule OSS = new OpportunityLineItemSchedule (
    OpportunityLineItemId=ELI.id,
    Revenue = 0,
    Type = 'Revenue',
    ScheduleDate = SchedStart.addMonths(i));
    RSSDatesFixed.add(OSS);
}
insert RSSDatesFixed;
  • Thanks - I want a resulting list of OpportunityLineItemSchedule - so I should be adding to a list of OpportunityLineItemSchedule no? – jaw999 Aug 9 '17 at 20:17
  • I see the logic but am stuck on the syntax for use with the object. – jaw999 Aug 9 '17 at 20:26
  • I used this to make the answer – jaw999 Aug 9 '17 at 20:42
  • Did you copy from my question into your answer? – jaw999 Aug 9 '17 at 20:50
  • I think issue will be OpportunityLineItemId=ELI.id, line – Santanu Boral Aug 9 '17 at 20:51
0

I would imagine you'd want to use the "ServiceDate" field on the OpportunityLineItem object to indicate the start date for the schedule, correct?

Are all of your flights going to be 12 months or do you need to allow users to indicate 6, 12, 18, 24 month durations etc?

You would want a trigger on OpportunityLineItem that uses details from the Line Item to create your schedule.

So, resulting code could look like this

Trigger

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

    if(trigger.isAfter){
        if(trigger.isInsert){
            OpportunityLineItemMethods.LineItemInserts(trigger.new);
        }
        if(trigger.isUpdate){
            OpportunityLineItemMethods.LineItemUpdates(trigger.newMap,trigger.oldMap);
        }
    } 

}

Helper Class

    public class Opportunity_Methods {

    public static void LineItemInserts(List<OpportunityLineItem> newList){
        List<OpportunityLineItem> workingList = new List<OpportunityLineItem>();

        for(OpportunityLineItem ol : newList){
            if(ol.ServiceDate != null && ol.Schedule_Duration__c != null){
                workingList.add(ol);
            }
        }

        if(workingList.size()>0){
            createSchedules(workingList);
        }

    }

    public static void LineItemUpdates(Map<Id,OpportunityLineItem> newMap, Map<Id,OpportunityLineItem> oldMap){
        List<OpportunityLineItem> workingList = new List<OpportunityLineItem>();

        for(OpportunityLineItem ol : newMap.values()){
            if((ol.ServiceDate != oldMap.get(ol.id).ServiceDate || ol.Schedule_Duration__c != oldMap.get(ol.id).Schedule_Duration__c) && (ol.ServiceDate != null && ol.Schedule_Duration__c != null){
                workingList.add(ol);
            }
        }

        if(workingList.size()>0){
            createSchedules(workingList);
        }
    }

    public static void createSchedules(List<OpportunityLineItem>workingList){
        List<OpportunityLineItemSchedule> newSchedules = new List<OpportunityLineItemSchedule>();

        List<OpportunityLineItemSchedule> deleteList = [SELECT Id FROM OpportunityLineItemSchedule WHERE OpportunityLineItemID IN :workingList];

        if(deleteList.size()>0){
            delete deleteList;  
        }

        for(OpportunityLineItem ol : workingList){
            Date dt = ol.ServiceDate.toStartofMonth();
            for(Integer i = 0;i<ol.Schedule_Duration__c;i++){
                OpportunityLineItemSchedule sched = new OpportunityLineItemSchedule();
                sched.OpportunityLineItemId = ol.id;
                sched.Quantity = 1;
                sched.ScheduleDate = dt.addMonths(i);
                sched.Revenue = ol.TotalPrice / ol.Schedule_Duration__c;
                newSchedules.add(sched);
            }
        }

        if(newSchedules.size()>0){
            insert newSchedules;
        }

    }

}
  • Sorry If it wasn't clear, but this is in a class already scheduling items. I have built the answer - revealed in my question with new edit. – jaw999 Aug 9 '17 at 20:43
  • I assume that's not your entire code? There are a lot of glaring inefficiencies if that is the bulk of your code. – sfdcJameson Aug 9 '17 at 20:47
  • It's not the entire class. This is a Visualforce extension to limit / handhold users into adding products. I am trying to do it all for them and current month + next 12 was determined to be the most useful template. – jaw999 Aug 9 '17 at 20:54
  • Ah, gotcha. In that case, you could use the example you indicated. This answer is to create a trigger that will perform that action. – sfdcJameson Aug 9 '17 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.