1

So, thanks to @BarCotter I have managed to get the first half of my problem sorted. I now have an Apex trigger to create a record in a custom object upon creation of an opportunity.

What I need to do now is create another trigger to update this newly created record from the Contract creation process. The Contract has a lookup field to Opportunity so there is a link between them and this data is also held on the customer record. Here's what I have so far:

trigger ApplyContractNumbertoJunction on Contract (after insert, after update) {
    Set<Id> Opportunity_list = new Set<Id>();

    for (Opportunities_and_Contracts__c o : Trigger.new) {
        Opportunity_list.add(Opportunity__c);
    }

    List<Opportunities_and_Contracts__c> OppCon = [SELECT Id, Opportunity__c
                              FROM Opportunities_and_Contracts__c
                              WHERE Opportunity__c IN :Opportunity_list];
        update OppCon;
    }

The error I'm getting is similar to the previous one:

Error: Compile Error: Variable does not exist: Opportunity__c at line 5 column 30   

I've already confirmed all the field names are correct but I'm very new to this so understanding all the statements is still a bit of a struggle.

Would anybody be so kind as to slap me with some perspective?

  • You can't have a trigger that fires on Contracts which then looks at an Opportunities_and_Contracts object's trigger.new. It needs to fire on the same object as you want it to look at the trigger.new of. That's why it can't find the field. – crmprogdev Aug 8 '14 at 15:14
  • So let me see if I understand this correctly, You want to reference the Contract on the custom object as well and using the opportunity number to find the record on the custom object and then adding the reference of the Contract to the custom object? – AlphaBravo Aug 8 '14 at 15:14
  • Pretty much, yes. The Contract holds the Opportunity name. I want the trigger to use the Opportunity name to find the approproate custom object record and then apply the Contract Number to this custom object record. – CaptainHaZ Aug 12 '14 at 8:33
  • Apologies for the delay too – CaptainHaZ Aug 12 '14 at 8:40
2

The reason you had that error is that you where referencing a field Opportunity__c but did not reference the object to which it belonged c. You have to specify fields as {object}.{field}

This is how it should be written. Now you will have to rewrite it to properly perform your business logic as what you had for the loop object did not match the context:

trigger ApplyContractNumbertoJunction on Contract (after insert, after update) {
    Set<Id> Opportunity_list = new Set<Id>();

    for (Contract c : Trigger.new) {
        Opportunity_list.add(c.Opportunity__c);
    }

    List<Opportunities_and_Contracts__c> OppCon = [SELECT Id, Opportunity__c
                              FROM Opportunities_and_Contracts__c
                              WHERE Opportunity__c IN :Opportunity_list];
        update OppCon;
    }

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.