+ Start a Discussion
Chris Brady 8Chris Brady 8 

how to write an apex call to update a field on the lead object

Hello,

I am new to APEX so thanks in advance for helping a newbie out.

I need an APEX to run at midnight (I will use the SF scheduler) to update a custom field (whatever__c) with a number (42). My end goal is to have this field update (whatever__c = 42) will kickoff a process I created in process builder. 

Thanks again everyone 
AubryAubry
Chris,
This would involve use of the Schedulable apex interface. 

I strongly recommend you check out the related Trailhead course: https://trailhead.salesforce.com/en/content/learn/modules/asynchronous_apex/async_apex_scheduled
Chris Brady 8Chris Brady 8
Here's what I have now
trigger UpdateWhatever__c on Lead (before update) {
    if(Lead.Whatever__c <> 1321){
        Whatever__c = '42';
        
    }
    
    
}     
But am getting an error on line 1: Invalid character in identifier: UpdateWhatever__c
AubryAubry
Chris, I don't know if you can put double-underscores in a trigger name.  change the name to not have the "__c" at the end, just "UpdateWhatever"
Chris Brady 8Chris Brady 8
Tried this and still getting errors
trigger UpdateWhatever on Lead (before update) {
    if(Lead.Whatever__c <> 1321){
        Whatever__c = '42';
        
    }
    
    

on line 2 and 3saying varriable does not exist Whatever__c
AubryAubry
If what you've pasted is your actual trigger code, there are a few changes you'll need to make.
trigger UpdateWhatever on Lead (before update) {
    for(Lead updlead : Trigger.new){ // First, loop through the leads that are triggering this apex
        if(updlead.Whatever__c != 1321){ // Check the value of each lead's Whatever__c field
            updlead.Whatever__c = 42; // Update the value, if criteria are matched.
        }
    }
}
I changed the <> formula operator to the != Apex operator.
I also removed the quotes around 42, assuming that Whatever__c is a numeric field.  If it is a text field, you will want to put quotes around both 42 AND 1321.
 
Chris Brady 8Chris Brady 8
Thanks Aubrey, I'll test this out later today, and don't worry, I'll make sure to mark as best😁
Chris Brady 8Chris Brady 8
Getting a line 0 invalid parameter value error. 

I copied and pasted and don't even see a line 0 
Tad Aalgaard 3Tad Aalgaard 3
Not sure if this is the case, but are you pasting the code that Aubrey wrote for you into in a Apex Trigger or and Apex Class?  If you are not and you are pasting it into an Apex Class then it will fail.

Please paste your full code as well as the full class or trigger name.
Chris Brady 8Chris Brady 8
Thanks Tad, I put it in a trigger and there were no errors.

Now I just need to be able to schedule this for a nightly run. Any suggestions? 

Thanks again, I'm still learning
Tad Aalgaard 3Tad Aalgaard 3
You really want to create a Schedulable class instead of a trigger if you are wanting to run this on a scheduled basis.  Triggers only run when a record is inserted, updated, or deleted.  Schedule classes can be set up to run on schedule.

Use the Trailhead link that Aubry posted earlier to learn about creating a schedulable job.  And, if possible remove the Process Builder you wrote and place it's actions into the schedulable class as you don't want or need two things stepping on each other.