You need to sign in to do that
Don't have an account?
SUMIT BANERJEE
Hi I have written batch class to update stage field of opportunity as per close date
Hi I have written batch class to update stage field of opportunity as per close date but when I run my batch class it will be updating close date also.
How to prevent that can you guide where I did mistake in my code :
global class batchOpportunityUpdate implements Database.Batchable <sObject>{
date dt1 = system.today().addDays(+30);
date dt2 = system.today().addDays(+120);
date dt3 = system.today().addDays(+182);
Id oppRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('New Rec3 Type').getRecordTypeId();
global Database.QueryLocator start(Database.BatchableContext BC){
string query = 'SELECT Id,CloseDate,StageName,RecordTypeId FROM Opportunity Where RecordTypeId =:oppRecordTypeId ';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Opportunity> scope){
for(Opportunity opp : scope){
if(opp.CloseDate <= dt1 && oppRecordTypeId == opp.RecordTypeId){
System.debug('Record'+oppRecordTypeId +'dt1'+dt1);
opp.StageName ='Qualification';
}else if(opp.CloseDate <= dt2 && oppRecordTypeId == opp.RecordTypeId){
opp.StageName ='Needs Analysis';
}else if(opp.CloseDate <= dt3 && oppRecordTypeId == opp.RecordTypeId){
opp.StageName ='Closed Won';
}
}
update scope;
}
global void finish(Database.BatchableContext BC){
}
}
Thanks,
sumit
How to prevent that can you guide where I did mistake in my code :
global class batchOpportunityUpdate implements Database.Batchable <sObject>{
date dt1 = system.today().addDays(+30);
date dt2 = system.today().addDays(+120);
date dt3 = system.today().addDays(+182);
Id oppRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('New Rec3 Type').getRecordTypeId();
global Database.QueryLocator start(Database.BatchableContext BC){
string query = 'SELECT Id,CloseDate,StageName,RecordTypeId FROM Opportunity Where RecordTypeId =:oppRecordTypeId ';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Opportunity> scope){
for(Opportunity opp : scope){
if(opp.CloseDate <= dt1 && oppRecordTypeId == opp.RecordTypeId){
System.debug('Record'+oppRecordTypeId +'dt1'+dt1);
opp.StageName ='Qualification';
}else if(opp.CloseDate <= dt2 && oppRecordTypeId == opp.RecordTypeId){
opp.StageName ='Needs Analysis';
}else if(opp.CloseDate <= dt3 && oppRecordTypeId == opp.RecordTypeId){
opp.StageName ='Closed Won';
}
}
update scope;
}
global void finish(Database.BatchableContext BC){
}
}
Thanks,
sumit
I believe this is standard salesforce functionality. If an opportunity is future and the status is changed to closed then close date gets updated to the date which it closed (e.g. today). This seems to be what you are seeing. If you wish to prevent this you might want to store the original close date in a date variable and adjust after the initial update to the record(s). Simply changing your execute function like this might do the trick:
Hope this helps!
If you have any further questions please feel free to as them. Also, if you feel like my answer helped resolve your issue(s) please remember to mark it as best answer so others can benefit from it as well.
AM
All Answers
I believe this is standard salesforce functionality. If an opportunity is future and the status is changed to closed then close date gets updated to the date which it closed (e.g. today). This seems to be what you are seeing. If you wish to prevent this you might want to store the original close date in a date variable and adjust after the initial update to the record(s). Simply changing your execute function like this might do the trick:
Hope this helps!
If you have any further questions please feel free to as them. Also, if you feel like my answer helped resolve your issue(s) please remember to mark it as best answer so others can benefit from it as well.
AM