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
Glen.ax1034Glen.ax1034 

Error: Invalid Data.

Apex trigger ContractCompliance caused an unexpected exception, contact your administrator: ContractCompliance: execution of BeforeUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contract.Status: Trigger.ContractCompliance: line 6, column 1

Error: Invalid Data. 
Review all error messages below to correct your data.

 

trigger ContractCompliance on Opportunity (before update) {
	
	for (Opportunity opp:Trigger.new){
		List<Contract> c = new List<Contract>([SELECT id from Contract where AccountId = :opp.accountid]);
		for(Contract cTypeObj :  c) {
			if (cTypeObj.Status == 'Activated Signed') {
				if (cTypeObj.Contract_ID_Prefix__c == 'MSA') {
					opp.ContractStructure__c = 'Yes';
					opp.MSA_or_Standalone_Effective_Date__c = cTypeObj.StartDate;
					
				}
			}
		}		
		
		List<Account> a = new List<Account>([SELECT id from Account where Id = :opp.accountid]);
		for(Account aTypeObj :  a) {
			opp.Legal_Account_Name__c = aTypeObj.Hoovers_Company_Name__c;
			opp.Customer_Contract_Street_Address_1__c = aTypeObj.Hoover_s_HQ_Address_1__c;
			opp.Customer_Contract_Street_Address_2__c = aTypeObj.Hoover_s_HQ_Address_2__c;
			opp.Customer_Notices_City__c = aTypeObj.Hoover_s_HQ_City__c;
			opp.Customer_Notices_Zip__c = aTypeObj.Hoover_s_HQ_Postal_Code__c;
		}
	}
}

 

The purpose of this trigger is to pull values of fields to the sObject Opportunity from the related Contract(s)and Account(s)

Best Answer chosen by Admin (Salesforce Developers) 
kritinkritin

Please Add Status column in your SOQL query,

 

List<Contract> c = new List<Contract>([SELECT id, Status from Contract where AccountId = :opp.accountid]);

 

 

 

And also make add all the missing column that you are using ahead this line.





All Answers

kritinkritin

Please Add Status column in your SOQL query,

 

List<Contract> c = new List<Contract>([SELECT id, Status from Contract where AccountId = :opp.accountid]);

 

 

 

And also make add all the missing column that you are using ahead this line.





This was selected as the best answer
Glen.ax1034Glen.ax1034
trigger ContractCompliance on Opportunity (before update) {
	
	for (Opportunity opp:Trigger.new){
		List<Contract> c = new List<Contract>([SELECT id, Status, Contract_ID_Prefix__c, StartDate from Contract where AccountId = :opp.accountid]);
		for(Contract cTypeObj :  c) {
			if (cTypeObj.Status == 'Activated Signed') {
				if (cTypeObj.Contract_ID_Prefix__c == 'MSA') {
					opp.ContractStructure__c = 'Yes';
					opp.MSA_or_Standalone_Effective_Date__c = cTypeObj.StartDate;
					
				}
			}
		}		
		
		List<Account> a = new List<Account>([SELECT id, Hoovers_Company_Name__c, Hoover_s_HQ_Address_1__c, Hoover_s_HQ_Address_2__c, Hoover_s_HQ_City__c, Hoover_s_HQ_Postal_Code__c from Account where Id = :opp.accountid]);
		for(Account aTypeObj :  a) {
			opp.Legal_Account_Name__c = aTypeObj.Hoovers_Company_Name__c;
			opp.Customer_Contract_Street_Address_1__c = aTypeObj.Hoover_s_HQ_Address_1__c;
			opp.Customer_Contract_Street_Address_2__c = aTypeObj.Hoover_s_HQ_Address_2__c;
			opp.Customer_Notices_City__c = aTypeObj.Hoover_s_HQ_City__c;
			opp.Customer_Notices_Zip__c = aTypeObj.Hoover_s_HQ_Postal_Code__c;
		}
	}
}

 Solved.. at least i hope so.

Glen.ax1034Glen.ax1034

do you think that this will error if those fields are blank in account and contract?

 

they shouldnt... because i ran it with some blank ones.. but i'm kind of new to apex.. and i'm thinking that perhaps this might be a potential error.

 

another question... how would i test this code? create an opportunity, account and contract and set the contract accountid to the account.id and the opportunity.accountid to the account.id? then upsert the account, then the contract, then the opportunity?

Glen.ax1034Glen.ax1034

test class success

 

/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class testContractCompliance {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        
            
        User u1 = [select id from User where alias='gbrad'];
        
//Run As U1  
    
        System.RunAs(u1){
        	
        Account acct = new Account(name='test account');
		insert acct;
        
        Contract contract = new Contract (accountid=acct.id);
        insert contract;
        
        Opportunity opp = new Opportunity (accountid=acct.id);
        opp.Name = 'TEST Co-BI/SAT/ADR';
        opp.StageNAme = 'what';
        opp.closedate = System.today(); 
        opp.StartDate__c = System.today();
        insert opp;
        	
        }
    }
}