-1

I have created a trigger on my custom object Policy__c. I want to update fields on Opportunity whenever certain fields are updated on Policy__c.

Opportunity has a lookup to Policy__c called Source_Policy__c.

The set up is as follows:

Opportunity 
* Source_Policy__c (Lookup to Policy__c)
* Inception_Date__c (Date)

Policy__c
* Start_Date__c (Date)

So, whenever Start_Date__c is updated on Policy__c, I want to update Inception_Date__c with the same value.

I have written the following trigger, but the error message I get is: Variable doesn't exist: Start_Date__c.

trigger updateRenewals on Policy__c(before insert, before update) { 
    List<ID> oppIDs = new List<ID>();

    for (Policy__c pol: trigger.new){
        oppIDs.add(pol.Opportunity);
    }

    //restrict to renewal opportunities
    List<Opportunity> opps = new List<Opportunity>([SELECT ID, Inception_Date__c FROM Opportunity WHERE IS in: oppIDs]);
    System.debug('Opportunities ' + opps);

        for(integer i = 0; i < oppIDs.size();i++){
            if(pol.Opportunity == opps[i].id){
                opps[i].Inception_Date__c = Start_Date__c;
            }
        }
    update opps;
}

Can anyone see what's wrong? Tia.

  • This isn't in scope if(pol.Opportunity == opps[i].id)` – EricSSH Dec 10 '14 at 20:03
1

Based on your description of the problem I would of changed the code to what is shown below. The description of the problem does not mention an Opportunity lookup on Policy so pol.Opportunity is not a valid field

trigger updateRenewals on Policy__c(after insert) { // Changed to after insert so that Policy Id is populated
    List<Id> policyIds = new List<Id>();
    for (Policy__c pol: trigger.new) {
        policyIds.add(pol.Id); // You should only add these Ids when Start_Date__c changes
    }

    //restrict to renewal opportunities
    List<Opportunity> opps = [SELECT Id, Inception_Date__c, Source_Policy__r.Start_Date__c
                                                FROM Opportunity
                                                WHERE Source_Policy__c in :policyIds];

     for (Opportunity opp : opps) {
         opp.Inception_Date__c = opp.Source_Policy__r.Start_Date__c;
     }
     update opps;
}

You could also achieve the same results by adding a Date formula field on Opportunity with the formula below and the date will be correctly displayed all the time

Source_Policy__r.Start_Date__c
| improve this answer | |
0

The reason you are getting the error is you are not telling it which object the Start_Date__c field is on, so it assumes its a variable and its not finding a variable instantiation.

Try changing this

opps[i].Inception_Date__c = Start_Date__c;

to this

opps[i].Inception_Date__c = opps[i].Start_Date__c;
| improve this answer | |
  • Start_Date__c is a field on Policy not Opportunity. – BarCotter Dec 10 '14 at 20:18
0

You have several issues with your trigger here.

for(integer i = 0; i < oppIDs.size();i++){
            if(**pol**.Opportunity == opps[i].id){//pol does not exist here
                opps[i].Inception_Date__c = **Start_Date__c**;//Start_Date__c is not a variable.  You likely want to use pol.Start_Date__c or opps[i].Start_Date__c
            }
        }

In addition to those 2 compile errors, you should put the Opportunities in a Map so that you can loop over your Policies again and grab the specifically related Opportunity. As it is, you have some basic errors you need to fix before moving on.

| improve this answer | |
0

You can probably rewrite this line:

 List<Opportunity> opps = new List<Opportunity>([SELECT ID, Inception_Date__c FROM Opportunity WHERE IS in: oppIDs]);

to

 List<Opportunity> opps = [SELECT ID, Inception_Date__c FROM Opportunity WHERE IS in: oppIDs];

Also your in your if statement

if(pol.Opportunity == opps[i].id){
            opps[i].Inception_Date__c = Start_Date__c;
        }

I wouldn't think you'd be able to call pol.Opporunity. That line should be giving be giving you an error since it's not actually in scope.

Start date is a custom field so you can't just call it like that, you have the follow the format of object__c.custom_field__c; .. So in your case it would be something like:

Policy__c.Start_Date__c;
| improve this answer | |
0

I think another alternative could be to find each matching opportunity, append to an opportunity objects' list, and update them at the end of the trigger's loop. Or, did I miss something?

trigger updateRenewals on Policy__c (after update) { // only when a policy is updated
   List<Opportunity> oppslist = new List<Opportunity>();
   for (Policy__c pol : trigger.new) {
       <Opportunity> opp = [SELECT ID, Inception_Date__c FROM Opportunity WHERE ID = :pol.ID];
       opp.Inception_Date__c = :pol.Start_Date__c;
       oppslist.add(opp);
   }
   update opps;
}
| improve this answer | |

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.