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
Jairus McClureJairus McClure 

Apex trigger create task on lead needs to be "when edited to subsequently meet criteria"

Apex trigger creat task on lead needs to be when edited to subsequently meet criteria. right now i have it as follows:

trigger createtask on lead (after insert) { 

The "after insert" needs to be changed but I'm not sure what it should be changed to.

Any help is greatly appreciated
Best Answer chosen by Jairus McClure
Andrew GAndrew G
Hi Jairus

trigger createtask on lead (after insert, after update) { 

in this way the trigger will fire the first time the record is saved (after insert) and any other time that it is updated (after update).

You could then segment your code using if statements example
​     if (Trigger.isInsert) {
        if (Trigger.isAfter) {
            // Process after insert
        } 
    }
    else if (Trigger.isUpdate) {
        // Process after update
    }
Regards
Andrew

All Answers

Andrew GAndrew G
Hi Jairus

trigger createtask on lead (after insert, after update) { 

in this way the trigger will fire the first time the record is saved (after insert) and any other time that it is updated (after update).

You could then segment your code using if statements example
​     if (Trigger.isInsert) {
        if (Trigger.isAfter) {
            // Process after insert
        } 
    }
    else if (Trigger.isUpdate) {
        // Process after update
    }
Regards
Andrew
This was selected as the best answer
Andrew GAndrew G
Additional note, if you are changing the Apex trigger from Insert to Update, then you will more than likely need to visit your test code and review the coverage.  I would assume that the existing test is for an insert only, not an update.

Regards
Andrew
Santosh Bompally 8Santosh Bompally 8
Hi Jarius, 

You have to create a boolean field on the object to execute this. try following this, 

Field : BooField (type = checkbox) 
Yourlogic : Companyamount > 5000$

Trigger 
 
trigger createtask on lead (after insert, after update){

If (Trigger.AfterInsert){

If(Yourlogic = true){
createtasktriggerhandler.myprocess();
BooField__c = true;
}

}

If (Trigger.AfterUpdate){

If(Yourlogic = true && BooField__c = false){
createtasktriggerhandler.myprocess();
BooField__c = true;
}
elseif(Yourlogic = false && BooField__c = true)
BooField__c = false;
}

// You can override this if you go with before insert and before update scenario
update lead; 

}

Handler
 
Public Class createtasktriggerhandler{

public static void myprocess(){

//Build your actions here 

{

}

Cheers