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
Matt Webber 20Matt Webber 20 

Batch class retrieving sObject row but not querying requested field

Hi all,

I'm working on this class mainly for my own development. There's likely to be a simpler declarative solution but I'm trying to solve the problem with code.

My error message is this:
First error: SObject row was retrieved via SOQL without querying the requested field: SBQQ__Quote__c.SBQQ__Opportunity2__r

Here's my code:

public class UpdateQuotesWithInactiveSalesUser implements Database.Batchable<sObject> {
 
   
 
   public Database.QueryLocator start(Database.BatchableContext bc) {
 
 
 
       return Database.getQueryLocator('SELECT SBQQ__SalesRep__c, Id FROM SBQQ__Quote__c WHERE SBQQ__SalesRep__r.IsActive = FALSE AND (SBQQ__Quote__c.SBQQ__Opportunity2__r.StageName = \'Needs Analysis\' OR SBQQ__Quote__c.SBQQ__Opportunity2__r.StageName = \'Proposal\' OR SBQQ__Quote__c.SBQQ__Opportunity2__r.StageName = \'Proposal Sent\' OR SBQQ__Quote__c.SBQQ__Opportunity2__r.StageName = \'Negotiation\')');
   }
  
   public void execute(Database.BatchableContext BC, list<SBQQ__Quote__c> quotesWithInactiveSalesRep){
  
 
  
 
//only 50000 lines can be returned from the SOQL query
 
//Loop through those quotes and set SalesRep to match whoever is Opportunity Owner
 
for(SBQQ__Quote__c quote: quotesWithInactiveSalesRep){
    quote.SBQQ__SalesRep__c = quote.SBQQ__Opportunity2__r.OwnerId;
}
 
update quotesWithInactiveSalesRep;
 
   }
 
 
 
public void finish(Database.BatchableContext BC){
 
 
//you need to update the list not the variable or the class
//Run a DML update statement, now all quote records in the list will have sales rep updated to be the same as the //opportunity owner
 
}
 
//single update and single query so should avoid governor limits
 
//gov limit you're most likely to hit is the maximum CPU time - that can depend how much automation you have on //the quote object because that affects the calculation
 
}


What I'm trying to do:
I want to update all quotes where the user in the sales rep field on quote is inactive and update that field to be whoever is the owner of the related opportunity. But only if the opportunity can be worked so only if the related opp is at the stages named in the query (not closed won or closed lost).
Currently I'm just seeing that error message in the Apex Jobs and my test quote that meets the conditions isn't being updated (and still has the inactive user in the sales rep field).


Sorry for the long message, this is my first time using this site. Thanks very much in advance for any help I can get with this.

Best wishes,

Matt
Best Answer chosen by Matt Webber 20
AnkaiahAnkaiah (Salesforce Developers) 
Hi Matt,

you need to add the SBQQ__Opportunity2__r.OwnerId  field in the query.

Modify the retun query like below.
return Database.getQueryLocator('SELECT SBQQ__SalesRep__c, Id,SBQQ__Opportunity2__r.OwnerId FROM SBQQ__Quote__c WHERE SBQQ__SalesRep__r.IsActive = FALSE AND (SBQQ__Quote__c.SBQQ__Opportunity2__r.StageName = \'Needs Analysis\' OR SBQQ__Quote__c.SBQQ__Opportunity2__r.StageName = \'Proposal\' OR SBQQ__Quote__c.SBQQ__Opportunity2__r.StageName = \'Proposal Sent\' OR SBQQ__Quote__c.SBQQ__Opportunity2__r.StageName = \'Negotiation\')');
If this helps, Please mark it as best answer.

THanks!!


 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Matt,

you need to add the SBQQ__Opportunity2__r.OwnerId  field in the query.

Modify the retun query like below.
return Database.getQueryLocator('SELECT SBQQ__SalesRep__c, Id,SBQQ__Opportunity2__r.OwnerId FROM SBQQ__Quote__c WHERE SBQQ__SalesRep__r.IsActive = FALSE AND (SBQQ__Quote__c.SBQQ__Opportunity2__r.StageName = \'Needs Analysis\' OR SBQQ__Quote__c.SBQQ__Opportunity2__r.StageName = \'Proposal\' OR SBQQ__Quote__c.SBQQ__Opportunity2__r.StageName = \'Proposal Sent\' OR SBQQ__Quote__c.SBQQ__Opportunity2__r.StageName = \'Negotiation\')');
If this helps, Please mark it as best answer.

THanks!!


 
This was selected as the best answer
TechRemoteHubTechRemoteHub
Yes, looks like you missed the field in the query. Try out Ankaiah's code.
Matt Webber 20Matt Webber 20
@Ankaiah thank you so much! That's what it was. I'm new to development and am learning how to do batch classes. Thank you thank you thank you :) Have a great day. Thanks also @TechRemoteHub for reinforcing. My test quote has updated in our sandbox and I might actually be able to present this to my boss as a solution. Exciting.