I have the Sobjects Sales_Order__c (SO), Sales_Order_Line__c (SOL) , and Stock__c. I want to add the following condition in a batch class as if (Quantity_Ordered__c) >= Quantity__c the SO Ordered_Status__c should be "Closed".

Quantity_Ordered__c - it is in Sales_Order_Line__c (object) and Quantity__c is in Stock__c (object)

Here's what's been tried so far.

global class Batchupdate implements Database.Batchable<sObject> {
    global Integer recordsProcessed = 0;  
    global Database.QueryLocator start(Database.BatchableContext BC){
        String query= 'Select id, Date_Time_Ordered__c,Need_Date__c,Type__c, Order_Status__c from Sales_Order__c';
        return Database.getQueryLocator(query);
    global void execute(Database.BatchableContext bc, List<Sales_Order__c> scope){
        List<Sales_Order__c> so=[Select id, Date_Time_Ordered__c,Need_Date__c,Type__c ,Order_Status__c from Sales_Order__c];
        for(Sales_Order__c salesorder: scope){
            salesorder.Order_Status__c ='Closed';
            recordsProcessed = recordsProcessed + 1;
        update scope;
    global void finish(Database.BatchableContext bc){
  • 1
    Is there some identifier for a particular product found in Stock__c that can correlate that product to the SOL? Or is there a relationship between the SOL and the Stock object? Some more details about the object model, relationships and such would help you answer this, probably. – pchittum Apr 28 '20 at 9:36
  • Hi pchittum. Thanks for your response.I have the quantity and product field in Stock__c. and the quantity available field in SOL. And also there is no relationship between SOL and Stock object. – Aishwarya Apr 28 '20 at 9:42
  • 1
    How do you know which stock item to check if there is no relationship? Is there an ID or a lookup or anything? Without some sort of relationship or even structured naming conventions you are going to have a hard time. – Brandon Wind Apr 28 '20 at 14:45
  • Iam having an Item lookup in stock and sales order line. – Aishwarya Apr 29 '20 at 4:06
  • That sounds like a relationship. Unless I'm misunderstanding. This sounds more a question of Apex, SOQL and Data Model than anything related to batch specifically. – pchittum Apr 29 '20 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.