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
Nisar AhmedNisar Ahmed 

Auto Field Update after 30 minutes of record creation.

Hi All,

 

I have a custom object by name 'Portfolio__c'. I have a picklist field 'Status_c' with values 'New' and 'Activated'.

When the record is first created the Status value is New. 

After 30 minutes of the record creation, I need to update the Status to Activated automatically.

 

I have written the following trigger, but still not able to update the field.

trigger portfolioAfterInsertAfterUpdate on Portfolio__c (after insert, after update) {

	List<Portfolio__c> Pfs = new List<Portfolio__c>();
	for (Integer i=0;i<trigger.new.size();i++) {	
		Pfs = [Select Name, Status__c, CreatedDate, Id from Portfolio__c where Status__c =: 'New' limit 1];	
	    if(Pfs.size() > 0 && Pfs.get(0).Status__c == 'New'){
	    	Datetime dt = Pfs.get(0).CreatedDate;
	    	dt = dt.addMinutes(30);
	    	if(dt == system.now()){
		    	Portfolio__c pf = new Portfolio__c(id=Pfs.get(0).id, Status__c = 'Activated');
		        update pf;
	    	}
	    }
	}
}

Could you please tell me where I have gone wrong or any work around to do the same.

Any help will be highly appreciated.

 

Chamil MadusankaChamil Madusanka

Hi,

 

You can use the Workflow with Time trigger for accomplish your above requirement.

 

Create a workflow for your Portfolio__c object with entry criteria status__c equal New

Then Add the time trigger for that workflow. (But you cannot add the time after 30 min. Its need some whole number like 1,2,3..)

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

chamil's blog

Nisar AhmedNisar Ahmed

Hi Chamil,

 

Thanks for the reply.

Yes, thats true and I already tried that. Since I want it to update it after 30 minutes I am using different approach to do this.

Thanks again. But please let me know if you find any other way to accomplish this.

 

PresbPresb

I notice that your time test "if(dt == system.now())" which may never be true.  Did you try "if(dt < system.now())"?  

Starz26Starz26

I will start with the obvious first question.....

 

The trigger will fire immediatly after the record is inserted. As such, you will never meet a criteria of 30 minutes post creation. The only way I see being able to meet the strict 30 minute criteria is to use the trigger to start a scheduled apex...

 

This will present problems as well. Mainly, the time may not be exactly 30 minutes and you can only schedule a max of 10 at once.

 

 

LoserKidLoserKid

If you or your company has a server that can intergrat with salesforce you can us that to run code, batches, or update fields and fire off triggers. We have a WebMethods Server at my company that we use to trigger and update to all open cases every 5 minutes to track SLA times.