0

I have a batch apex class that deletes all the records from a custom object and it is deployed from sandbox to production. Now I have to write an other batch apex to delete all records from other custom object, Can I edit(reuse) the old batch apex class in sandbox and use it for other objects? will it affect the batch class that was deployed in production?

0

When you make an edit of any existing class in a sandbox and redeploy it to Production, the class in production will be overridden with your changes.

This may disturb your functionality. If your batch class is no longer needed in Production, Then you can modify it in the sandbox and re-deploy it to Production with new functionality.

If you don't want to deploy the code to production and just keep it to the sandbox, then you will not have any impact on your production instance.

0

Yes, you can do it.

Create a constructor in batch class where you pass the query you want to execute in the start method.

Then in the execute batch method pass the List<sObject> scope instead of the particular object.

On production, however, you should rerun the batch with new constructor.

To summarize, the batch will look somehow like this:

public with sharing class RunFlowBatch
        implements Database.Batchable<sObject> {
    public String query;
    public RunFlowBatch(String query) {
        this.query = query;
    }

    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(this.query);
    }

    public void execute(Database.BatchableContext bc, List<sObject> scope) {
        //do necessary operations
    }

    public void finish(Database.BatchableContext bc) {
        //do necessary operations
    }
}

  • So, once the code is deployed in production, changes made in sandbox code will not be affected right ? @Mariia IIIarionova – Salesforce User Jul 12 at 9:14
  • If you won't refresh your sandbox or deploy from sandbox to prod - than yes, these are two separated environments. @SalesforceUser – Mariia Illarionova Jul 12 at 9:30
  • Thank you @Maria lllarionova – Salesforce User Jul 12 at 9:34

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.