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
magandrezmagandrez 

Question about to use or not a trigger

Hi all,

 

I have a general question regarding the use of triggers. My problem is the following:

 

I have to schedule monthly an update over one checkbox field. The problem that I have is that I can't find an elegant solution to calculate the next month "date of update" because all the formulas I can build are based on TODAY() and, as you may have notice, that changes every day (what a surprise!).

 

My question is if whether I should use triggers or any other solution for this problem (workflows and update the needed field?)

 

If I need to create a trigger, do you have any clue on how to? Since the triggers trigger after or before an action happens, I don't have many ideas on how to model this...

 

Thank you all,

 

MGA

Best Answer chosen by Admin (Salesforce Developers) 
magandrezmagandrez

Hi Pankaj!,

 

Indeed it was!. I dug a bit more into the posibility of implementing an Schedulable and I got the solution. So now I have a method scheduled to update what I need every 1st of the month. I don't have deeper requierements on the timing, so with the Apex Scheduler is enough for me to schedule this job.

 

Here is the code:

 

global class scheduleUpdateField implements Schedulable{

global void execute(SchedulableContext SC){
    
    List<Product2> allPos = [SELECT Id, IsActive FROM Product2];

    for (Integer i = 0; i<allPos.size();i++){

        allPos[i].IsActive = false;
    }
    
    if (allPos.size()>0){
        update allPos;
    }
   
}
    
}

 

All Answers

pankaj.raijadepankaj.raijade

If you can achieve required functionality using workflow rule it would be best option. as it can be done just by confuguration.

 

Please try it. 

 

if work flow is not good enough let us know your functionality in detail will give you trigger code.

 

Regards,

Pankaj Raijade.

MelliottMelliott

Are you saying that you want to update the field every month or only the month after the field was initially populated.  This will make a difference.  If once a month, every month, you need to set a specific date to do this. 

 

If you want to do it only the month after it was initially populated, you could do this.

 

Step 1: Add a field to capture the date field x was updated

Step 2: Use a workflow that says field X is true, then fire a time based action that says do whatever action 30 days from rule trigger date.

magandrezmagandrez

Hi,

 

I already made some tests in my dev org and I couldn't achieve my purpose. This is in detail, my problem:

 

I need to change the value of a checkbox called "Active" from true to false every first day of every month in order to do some other tasks. In order to make this happen, I thought on a workflow rule triggered depending on a date field, but for that i need a field (lets call it "Next Date of Update") pointing out the 1st day of the next month. So if I calculate this field ("Next Date of Update"), it will be different everyday because all the ways to calculate next month that I can think about are based on TODAY() function.

 

I hope I explained myself properly...

 

Also I thought about make this task (the field update to false) using an Apex class that implements the interface Schedulable.

 

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_scheduler.htm

 

pankaj.raijadepankaj.raijade

if you want to update records on 1st of evert month scheduleble class would be best possible option.

 

it would be scheduled to run on every 1st day of month.

 

Regards,

Pankaj Raijade,

magandrezmagandrez

Hi Pankaj!,

 

Indeed it was!. I dug a bit more into the posibility of implementing an Schedulable and I got the solution. So now I have a method scheduled to update what I need every 1st of the month. I don't have deeper requierements on the timing, so with the Apex Scheduler is enough for me to schedule this job.

 

Here is the code:

 

global class scheduleUpdateField implements Schedulable{

global void execute(SchedulableContext SC){
    
    List<Product2> allPos = [SELECT Id, IsActive FROM Product2];

    for (Integer i = 0; i<allPos.size();i++){

        allPos[i].IsActive = false;
    }
    
    if (allPos.size()>0){
        update allPos;
    }
   
}
    
}

 

This was selected as the best answer