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
TanuTanu 

Time trigger to update field based on condition

Hi All,
Please let me know if is there any workaround to achieve below requirement:
I have created a picklist field ABC(having values Y and N) and formula field "stage change duration". now my requiremnt is if stage change duration >45 days then ABC should get updated as N.

I created a timebased workflow but it works with newly created opportunities. my main target is to update existing opportunities in which stage change duration> 45 days.

Any suggestions??
Thanks




 
Pankaj MehraPankaj Mehra
Hi Tanu,

Create a one time batch class to update existing records and after that your time-based workflow will take care of upcoming opportunities.
 
global class batchUpdateOpportunity Implements Schedulable, Database.Batchable<sObject>{

    
	global database.queryLocator start(Database.BatchableContext BC) {
	    // get Opportunity
           String query = 'Select Id from Opportunity Where Stage_Change > 45 and Updated = N'
	    ...
	} 

	global void execute(Database.BatchableContext BC, list <Obj1__c> scope) {
	    for(Opportunity o :scope ){
              o.updated = Y
            }
           update scope;
	    ...
	}



If you're satisfied with the answers provided, please don't forget to select a Best Answer.
Thanks!
 
TanuTanu
Hi Pankaj,

Thanks for your timely response.

I have already updated existing records using workbench. Still i am unable to see any record in the queue those need to be updated as N after 45 days. Only newly created opportunities are in the queue. 

Will it work if i write a batch class which will run daily so that it will count the stage change duration(i.e. no. of days) and fire on 45th day.. please Correct me if i am wrong..  ??

Thanks 
Tanu
Pankaj MehraPankaj Mehra
Hi Tanu,

Yes, the best practices says that you can use a daily batch to update such records but you already have a time based workflow to update new records so there's no need of daily batch, just create a batch one time and execute it according to need

In case you are not able to see updated records from workbench, following may be the cause and resolution :

1. Opportunity may be private and you do not have access to such opportunities so workbench would not be able to process all records. You can update your users permission and set "Modify All Data" permission to true so that you can update all opportuntiy records.

2. In case you have all necessary permission open developer console and try to query records [Select Id from Opportunity where Stage_Change > 45 and Updated = N'] to see the records.

3. Check for any Validation rule that restrict the records being updated (Old or closed Opportunites are restricted to be updated)

4. At Last check debug logs and Apex Job (Queue) if the batch is executed with no error.

Thanks
TanuTanu
Thanks Pankaj... your suggestions really works.. 

would request you to please help with below scenario:
In my org, i created workflow email alerts which fires whenever any opporunity is getting created or edited. if a batch apex is scheduled(as discussed above), which will update a field on opportunity then email alerts will be triggered???? if yes !! then how to stop these alerts as i dont want to deactivate them. please share your thoughts!!..

Thanks
Tanu