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 

Lookup and update object in Apex Class

I have an Apex Class written by my in-house non-Salesforce developer. I tried to modify it and am now getting an internal server error. Here's the part of the code that works (there's a lot more, I'm just not showing it here).
private static string processSale(Quote aQuote) {
               
       if (aQuote.Opportunity.Account.Member_ID__c == null || aQuote.Opportunity.Account.Member_ID__c == '') {
          aQuote.Opportunity.Account.Member_ID__c = generateMemberID(aQuote.Opportunity.Account.Name);
       }      
        
       //New clients - Build the Contract
        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 = 'Draft'
                ,Number_of_Quotes__c = 1
            	);
            insert aContract; 
            
            aContract.Status = 'Activated';
            update aContract;

            aQuote.ContractId = aContract.Id;
            
            aQuote.Opportunity.ContractId = aContract.Id;
            
        }
}
The part that isn't working is this:
//Existing clients - Append to the Contract
        if (aQuote.ContractId != null && aQuote.Opportunity.Type != 'New Business') {
          /*  List<Contract> updateContract = New List<Contract>();
            For(Contract con : [Select id, Number_of_Quotes__c , Addendums__c FROM Contract WHERE Id = :aQuote.ContractId]){
                con.Addendums__c = aQuote.Addendum__c;
                con.Number_of_Quotes__c = con.Number_of_Quotes__c + 1;
            }
				
			update updateContract;   
            For (Contract con : [Select id, Number_of_Quotes__c , Addendums__c FROM Contract WHERE Id = :aQuote.ContractId] ){
                con.Number_Of_Quotes__c = con.Number_of_Quotes__c + 1;
                con.Addendums__c = aQuote.Addendum__c;
            }
            
            update con;*/
            
            //This throws an internal server error
                Contract updateContract = [SELECT id, Number_of_Quotes__c , Addendums__c FROM Contract Where Id = :aQuote.ContractId LIMIT 1];
            
                updateContract.Number_of_Quotes__c = updateContract.Number_of_Quotes__c + 1;
                updateContract.Addendums__c = aQuote.Addendum__c;
            update updateContract;
            
                
       }

Essentially, this is a combination of everything I've tried and it didn't work. the first List/For bit threw an internatl server error. The second For bit didn't actually do anything (but didn't throw an error - it didn't update the contract though). The bit under the "//This throws an internal server error" obviously didn't work either. 

I'm a coding novice - pretty solid admin, but code is still new to me so I'm self-teaching as I go. Any and all help is appreciated.

As a note, the ContractId for aQuote is being grabbed at the bottom of the Apex Class in a getQuoteSOFor call (call? statement? method?)
 
Ashish KumarAshish Kumar
Hi Carissa,

Could you please tell "aQuote" is an object/instance of which Salesforce Object. Basically want to know type of "aQuote".
Secondly Could you please remove dml operation once and check again if the issue is persisting? These information could help in finding the root cause of the issue.

Regards,
Ashish Kr.
Carissa CrittendenCarissa Crittenden
Here's the code that pulls aQuote:
private static Quote getQuoteSOFor(String quoteId) {
             Quote aQuote = [select id
                   ,Status 
                   ,Opportunity.StageName
                   ,Opportunity.Account.Type
                   ,Opportunity.Account.Member_ID__c
                   ,Opportunity.Account.Name
                   ,Opportunity.Account.BillingStreet
                   ,Opportunity.Account.BillingCity
                   ,Opportunity.Account.BillingState
                   ,Opportunity.Account.BillingPostalCode
                   ,Opportunity.Type          
                   ,CreatedBy.id
                   ,Due_Now__c 
                   ,Email
                   ,Phone
                   ,BillingName
                   ,BillingStreet
                   ,BillingCity
                   ,BillingPostalCode
                   ,BillingState
                   ,Name
                   ,Opportunity.Owner.Name
                   ,Opportunity.Owner.Email 
                   ,Opportunity.Owner.Phone 
                   ,Opportunity.Account.Association__c
                   ,Opportunity.Account.Association__r.Name                   
                   ,Opportunity.Solutions_Consultant_Used__c 
                   ,Opportunity.ContractId          
                   ,Software_Contract_Terms__c
                   ,Additional_Terms__c
                   ,TotalPrice
                   ,QuoteNumber
                   ,CreatedDate
                   ,ExpirationDate
                   ,Contact.Name
                   ,Contact.FirstName
                   ,Contact.LastName
                   ,Contact.Phone
                   ,Contact.Email
                   ,tax_percent__c
                   ,Yr_1_net_amt__c
                   ,Yr_2_net_amt__c
                   ,Yr_3_net_amt__c                   
                   ,Yr_1_sum__c
                   ,Yr_2_sum__c
                   ,Yr_3_sum__c                   
                   ,Yr_1_disc_sum__c
                   ,Yr_2_disc_sum__c
                   ,Yr_3_disc_sum__c
                   ,Yr_1_tax_amt__c
                   ,Yr_2_tax_amt__c
                   ,Yr_3_tax_amt__c
                   ,Description
                   ,disc_total_amount_sum_c__c                   
                   ,tax_total_amount_sum__c   
                   ,grand_total__c 
                   ,Signed_MSSA__c
                   ,ContractId
                   ,Initial_Contract_Term_Length__c
                   ,Initial_Contract_Start_Date__c
                             
               from Quote where id = :quoteID];
               
        return aQuote;
    }

I have tried commenting out the entire Existing Clients section and the code worked flawlessly. It's somewhere in there that the error is being thrown.
Ashish KumarAshish Kumar
Hi Carissa, They issue might be because of heap size error. Please fetch only the required fields from quote object, but as you are just fetching a single record so issue might be something else too.. In that case could you please comment only the line where you had your DML Operation ( update statement ) rather commenting the whole section. Please check once and let me know. Regards, Ashish Kr. Get Outlook for iOS
Carissa CrittendenCarissa Crittenden
Believe it or not, each of those fields is being used somewhere in the 578 lines of code of this Apex Class. 

Are you referring to the statement that updates aQuote or the Contract record? Or are you referring to commenting it out of my Apex Class? Sorry, I'm a very very inexperienced developer - much better with clicks than code.
Ashish KumarAshish Kumar
I am referring to this line update updateContract; Please comment the above line once and it will be helpful if you can check and see what's coming in the logs. Regards Get Outlook for iOS