Batch example scenario
Consider two fields x and y. we have already uploaded or inserted so many Account records. At that time we haven't written any trigger or we were not aware of the future requirement.
Requirement: If the x value is 1 we have to update 10 in DB, if x value is 2 we have to update 20 and so on [code is self-explanatory].
we might have a huge number of records with these unique values (1,2,3,4) and while processing in DB should be mapped to corresponding values like 20,30,40,50.
As of know, only 4 unique numbers starting from 1.
for example:
x ---->y
1---->20
2---->30
3---->40
4---->50
Best approach is batch class
global class MapConceptUpdate implements Database.Batchable<sObject>{
global Database.QueryLocator start(Database.BatchableContext BC) {
string query='select id,x_value__c,y_value__c from Account';
return database.getqueryLocator(query);
}
global void execute(Database.BatchableContext BC,List<Account> scope){
map<double,double> mapval=new map<double,double>();
mapval.put(1,20);
mapval.put(2,30);
mapval.put(3,40);
mapval.put(4,50);
List<Account> aclist=new List<Account>();
for(account ac:scope){
if(ac.x_value__c!=null && !mapval.isEmpty()&& mapval.containsKey(ac.x_value__c)&&mapval.get(ac.x_value__c)!=null){
ac.y_value__c=mapval.get(ac.x_value__c);
aclist.add(ac);
}
}
if(!aclist.isEmpty()){
update aclist;
}
}
global void finish(Database.BatchableContext BC) {
}
}
Requirement: If the x value is 1 we have to update 10 in DB, if x value is 2 we have to update 20 and so on [code is self-explanatory].
we might have a huge number of records with these unique values (1,2,3,4) and while processing in DB should be mapped to corresponding values like 20,30,40,50.
As of know, only 4 unique numbers starting from 1.
for example:
x ---->y
1---->20
2---->30
3---->40
4---->50