You need to sign in to do that
Don't have an account?
MaggieSumit
How to Update acccount owner and opportunity Stage, If opportunity stage equal to Qualification by Batch Class
This Code but I think this is not correct
- global class BatchOpportunitys implements Database.Batchable<SObject>, Database.Stateful{
- List<Opportunity> listRecords = new List<Opportunity>();
- global Database.QueryLocator start(Database.BatchableContext BC)
- {
- String query = 'Select Id, Name, CloseDate,StageName From Opportunity';
- return Database.getQueryLocator(query);
- }
- global void execute(Database.BatchableContext BC, List<SObject> scope){
- for(Opportunity obj : (Opportunity[]) scope){
- if(obj.StageName == 'Qualification'){
- obj.CloseDate = date.today();
- obj.StageName = 'Value Proposition';
- obj.account.ownerId = obj.ownerId;
- obj.account.Status__c = 'Lost';
- listRecords.add(obj);
- }
- }
- }
- global void finish(Database.BatchableContext BC){
- system.debug('list size :: '+listRecords.size());
- if(!listRecords.isEmpty())
- {
- update listRecords;
- }
- }
- }
here is the code .
global class BatchOpportunitys implements Database.Batchable<SObject>, Database.Stateful{
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query = 'Select Id, Name,AccountId, CloseDate,StageName From Opportunity';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<SObject> scope){
List<Opportunity> opptobeUpdate = new List<Opportunity>();
List<Account> accobeUpdate = new List<Account>();
for(Opportunity obj : (Opportunity[]) scope){
if(obj.StageName == 'Qualification'){
obj.CloseDate = date.today();
obj.StageName = 'Value Proposition';
Account a = obj.Account;
a.ownerId = obj.ownerId ;
//obj.account.ownerId = obj.ownerId;
// obj.account.Status__c = 'Lost';
opptobeUpdate.add(obj);
accobeUpdate.add(a) ;
}
}
if(opptobeUpdate.size()>0){
update opptobeUpdate ;
}
if(accobeUpdate.size()>0){
update accobeUpdate ;
}
}
global void finish(Database.BatchableContext BC){
}
}
global class BatchOpportunitys implements Database.Batchable<SObject>, Database.Stateful{
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query = 'Select Id, Name,AccountId, CloseDate,StageName From Opportunity';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<SObject> scope){
List<Opportunity> opptobeUpdate = new List<Opportunity>();
List<Account> accobeUpdate = new List<Account>();
for(Opportunity obj : (Opportunity[]) scope){
if(obj.StageName == 'Qualification'){
obj.CloseDate = date.today();
obj.StageName = 'Value Proposition';
if(obj.Account!=null){
Account a = obj.Account;
a.ownerId = obj.ownerId ;
accobeUpdate.add(a) ;
obj.account.Status__c = 'Lost';
}
//obj.account.ownerId = obj.ownerId;
// obj.account.Status__c = 'Lost';
opptobeUpdate.add(obj);
}
}
if(opptobeUpdate.size()>0){
update opptobeUpdate ;
}
if(accobeUpdate.size()>0){
update accobeUpdate ;
}
}
global void finish(Database.BatchableContext BC){
}
}