Skip to main content

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

 

@Salesforce CPQ 

@* Salesforce Revenue Cloud * 

2 respuestas
  1. 31 oct 2022, 14:08

    Hi Kermit, this has actually been solved very helpfully by Ankaiah. My code now works and updates my test quote. 

    The problem was the omission of the field SBQQ__Opportunity2__r.OwnerId from the return query as Ankaiah pointed out.

    Thanks for getting involved though 😃

0/9000