3

Following from my previous question I'm still trying to link Opportunities and Cotnracts together and report on them.

Firstly, I now have a lookup relationship between Opportunities and Contracts so that my users can now create a contract straight from their opportunities.

I've gone down the road of creating a custom object to serve as a data junction. This object contains the Master-Detail fields Opportunity__c (and Opportunity Name lookup field) and Contract__c (a Contract Number lookup field).

I had the thought of creating some Apex triggers to populate these fields upon creation of an Opportunity and Contract. My first attempt, populating the Opportunity__c field with the Opportunity.Name field is thus:

trigger ApplyOppNameToJunction on Opportunity (before insert) {    
      Opportunities_and_Contracts__c bb = Opportunity__c;
      for (Opportunity aa : trigger.new){
          aa.Opportunity.Name = bb.Opportunity__c;
      }  
}

but this won't save. It tells me

Error: Compile Error: Variable does not exist: Opportunity_c at line 2 column 40

even though that field does exist within the Opportunities_and_Contracts__c object. As this is only stage one and I'm aleady htiting stumbling blocks, can anybody offer any advice?

I do have more queries too:

Once this si sorted, I can set something similar to populate the Contract__c field. My hope is that this would then happen:

User creates Opportunity - Opportunity name automaitcally populated in junction record. User creates Contract from Opportunity - Contract number automatically populated into related junction record.

Can anyone point out if I need anything else to get this to work?

9
  • Your error suggests you have a typo in the code, although its not shown above. Its saying that you have Opportunity_c (only 1 underscore), where it should be Opportunity__c (2 underscores) Commented Aug 8, 2014 at 12:07
  • I retyped the field name and got this instead Error: Compile Error: Variable does not exist: Opportunity__c at line 2 column 40
    – CaptainHaZ
    Commented Aug 8, 2014 at 12:20
  • Can you go to the custom lookup field on the Opportunities_and_Contracts__c object and confirm the name of the lookup field is the default Opportunity__c, and you didnt name it something different. Commented Aug 8, 2014 at 12:22
  • Definitely called Opportunity__c
    – CaptainHaZ
    Commented Aug 8, 2014 at 12:37
  • 1
    Can you just tell us (not in code) what you ant this trigger to accomplish. Might help as I am unclear as to what the end result you are looking for. Commented Aug 8, 2014 at 12:47

1 Answer 1

3

The line number in your error is line 2, which is the line shown below:

Opportunities_and_Contracts__c bb = Opportunity__c;

If you are trying to create a new Opportunities_and_Contracts__c record every time an Opportunity is created then the code below will work. Note that I have changed the trigger to after insert so that the Opportunity Id is set and available to use.

trigger ApplyOppNameToJunction on Opportunity (after insert) {    
      List<Opportunities_and_Contracts__c> ocs = new List<Opportunities_and_Contracts__c>();
      for (Opportunity aa : trigger.new){
          ocs.add(new Opportunities_and_Contracts__c(Opportunity__c = aa.Id));
      }
      insert ocs;
}
14
  • Ok, this may sound a bit simple but what is defined as a Known Record?
    – CaptainHaZ
    Commented Aug 8, 2014 at 12:47
  • I think there may be an issue as the Opportunity and Contracts record does not exist at the point in time when an Opportunity is created.
    – CaptainHaZ
    Commented Aug 8, 2014 at 12:49
  • 1
    No worries @BarCotter, you can have it. Just wanted to make sure it was clear what he was trying to accomplish. Commented Aug 8, 2014 at 12:54
  • 1
    I changed the Contract__c field type to a LookUp and it allowed it. I'm going to work on the trigger to apply the contract details when a contract is created. Thanks for your help.
    – CaptainHaZ
    Commented Aug 8, 2014 at 13:24
  • 1
    @CaptainHaz, glad that you were able to get it working. In addition to accepting Bar's answer, you should upvote it as well. :) Commented Aug 8, 2014 at 13:48

You must log in to answer this question.

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