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
PrakkiPrakki 

Unable to update records using batch Apex

Here i am trying to append "Updated"  to all of my accounts but no luck. 


public class INFSimpleBatchExp implements Database.Batchable<sobject> {
  
    public Database.QueryLocator start(database.BatchableContext bc )
    {
        String query = 'select id, name from Account';
        return Database.getQueryLocator(query);
    }
   
    public void execute (Database.BatchableContext bc, List<Account> scope )
    {
      
        for(Account acc : scope)
        { 
          
            acc.name = acc.name + 'updated';
        }
    update scope;
    }
   
   
    public void finish(Database.BatchableContext bc)
    {
       
    }
}
----------------------------------------------------------------------------------------------------------------------

I am using following statemets in Developer console

INFSimpleBatchExp inF = new INFSimpleBatchExp();
Database.executeBatch(inF, 200);

Anyone kindly let me know where it went wrong?

wwwwwwThanks,
Prakki 

Best Answer chosen by Prakki
kiranmutturukiranmutturu
Also first check the apex jobs under monitoring and debug logs whether you are getting erros in this or not?

All Answers

Sfdc CloudSfdc Cloud
Hi 
Batch class should be global instead of public.You need to change class and method to global
global class INFSimpleBatchExp implements Database.Batchable<sobject> {
  
    global Database.QueryLocator start(database.BatchableContext bc )
    {
        String query = 'select id, name from Account';
        return Database.getQueryLocator(query);
    }
   
    global void execute (Database.BatchableContext bc, List<Account> scope )
    {
      
        for(Account acc : scope)
        { 
          
            acc.name = acc.name + 'updated';
        }
    update scope;
    }
   
   
    global void finish(Database.BatchableContext bc)
    {
       
    }
}


Anonymous Block Code
INFSimpleBatchExp inF = new INFSimpleBatchExp();
Database.executeBatch(inF, 200);

This code is working fine in my org

If this answer helps you,please mark it as best answer to help others :)
Thanks
kiranmutturukiranmutturu
 change the access modifier to global and try
PrakkiPrakki
I changed the access modifier from public to global, still not working.

------------------------------------------------------------------------------------------------------------

global class INFSimpleBatchExp implements Database.Batchable<sobject> {
  
    global Database.QueryLocator start(database.BatchableContext bc )
    {
        String query = 'select id, name from Account';
        return Database.getQueryLocator(query);
    }
   
    global void execute (Database.BatchableContext bc, List<Account> scope )
    {
      
        for(Account a : scope)
        { 
          
            a.name = a.name +'updated';
        }
    update scope;
    }
   
   
    global void finish(Database.BatchableContext bc)
    {
       
    }



kiranmutturukiranmutturu
try like this and see

Id batjobId = Database.execute(new INFSimpleBatchExp(), 200);
kiranmutturukiranmutturu
Also first check the apex jobs under monitoring and debug logs whether you are getting erros in this or not?
This was selected as the best answer
Sfdc CloudSfdc Cloud
Hi,

Could you check debug log what error you are getting So i can help you out. this code is working fine in my org.
PrakkiPrakki
Thanks @kiran_Mutturu, @sfdc cloud, i figured out the actual problem, nothing wrong in the code  due to a validation rule on the account object i was unable to update Account records. 

Regards,
Prakash

   
Arpit Gupta 37Arpit Gupta 37
Hi I am not getting email to mail to my personal email Id on completion of the Apex Job,Kindly suggest

global class INFSimpleBatchExp implements Database.Batchable<sobject> {
   

    global Database.QueryLocator start(database.BatchableContext bc )

    {

        String query = 'select id, Primary_Email_amgn__c from Account';

        return Database.getQueryLocator(query);

    }

    

    global void execute (Database.BatchableContext bc, List<Account> scope )

    {

       

        for(Account acc : scope)

        {

           

           acc.Primary_Email_amgn__c='';

        }

    update scope;

    }

    

    

    global void finish(Database.BatchableContext bc)

    {
AsyncApexJob a = [Select Id, Status,ExtendedStatus,NumberOfErrors, JobItemsProcessed,TotalJobItems, CreatedBy.Email    from AsyncApexJob where Id =:BC.getJobId()];

       

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

        String[] toAddresses = new String[] {'arpitguptacom1310@gmail.com'};

        mail.setToAddresses(toAddresses);

        mail.setSubject('Match Merge Batch ' + a.Status);

        mail.setPlainTextBody('records processed ' + a.TotalJobItems +   'with '+ a.NumberOfErrors + ' failures.');

        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

        

    }

}
Vivek Thiru 6Vivek Thiru 6

Getting below error :

SUCCESS: (
        {
        errorCode = "NOT_FOUND";
        message = "The requested resource does not exist";
    }
)

While upating the firstName in my salesforce


        let postBody = ["FirstName": "Express Logistics"]
        let data: Data? = try? JSONSerialization.data(withJSONObject: postBody, options: [])
        var urlRequest = NSMutableURLRequest()
        if let aString = URL(string: "instance_url/services/data/v20.0/sobjects/Lead/Account") {
            urlRequest = NSMutableURLRequest(url: aString)
        }
        //create the Method "POST"
        urlRequest.httpMethod = "PATCH"
        urlRequest.setValue("application/json;charset=UTF-8", forHTTPHeaderField: "content-type")
        urlRequest.setValue("Bearer access_token", forHTTPHeaderField: "Authorization")
        urlRequest.httpBody = data

DB URL given as :- "instance_url/services/data/v20.0/sobjects/Lead/Account" 

working in  ios swift

Thanks.