0
global class BatchForLatestCaseOHVRDatesAccountField implements Database.Batchable<sObject> {

      // Start Method

    global Database.QueryLocator start(Database.BatchableContext BC){ 

        return Database.getQueryLocator( [Select ID from Account where ID IN ( '0018000000YFjri','0018000000UoAuO') ]) ;
    }

    global static void updateAccountDetails(List<Account> lstAccs){

        string accountIds;
        accountIds = (String)lstAccs.get(0).ID;

        Case cs = New case();
        cs = [ select ID,AccountId,createddate from Case where accountid =: accountIds order by createddate desc limit 1 ];
        Order_History__c orh = New Order_History__c(); 
        orh = [ select ID,Account__c ,createddate from Order_History__c where Account__c  = : accountIds order by createddate desc limit 1 ];
        Visit_Report__c vr = New  Visit_Report__c(); 
        vr= [ select ID,Account_ID__c ,createddate from Visit_Report__c where Related_Account__c   = :accountIds order by createddate desc limit 1 ];
        List<Account> lstacc = [Select ID, Latest_Case_Created_Date__c,Latest_OrderHistory_Created_Date__c,Latest_VisitReport_Created_Date__c from Account where ID = :accountIds ];

        // Case Update

        for(Account acc : lstacc){

                    if(cs != NULL)
                    acc.Latest_Case_Created_Date__c  = cs.createddate;
                    if(orh != NULL)
                    acc.Latest_OrderHistory_Created_Date__c  = orh.createddate;
                    if(vr != NULL)
                    acc.Latest_VisitReport_Created_Date__c  = vr.createddate;

        }
        Update lstacc;

        List<Contact> con = [SELECT ID,Name FROM CONTACT where AccountID = :accountIds];
        UPDATE con;   


    }
    // Execute Logic
    global void execute(Database.BatchableContext BC, List<SObject> scope){

        updateAccountDetails(scope);

    }


    global void finish(Database.BatchableContext BC){
        // Logic to be Executed at finish
    }


}
  • please see How to Ask --specifically, which query? – cropredy Jul 26 at 16:44
  • 2
    Don't use hard-coded ids. Don't use global access modifier. – Adrian Larson Jul 26 at 17:03
0

Clearly one of the query is returning null result. Instead use a list of sObject for your queries and take the 0th element to avoid such errors.

  • Acctually I need to update all existing records – Sharathchowdary Y Jul 28 at 6:29
0

check the list size() before doing for loop and DML operations like below. your issue with either list<account> / list<contact>. and you should create list instance first.

List<Account> lstacc = new List<Account>();
lstacc  = [Select ID, Latest_Case_Created_Date__c,Latest_OrderHistory_Created_Date__c,Latest_VisitReport_Created_Date__c from Account where ID = :accountIds ];
if(lstacc.size() > 0){
 for(Account acc : lstacc){

                    if(cs != NULL)
                    acc.Latest_Case_Created_Date__c  = cs.createddate;
                    if(orh != NULL)
                    acc.Latest_OrderHistory_Created_Date__c  = orh.createddate;
                    if(vr != NULL)
                    acc.Latest_VisitReport_Created_Date__c  = vr.createddate;

        }

        Update lstacc;
}


   List<Contact> conlist = new List<Contact>();
   conlist = [SELECT ID,Name FROM CONTACT where AccountID = :accountIds];
  if(conlist.size() > 0){
     UPDATE con;
   }

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.