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
BenPBenP 

update records within custom object

Let me start by stating that while I can figure out what code does, I am by no means a programmer. 
 
We have a custom object (SVMXP__Service_Group__c) that has around 2,000 records in it.  These records almost never get updated, but they have a checkbox that needs to be unchecked when a date field expires.  There is currently a workflow for this, but if I understand those correctly they only fire when a record is updated which doesn't happen.  I'd like to make a little script that I could call with cronkit which would just check if the date field is less than today and if so change the checkbox.
 
The object is SVMXP__Service_Group__c
the field to change from true to false is SVMXP__Active__c
the field to check if the date is less than today is PolicyExp__c
 
I have some documents on apex, but I am overwhelmed right now.  It's simple in my mind, but I don't know enough to get anything going.
LumingLuming
Function for that is quite straightforward, i think challenge is about how to call this function.
 
As my understanding, APEX codes can be called by trigger or other user actions. hower, it seems like that you want a daemon. I don't think there is a easy solution.
 
RickyGRickyG
BenP -

Have you looked into timed workflows?  If the expiry date is not too far in the future, you can simply set up a timed workflow to do a field update at the appropriate time.

Hope this helps.
BenPBenP

There is a timed workflow setup that is supposed to change the active field to false one hour before "policyexp".  Although I'm still testing this, it seems to not be doing what it should.  What keeps getting me is the evaluation criteria that says this will only run "When a record is created, or when a record is edited and did not previously meet the rule criteria".  Seeing that these records are almost never edited, this rule may not check the record.

I could be doing something wrong here, so please correct me if so.

BenPBenP

luming,

I should be able to call the code with cronkit from what I understand of it.  I'd just need the code to call.

RickyGRickyG
Ben -

So it sounds like you want to do this on existing records.  If so, you are out of luck with this solution right now, but you could always just do a 'dummy' update on the existing records to trigger the timed workflow iniitially.

Hope this helps.
BenPBenP

Ok, so check me on my understanding. 

If I export the records via the data loader and then update them right back in.  From then on any record that meets that timed workflow will be acted upon?  In other words, when the record is updated the workflow is checked and a "timer" is set that waits for the timed workflow to become true?

RickyGRickyG
When a timed workflow is triggered, it is placed in a queue, based on the time delay specified for the workflow.  When the workflow is about to be executed from the queue (based on the time specified), conditions are checked again to make sure the workflow should still be executed.

If the timed workflow is scheduled for a time that has passed, it will be placed in the queue for immediate operation.

The one potential flaw in your understanding has to do with exporting and updating the records to trigger the workflow.  Not sure if there will actually be a workflow-triggering update if the records are just reloaded with the same values.
BenPBenP
That makes sense.  Is that que that these pending actions are put into visible?
 
Maybe I can do a mass update on a field for these records, then update again to put it back.  Then wait and see.
RickyGRickyG
Ben -

You should read about timed workflows in the manual, or in Chapter 5 of the Developer Guide, for the comprehensive info you are seeking.
BenPBenP
Will do.  Thank you for the help thus far, I have learned something today.
LumingLuming
Just checked CronKit. It is great. I believed it can solve your problem.
 
I think you can edit  trigger from that application and add your logic to check expiracy date.
 
BenPBenP
Yeah, I'm going to study up on both options and see what I can come up with.
cherialcherial
Hi,
          I am also facing the same problem to update all records. I tried with triggers but it is not worked and only one record get updated. You have any solution to update all records?
Please guide me....

Thanks in advance
-Nath
BenPBenP
after some testing I've found that in my case I have to update the actual field that the rule is checking and that will then trigger the action. 
 
Example:
my workflow action is to change "active" from true to false one hour before "policy_expire".  The only way I've been able to get it to check the old records is to change the "policy_expire" field.  I just use the data loader to export the data and then increase the date by one day and update it back.  After that there are new entries in the time based workflow monitoring screen. 
 
Now I just have to figure out how to get excel to increase the date cell by one day on 1500 records. :)
RickyGRickyG
Just a thought - why not use Execute Anonymous in either the Force.com IDE or the System Log?  Since you are going to change the data, no sense taking in all out and adding it all back in.  Your only hurdle will be making sure you don't update more than 1000 records at a time.

Hope this helps.