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
econ242econ242 

Newbie to Triggers - need HELP

Hi Everyone,

I'm just getting started on how to use triggers...I'm trying to write a trigger that will update a custom field (Stage_c) to a specific picklist value (Actively Working) on my Lead Detail page based on an Activity Task Type - Call to Qualify (Unsuccessful) - when the Activity has been completed.

I came across this post - https://developer.salesforce.com/forums/ForumsMain?id=906F00000008mcOIAQ - and tried tweaking it a bit to work for this requirement, but I keep getting errors.

ANY assistance would be greatly appreciated!!!

Thank you in advance for your help!!!
Best Answer chosen by econ242
econ242econ242
PERFECT!!! Thank you Daniel!!!

All Answers

Daniel B ProbertDaniel B Probert
hey try this code should do the trick

trigger changeLeadStatus on Task (before insert, before update) {
    List<Lead> LeadToUpdate = new List<Lead>();
        for (Task tsk: Trigger.new){
            if(tsk.Status=='Completed' && tsk.Type=='Call to Qualify (Unsuccessful)'){
                Lead ld = new Lead(Id=tsk.whoid);
                    ld.Stage__c = 'Actively Working';
                LeadToUpdate.add(ld);            
            }
            
        }        
        update LeadToUpdate;
    }

if it solved you question please mark as solved :)
econ242econ242
PERFECT!!! Thank you Daniel!!!
This was selected as the best answer
Daniel B ProbertDaniel B Probert
no worries glad it worked..
econ242econ242
If I wanted to replicate this trigger, but instead of on a Lead, it would be on an Account...would it be similar? I tried using the code below, but I keep getting an exception (below the code):

trigger changeAccountStagetask1 on Task (before insert, before update) {
    List<Account> accsToUpdate = new List<Account>();
        for (Task tsk: Trigger.new){
            if(tsk.Status=='Completed' && tsk.Subject=='Set 1st Appointment'){
                Account ld = new Account(Id=tsk.whoid);                             
                     ld.Stage__c = 'Need To Set Meeting';
                accsToUpdate.add(ld);          
            }
          
        }      
        update accsToUpdate;
    }

Error:

Apex trigger changeAccountStagetask1 caused an unexpected exception, contact your administrator: changeAccountStagetask1: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.changeAccountStagetask1: line 11, column 1

Any thoughts? I'm sure I'm way off in my code...ugh!!!

Thank you in advance!!!  :)