You need to sign in to do that
Don't have an account?
travis.truett
Opportunity Product Trigger not Working
I wrote a trigger that edits the schedule of an opportunity product once it's created. Here's the trigger:
trigger editSchedule on OpportunityLineItem (after insert) {
if(Trigger.isInsert){
List<OpportunityLineItemSchedule> listOLIS = new List<OpportunityLineItemSchedule>();
for (OpportunityLineItem oli: Trigger.new){
Date myDate = Date.today();
for(Integer duration = (Integer)oli.Duration__c; duration == 0; duration--){
listOLIS.add(new OpportunityLineItemSchedule(OpportunityLineItemId = oli.Id, Quantity = 1, ScheduleDate = myDate));
myDate.addMonths(1);
}//end of inner for loop
}//end of for loop
insert listOLIS;
}//end of isInsert
}//end of trigger
Basically, it pulls in a custom field that I created called Duration, which is an integer representing how many months the client will be using the particular product. I want to add a schedule entry once a month based on the size of duration. For some reason though, when I add a product to an opportunity, my trigger doesn't change the schedule at all.
trigger editSchedule on OpportunityLineItem (after insert) {
if(Trigger.isInsert){
List<OpportunityLineItemSchedule> listOLIS = new List<OpportunityLineItemSchedule>();
for (OpportunityLineItem oli: Trigger.new){
Date myDate = Date.today();
for(Integer duration = (Integer)oli.Duration__c; duration == 0; duration--){
listOLIS.add(new OpportunityLineItemSchedule(OpportunityLineItemId = oli.Id, Quantity = 1, ScheduleDate = myDate));
myDate.addMonths(1);
}//end of inner for loop
}//end of for loop
insert listOLIS;
}//end of isInsert
}//end of trigger
Basically, it pulls in a custom field that I created called Duration, which is an integer representing how many months the client will be using the particular product. I want to add a schedule entry once a month based on the size of duration. For some reason though, when I add a product to an opportunity, my trigger doesn't change the schedule at all.
addmonths method do not update the original date varaiable reference it returns the updated date , you should assign it to the varaible
if(!SetMonthlyAmountHelper.hasAlreadySetMonthlyAmount()) {
for (OpportunityLineItem LI : trigger.new) {
PricebookEntry pbentry = [select Product2Id from PricebookEntry where Id = :LI.PriceBookEntryId];
Product2 prod = [select CanUseRevenueSchedule, NumberOfRevenueInstallments from Product2 where Id = :pbentry.Product2Id];
if (prod.CanUseRevenueSchedule==true && prod.NumberofRevenueInstallments>0) {
OpportunityLineItem opp_prod = [select Monthly_amount__c, Contract_duration__c from OpportunityLineItem where Id = :LI.Id];
List<OpportunityLineItemSchedule> schedule = [select ScheduleDate from OpportunityLineItemSchedule where OpportunityLineItemId = :LI.Id];
if (schedule.size()==0)
opp_prod.Monthly_amount__c = LI.UnitPrice;
else
opp_prod.Monthly_amount__c = LI.UnitPrice / schedule.size();
opp_prod.Contract_duration__c = schedule.size();
SetMonthlyAmountHelper.setAlreadySetMonthlyAmount();
Update opp_prod;
}
}
}
}
I also found this code in another forum post. I'm not sure what some of these functions are, but would any of this be helpful in my case?
only runs if Oli.duration__c value is equal to zero . if the line item duration__c field value is not equal to zero it never enters the loop
Apex trigger editSchedule caused an unexpected exception, contact your administrator: editSchedule: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Type]: [Type]: Trigger.editSchedule: line 20, column 1
eg:
for more info refer https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_opportunitylineitemschedule.htm
please mark this best answere if it helped to resolve your issue