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
djensendjensen 

A trigger that updates a picklist field based on a date field

Hello - Below is a simple trigger on a contact record that updates a picklist field called Demo Status when a date field called Demo Date is either 1 year or 2 years old. 

Questoin is: How can I get this trigger to automatically update existing contact records when the Demo Date reaches 1 years old? Right now the trigger only updates the field if the contact record is edited.  Looking for something that doesn't require editing to trigger this trigger.

(I am not a developer so hopefully the trigger is written correctly. It does work, but I haven't tested it fully.)

trigger UpdateDemoStatus on Contact (before update)
{
    for (Contact cnts : Trigger.new){
          if (cnts.Demo_Date__c < date.today() - 730) {
            cnts.Demo_Status__c = 'No Demo';
        } else if (cnts.Demo_Date__c < date.today() - 365) {
            cnts.Demo_Status__c = 'Stale';
        } else {cnts.Demo_Status__c = cnts.Demo_Status__c;}
}
}
Best Answer chosen by djensen
Ajay K DubediAjay K Dubedi
Hello,
You can write script and make changes in the existing records ,trigger only fires when the event occur(insert, update or delete) on records.
Go to  developer console --> anonymous block  and try  the following code and execute it.
like this: 
list<Contact> contactListToupdate = new list<Contact>();
list<Contact> contactList =[write the query here to fetch all contact  whose record you would like to Update];
  for (Contact cnts : contactList)
    {
       if (cnts.Demo_Date__c < date.today() - 730) {
           cnts.Demo_Status__c = 'No Demo';
        } 
       else if (cnts.Demo_Date__c < date.today() - 365) {
            cnts.Demo_Status__c = 'Stale';
        } 
     else {
        cnts.Demo_Status__c = cnts.Demo_Status__c;
        }
    contactListToupdate.add(cnts);
    }
update contactListToupdate;

hope this would be helpful to you
 

All Answers

Ajay K DubediAjay K Dubedi
Hello,
You can write script and make changes in the existing records ,trigger only fires when the event occur(insert, update or delete) on records.
Go to  developer console --> anonymous block  and try  the following code and execute it.
like this: 
list<Contact> contactListToupdate = new list<Contact>();
list<Contact> contactList =[write the query here to fetch all contact  whose record you would like to Update];
  for (Contact cnts : contactList)
    {
       if (cnts.Demo_Date__c < date.today() - 730) {
           cnts.Demo_Status__c = 'No Demo';
        } 
       else if (cnts.Demo_Date__c < date.today() - 365) {
            cnts.Demo_Status__c = 'Stale';
        } 
     else {
        cnts.Demo_Status__c = cnts.Demo_Status__c;
        }
    contactListToupdate.add(cnts);
    }
update contactListToupdate;

hope this would be helpful to you
 
This was selected as the best answer
djensendjensen
Thanks so much for this update. This is very helpful. Greatly appreciated.