+ Start a Discussion
Eddie DeveauEddie Deveau 

Field Monitoring, Updating other Fields Based on Last Activity of a Specified Field

I'm not sure if this should be solved w/ flow or programmatically....

over a 12 month period if a specified field has not been updated then a certain score is to be provided for the related account record.

For example if the specified field has been updated within 1-3 months, it is given a score of 5; was updated within 4-6 months, score of 4; updated within 7-8 months, score 3; updated 9-10 months, score 2; updated 11+ months score 1.... 

Looking for the best practice to solve for this. 
tbrookstbrooks
Hi Eddie,

The best/ easiest way to go about this is to create a second date or date/time field for the last modified date for that specific field. Then in a simple process builder check if that field value has changed and if so populate the timestamp. Then you could create a formula field to come up with the score by checking the timestamp vs the current time and determining if it is 1-3 months old, 4-6 months old, etc. 

There is a programatic solution to this where you could run a scheduled batch class (Would have to be this unless you wait until the object is updated to run through a trigger or a button is pressed to launch the code via a lightning component. But a scheduled batch would do it automatically) to query the object and it's related history something like
SELECT Name, customfield__c, (SELECT OldValue, NewValue, CreatedDate FROM Histories WHERE Field = 'customfield__c' ORDER BY CreatedDate DESC) 
FROM foo__c WHERE customfield__c != null
And then checking the first index of the related history objects createddate
 
for(foo__c record : scope){
 if(record.Histories.size() != 0){
  Integer monthsBetween = 
  Date.valueOf(record.Histories[0].CreatedDate).monthsBetween(System.Today());
  if(monthsBetween <= 3){
  record.Score__c = 5;
  }
  else if(monthsBetween >3 && monthsBetween <=6){
  record.Score__c = 4;
  }
  //etc.
 }
}
update scope;

Now the code above hasn't been tested so I can't guarantee functionality but that was just a rough draft of something I think could work. Again, the first solution is simpler and easier to maintain and since it is a formula field once the timestamp is updated it will automatically recalculate rather than a scheduled batch. 

If this answers your question please mark as best answer!
Regards,
Tyler
 
vicky Pakharevicky Pakhare
Very nice <a href="https://mahakal-shayari.blogspot.com/2020/04/royal-dosti-yari-status-shayari-in-hindi.html">Yaari Status in hindi 2020</a>

Very nice <a href="https://mahakal-shayari.blogspot.com/2020/04/jaat-attitude-status-in-hindi.html">Jaat status 2020</a> 

Very nice <a href="https://mahakal-shayari.blogspot.com/2020/04/indian-army-status-shayari.html">Army Status in Hindi 2020</a>

Very nice <a href="https://mahakal-shayari.blogspot.com/2020/04/bhaigiri-dadagiri-attitude-status-shayari.html">bhaigiri Status</a>


Very nice <a href="https://mahakal-shayari.blogspot.com/2020/04/royal-attitude-status-in-marathi.html
">royal attitude Status in marathi</a>


Very nice <a href="https://mahakal-shayari.blogspot.com/2018/11/latest-mahakal-shayari-mahakal-status.html
">mahakal shayari</a>


Very nice <a href="https://mahakal-shayari.blogspot.com/2019/02/mujhe-koi-fark-nahi-padta-status.html
">mujhe koi fark nahi padta status</a>