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
Shaikh RayyanShaikh Rayyan 

I want to copy all accounts with rating as warm to custom warm accounts object without duplication.

Global class copyWarmAccounts implements Database.Batchable<Sobject>
{
    Global Database.QueryLocator start(Database.BatchableContext bContext)
    {
        string query = 'Select name, id, rating from account';
        return database.getQueryLocator(query);
    }
    Global void Execute(Database.BatchableContext bContext, list<Account> batch)
    {
        list<warm_accounts__c> lstwac = new list<warm_accounts__c>();
        for(Account acc : batch)
        {
                  if(acc.rating == 'warm')
                {
                           Warm_accounts__c wac = new Warm_accounts__c();
                        wac.name = acc.name;
                        wac.Rating__c = acc.Rating;
                        wac.Id = acc.Id;
                        lstwac.add(wac);
                 }
         }
        insert lstwac;
    }
    Global void finish(Database.BatchableContext bContext)
    {
        //do nothing
    }
}
Best Answer chosen by Shaikh Rayyan
mukesh guptamukesh gupta
Hi Shaikh,

Please use below code:- 
 
Global class copyWarmAccounts implements Database.Batchable<Sobject>
{
    Global Database.QueryLocator start(Database.BatchableContext bContext)
    {
        string query = 'Select name, id, rating from account';
        return database.getQueryLocator(query);
    }
    Global void Execute(Database.BatchableContext bContext, list<Account> batch)
    {
	    list<warm_accounts__c> lstwac = new list<warm_accounts__c>();
		Set<Id> accIds = new Set<Id>();
		for(Account acc : batch)
        {
		  accIds.add(acc.Id);
		}

		Map<Id,Warm_accounts__c> wramAccList = Map<Id,Warm_accounts__c>([SELECT Id FROM  Warm_accounts__c Id IN: accIds]);

        for(Account acc : batch)
        {
                if(acc.rating == 'warm' && wramAccList.get(acc.Id).Id != acc.id )
                {
                        Warm_accounts__c wac = new Warm_accounts__c();
                        wac.name = acc.name;
                        wac.Rating__c = acc.Rating;
                        wac.Id = acc.Id;
                        lstwac.add(wac);
                 }
         }
        insert lstwac;
    }
    Global void finish(Database.BatchableContext bContext)
    {
        //do nothing
    }
}



if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh


 

All Answers

CharuDuttCharuDutt
Hii Shaikh
Try Below Code

Points to Follow 
Make Checkbox Field on Account To Check To Process Only Those Accounts Whose Rating Is Warm Once Those Accounts Gets Processsed Then Those Wil Not Be Processed Further Again
{
    Global Database.QueryLocator start(Database.BatchableContext bContext)
    {
        string query = 'Select name, id, rating from account where AND isProcessed = false';
        return database.getQueryLocator(query);
    }
    Global void Execute(Database.BatchableContext bContext, list<Account> batch)
    {
        list<warm_accounts__c> lstwac = new list<warm_accounts__c>();
        for(Account acc : batch){
                  if(acc.rating == 'warm'){
				  
                           Warm_accounts__c wac = new Warm_accounts__c();
                        wac.name = acc.name;
                        wac.Rating__c = acc.Rating;
                        wac.Id = acc.Id;
                        lstwac.add(wac);
						
						isProcessed = true;
                 }
         }
        insert lstwac;
		update batch;
    }
    Global void finish(Database.BatchableContext bContext)
    {
        //do nothing
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
mukesh guptamukesh gupta
Hi Shaikh,

Please use below code:- 
 
Global class copyWarmAccounts implements Database.Batchable<Sobject>
{
    Global Database.QueryLocator start(Database.BatchableContext bContext)
    {
        string query = 'Select name, id, rating from account';
        return database.getQueryLocator(query);
    }
    Global void Execute(Database.BatchableContext bContext, list<Account> batch)
    {
	    list<warm_accounts__c> lstwac = new list<warm_accounts__c>();
		Set<Id> accIds = new Set<Id>();
		for(Account acc : batch)
        {
		  accIds.add(acc.Id);
		}

		Map<Id,Warm_accounts__c> wramAccList = Map<Id,Warm_accounts__c>([SELECT Id FROM  Warm_accounts__c Id IN: accIds]);

        for(Account acc : batch)
        {
                if(acc.rating == 'warm' && wramAccList.get(acc.Id).Id != acc.id )
                {
                        Warm_accounts__c wac = new Warm_accounts__c();
                        wac.name = acc.name;
                        wac.Rating__c = acc.Rating;
                        wac.Id = acc.Id;
                        lstwac.add(wac);
                 }
         }
        insert lstwac;
    }
    Global void finish(Database.BatchableContext bContext)
    {
        //do nothing
    }
}



if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh


 
This was selected as the best answer