I'm trying to write a trigger to Update product dates when opp close date changes .Below is what i've tried so far,

trigger productUpdate on Opportunity (after update) {

    // declare list to hold opportuity line item
    List<OpportunityLineItem> olis = new List<OpportunityLineItem>();

    // declare a set to hold opporrunity ids
    // loop through trigger to get all ids of updated opportuniities
    Set<Id> Ids = new Set<Id>();

    // fetch all line items for the opportnities updated order by opportunitiy id
    olis = [SELECT Id, Name, ServiceDate, Opportunity__r.CloseDate FROM OpportunityLineItem WHERE OpportunityId IN :Ids];

    //loop through line items
    for(OpportunityLineItem oli : olis ){
        // perform logic to get date
        oli.ServiceDate = oli.Opportunity__r.CloseDate + (10);
    update olis;


[EDIT] It gives me error as Didn't understand relationship 'Opportunity__r' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.

Please help !

  • 1
    Five points: (1) you didn't populate the variable Ids, (2) you can just use :Trigger.new instead of using a set, (3) you don't need to initialize olis on that line, because a query is initialization, (4) you don't need to query ServiceDate or Name or even Id, and (5) you should probably use Date.addDays instead of +(10), which has the same meaning but may not be as clear to future developers.
    – sfdcfox
    Dec 4, 2014 at 7:17
  • Thanks sfdcfox. I got with (5) point .Could you please help me to achieve the points you mentioned . Not able to see the updated date on products also after i changed the closed date on opp.
    – JJoseph
    Dec 4, 2014 at 7:21
  • 1
    trigger updateServiceDate on Opportunity (after update) { OpportunityLineItem[] items = [SELECT OpportunityId FROM OpportunityLineItem WHERE OpportunityID IN :Trigger.new]; for(OpportunityLineItem item: items) item.ServiceDate = Trigger.newMap.get(item.OpportunityId).CloseDate.addDays(10); update items; } is approximately how I'd write it. It should work fine, as I've used code like this in the past.
    – sfdcfox
    Dec 4, 2014 at 7:30
  • There are number of products added to particular opportunity and each one has different ServiceDate . So I want to add the difference (service date - Opportunity.closeDate) to each of them so that every product service date is pushed to a new date with difference equal to the number of days between service date and close date.
    – JJoseph
    Dec 4, 2014 at 9:22
  • I have tried this: OpportunityLineItem[] olis = [SELECT Id, Name, ServiceDate, Opportunity.CloseDate FROM OpportunityLineItem WHERE OpportunityId IN :Trigger.New]; for(OpportunityLineItem oli : olis ){ Integer dat=oli.ServiceDate.day(); System.debug('dat prod '+ oli + dat); Integer dot=oli.Opportunity.CloseDate.day(); System.debug('opp cd '+ dot); Integer diff=dat-dot; System.debug('diff'+ diff); oli.ServiceDate = Trigger.newMap.get(oli.OpportunityId).CloseDate.addDays(diff);
    – JJoseph
    Dec 4, 2014 at 9:24

1 Answer 1


'__r' is appended only in case of custom relationships. These are standard objects so you can simply use

olis = [SELECT Id, Name, ServiceDate, Opportunity.CloseDate FROM OpportunityLineItem WHERE OpportunityId IN :Ids];


oli.ServiceDate = oli.Opportunity.CloseDate + (10)

  • btw, there is no Name field in OpportunityLineItem. You can remove that.
    – ntnng
    Dec 4, 2014 at 7:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .