function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Carissa CrittendenCarissa Crittenden 

variable does not exist - Update Lookup Field value with newly inserted object Id

Hi,

I'm brand new to apex. I'm trying to update an already existing apex class with some code that will, based on field values, create a new record. I then want to update the Quote and Opportunity to tie back to that newly created record and I'm getting the error for "Variable does not exist" for everything I'm trying.

Here's a snippet of the code I'm using. It does more than just this, but the errors are being thrown at the aQuote.ContractId & aQuote.Opportunity.ContractId
 
private static string processSale(Quote aQuote) {
               
               
       
        if (aQuote.IsPaid__c && (aQuote.Opportunity.Account.Has_a_signed_MSSA__c == 'No' || aQuote.Opportunity.Account.Has_a_signed_MSSA__c == null	) && aQuote.Opportunity.Account.Type != 'Current Customer') {
            Contract aContract = new Contract (
                AccountId = aQuote.Opportunity.Account.id
                ,ContractTerm = aQuote.Initial_Contract_Term_Length__c.intValue()
                ,StartDate = aQuote.Initial_Contract_Start_Date__c
            	);
            
            insert aContract;  
            
                      
            aQuote.ContractId = aContract.Id;
            aQuote.Opportunity.ContractId = aContract.Id;
                        
            update aQuote;
            update aQuote.Opportunity;
        }
}

What do I need to do to ensure the Opp & Quote records get tied back to the Contract?

Thanks!
Best Answer chosen by Carissa Crittenden
Carissa CrittendenCarissa Crittenden
Just to close the loop, once I put my code in to the correct Apex Class, it worked the way it was supposed to. Thanks all!

All Answers

Alain CabonAlain Cabon
No such column 'ContractId' on entity 'Opportunity'
By default Contract field on Opportunity is hidden from all profiles. 
In order to make it visible follow the steps
  1. Go to Setup->Customize->Opportunity->Fields
  2. Click on Contract 
  3. Click on "Set Field Level Security" button

https://help.salesforce.com/articleView?id=000213295&type=1
Carissa CrittendenCarissa Crittenden
There were actually a number of things wrong with my code. Field Access wasn't a problem - I had access to the field, but my Apex Class was running API version 29, rather than the current 41. The ContractId field wasn't available in API until around API version 33. That solved one problem. Additionally, the lookup fields can't be updated without separate queries for each object. My updated code is below. This saved without errors, though I'm still working on actually making it work in execution.
if (aQuote.Signed_MSSA__c == false && aQuote.Opportunity.Type == 'New Business') {
            Contract aContract = new Contract (
                AccountId = aQuote.Opportunity.Account.id
                ,ContractTerm = aQuote.Initial_Contract_Term_Length__c.intValue()
                ,StartDate = aQuote.Initial_Contract_Start_Date__c
                ,Status = 'Activated'
            	);
            
            insert aContract;  
            
       		LIST <Quote> bQuote = [Select Id, ContractId FROM Quote Where Id=:aQuote.Id ];
         		for(Quote quote : bQuote){
                quote.ContractId = aContract.Id;
           		}
				
				update bQuote;
       
            
            LIST <Opportunity> quoteOpp = [Select Id, ContractId FROM Opportunity Where Id=:aQuote.Opportunity.Id];
				for(Opportunity opp : quoteOpp){
					opp.ContractId = aContract.Id;
					}
				update quoteOpp;
        }

 
Abhishek BansalAbhishek Bansal
Hi Carissa,

Can you please let us know what error you are facing during the execution of this code. It looks to fine. Is there any error ?

Thanks,
Abhishek Bansal.
Carissa CrittendenCarissa Crittenden
It isn't giving any errors - it just isn't doing what it's supposed to. I've tried changing my if statement in line 1, but it still isn't building the new Contract record.
Abhishek BansalAbhishek Bansal
Hi Carissa,

If it not inserting a new Contract record then it is clear that the code is not executed after If statement. There must be some variable that are not set correctly and being used in the If statement. You should check them properly and then try to execute your code. If there is any urgency on this then you can contact me on Gmail or Skype:
Gmail: abhibansal2790@gmail.com
Skype: abhishek.bansal2790

Thanks,
Abhishek Bansal.
Carissa CrittendenCarissa Crittenden
I commented out the if statement and the code still didn't fire. That led to the realization that this apex class isn't being called - a different one is. I am working on tracking down that. I will certainly come back if I need more help. Thank you!
Carissa CrittendenCarissa Crittenden
Just to close the loop, once I put my code in to the correct Apex Class, it worked the way it was supposed to. Thanks all!
This was selected as the best answer