+ Start a Discussion
SF DEVSF DEV 

Batch apex

i have appointment and accounts objects, both got 2 similar fields email and tax, if appoinment getting insert with the same email and tax of account record, then update appointment acc__c with account id.

 

 

how to implement in batch apex.

is my code written is correct?

 


global class updaterecords implements Schedulable
{

public updaterecords()
{

}
/* to excute the fields */
/*
global void execute(SchedulableContext ctx)
{
appointment []app=[select email,tax  from appointment where CreatedDate = TODAY ];

account []accts=[select email, tax_acc from account];


for(appointment s:app)
{
for(account a:accts)
{
if(a.email==s.email|| a.tax_acc==s.tax)
{
s.acct__c= a.ID;

}
}
}
update app;

}
}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Prady01Prady01

Woop's!!

global class batchUpdateAccounts implements Database.Batchable<sObject>, Database.Stateful{

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

    return database.getQuerylocator('select id,name,CustomNameFiled__c from account');
}

global void execute(Database.BatchableContext bc, List<SObject> scope) {
    
        List<Account> accs = (List<Account>)scope;
        List<Account> accsToUpdate = new List<Account>();
 
        for(Account acunts : accs){
        If(acunts.CustomNameFiled__c == 'world' || acunts.CustomNameFiled__c == null){
            acunts.CustomNameFiled__c = 'Hello World';
            accsToUpdate.add(acunts);
        }
    }
    update accsToUpdate;

}

global void finish(Database.BatchableContext bc){
        

}

}

 

All Answers

Prady01Prady01

Hello there, You talking about a Batch classs then,

global class yourclassname implements Database.Batchable<sObject>, Database.Stateful{

global Database.QueryLocator start(Database.BatchableContext bc){
	return database.getQuerylocator('SOQL query space! Eg: select id, name from account where name=\'SuperAccounts\'');
}

global void execute(Database.BatchableContext bc, List<SObject> scope) {
    
        List<Account> accs = (List<Account>)scope;
        Map<id,Account> accountsToupdate = new Map<id,Account>(); 
        for(Accounts acunts : accs){
        If(acunts.CustomNameFiled == 'world' && acunts.CustomNameFiled == null){
        	acunts.CustomNameFiled = 'Hello World';
        	accountsToupdate.add(acunts.id,acunts);
        }
	}
	update accountsToupdate.values();
}

global void finish(Database.BatchableContext bc){
        

}

 

Prady01Prady01

Woop's!!

global class batchUpdateAccounts implements Database.Batchable<sObject>, Database.Stateful{

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

    return database.getQuerylocator('select id,name,CustomNameFiled__c from account');
}

global void execute(Database.BatchableContext bc, List<SObject> scope) {
    
        List<Account> accs = (List<Account>)scope;
        List<Account> accsToUpdate = new List<Account>();
 
        for(Account acunts : accs){
        If(acunts.CustomNameFiled__c == 'world' || acunts.CustomNameFiled__c == null){
            acunts.CustomNameFiled__c = 'Hello World';
            accsToUpdate.add(acunts);
        }
    }
    update accsToUpdate;

}

global void finish(Database.BatchableContext bc){
        

}

}

 

This was selected as the best answer
SF DEVSF DEV

Thank you, for the solution.