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
Chirag Verma 13Chirag Verma 13 

How to trigger a Task on lead when two fields are not matching of object which is related to Lead

Hi,
There is a custom object called Cart_c, and it is related to Lead.
in this object i have 3 fields 
1) Latest Month, 2) Latest volume, 3)Last Volume.
so If  (lastest Month == jan)
{
check Lastest volume   (not equal to)  !=   Last volume;
}
then we need to create Task on Lead.

Please help me i am stuck up on this from morning.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Chirag,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
trigger CreateTaskOnLead on Cart__c (before insert, before update) {
    
    List<Task> tsk = new List<Task>();
    
    for(Cart__c ct : Trigger.new){
        if(ct.Latest_Month__c == 'Jan'){
            if(ct.Latest_Volume__c != ct.Last_Volume__c){
                Task t = new Task();
                t.OwnerId = ct.OwnerId;
                t.priority = 'Normal';
                t.Status = 'Not Started';
                t.Type = 'Action';
                t.WhoId = ct.Lead__c; // Lead__c is lookup field on Cart Object
                t.Subject = 'New Task - Test';
                t.ReminderDateTime = DateTime.now().addHours(8);
                tsk.add(t);
                System.debug('tsk--->> ' + tsk);
            }
        }
    }
    if(!tsk.IsEmpty()){
        INSERT tsk;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Chirag Verma 13Chirag Verma 13
Thanks Khan for your quick response.

But I need to write a trigger on Task object, whenever Cart__c fields are not matching, then new task should be create on lead