-1

Below is my trigger which creates a lead and then it should create a task but it is not doing that

trigger LeadKeyFieldPopulated on Lead (before insert, after update) {
    List<Task> NewTask = new List<Task>();
    //Task t;
    for(Lead leadKeyField : Trigger.new) {

        //Lead newLeadKeyField = new Lead();
        leadKeyField.LastName = 'Test';
        leadKeyField.Company     = 'XYZ';
        //insert leadKeyField;
        Integer keyField = 0;
        Integer CountKeyField = 0;
        leadKeyField.Key_Fields_Populated__c = keyField;
        if (leadKeyField.FirstName != Null) {
            keyField = KeyField +1;
            CountKeyField = 1;
        }
        if (leadKeyField.LastName != Null) {
            keyField = KeyField+1;
            CountKeyField = 2;
        }
        if (leadKeyField.Email != Null) {
            keyField = KeyField+1;
            CountKeyField = 3;
        }
        if (leadKeyField.Phone != Null) {
            keyField = KeyField+1;
            CountKeyField = 4;
        }
        if (leadKeyField.Website != Null) {
            keyField = keyField+1;
            CountKeyField = 5;
        }
        if (leadKeyField.Title != Null) {
            keyField = keyField+1;
            CountKeyField = 6;
        }
        leadKeyField.Key_Fields_Populated__c = keyField;
        //if (leadKeyField.Key_Fields_Populated__c >=3) {
            Task t = new Task();
            //for(integer i=0;i<=leadKeyField.Key_Fields_Populated__c;i++) {
                //t.OwnerId = '0056g000001Sp3f';
                t.OwnerId = leadKeyField.OwnerId;
                t.WhoId   = leadKeyField.Id;
                t.WhatId  = leadKeyField.Id; 
                t.Description = leadKeyField.Description; 
                t.ActivityDate = Date.today(); 
                t.Status = 'Completed';
                //if (CountKeyField == 5) {
                   // system.debug('Verify the' + 'First name' + leadKeyField.FirstName);
                //}

                //t.Subject = system.debug('Verify the'+newLeadKeyField);
          NewTask.add(t);       
        //insert NewTask;
            }
           //NewTask.add(t);
           insert NewTask;
         }
  // }


//}
4
  • It seems like you're combining the logic and test data in the same record. What does a debug show keyField is before you start the task part? maybe that will help guide you to identify the issue. Dec 26, 2019 at 18:32
  • 5
  • 1
    @Vijay The issue is that you are trying to assign the Lead Id to the task's WhoId and WhatId field in the before insert context. You wont have a lead Id in the before insert. You have to move the creation of the task to After Insert context, then you would have the Lead Id available to assign to the task.
    – samdev
    Dec 26, 2019 at 18:49
  • how can I move the creation of the task to After insert context. Please advise Dec 27, 2019 at 0:21

1 Answer 1

1

You need to populate the Key_Fields_Populated__c field in the before trigger and create the task in the the after trigger. I found another issue where your code was running irrespective of context, so you were trying to update the lead fields on after update which was causing issues as the record would be read only. I have added the trigger context and also moved the logic of task creation to After context.

trigger LeadKeyFieldPopulated on Lead (before insert,after insert, before update, after update) {

if(Trigger.isBefore){
    if(Trigger.isInsert){
        for(Lead leadKeyField : Trigger.new) {
            Integer keyField = 0;
            Integer CountKeyField = 0;
            leadKeyField.Key_Fields_Populated__c = keyField;
            if (leadKeyField.FirstName != Null) {
                keyField = KeyField +1;
                CountKeyField = 1;
            }
            if (leadKeyField.LastName != Null) {
                keyField = KeyField+1;
                CountKeyField = 2;
            }
            if (leadKeyField.Email != Null) {
                keyField = KeyField+1;
                CountKeyField = 3;
            }
            if (leadKeyField.Phone != Null) {
                keyField = KeyField+1;
                CountKeyField = 4;
            }
            if (leadKeyField.Website != Null) {
                keyField = keyField+1;
                CountKeyField = 5;
            }
            if (leadKeyField.Title != Null) {
                keyField = keyField+1;
                CountKeyField = 6;
            }
            leadKeyField.Key_Fields_Populated__c = keyField;
        }
    }
    if(Trigger.isUpdate){
          //Add your before update logic here
    }
}
if(Trigger.isAfter){
    if(Trigger.isInsert){
        List<Task> NewTask = new List<Task>();
        for(Lead leadKeyField : Trigger.NewMap.values()) {
            // Add your criteria for task creation
            if (leadKeyField.Key_Fields_Populated__c >=2) {
                Task t = new Task();
                t.OwnerId = leadKeyField.OwnerId;
                t.WhoId   = leadKeyField.Id;
              //  This should not be lead.
              //  t.WhatId  = leadKeyField.Id; 
                t.Description = leadKeyField.Description; 
                t.ActivityDate = Date.today(); 
                t.Status = 'Completed';
                NewTask.add(t);       
            }
        }
        if(NewTask != Null && NewTask.size() > 0){
            insert NewTask;
        }
    }
    if(Trigger.isUpdate){
        List<Task> NewTask = new List<Task>();
        for(Lead leadKeyField : Trigger.new) {
            // Add your criteria for task creation
            if (Trigger.OldMap.get(leadKeyField.Id).Key_Fields_Populated__c != Trigger.NewMap.get(leadKeyField.Id).Key_Fields_Populated__c && leadKeyField.Key_Fields_Populated__c >=2) {    
                Task t = new Task();
                t.OwnerId = leadKeyField.OwnerId;
                t.WhoId   = leadKeyField.Id;
                //  t.WhatId  = leadKeyField.Id; 
                t.Description = leadKeyField.Description; 
                t.ActivityDate = Date.today(); 
                t.Status = 'Completed';
                NewTask.add(t);       
            }
        }
        if(NewTask != Null && NewTask.size() > 0){
            insert NewTask;
        }
    }
}}
4
  • Thanks for your help. It really helped me. Dec 27, 2019 at 17:22
  • I have also added my test class. It shows success but the coverage is coming only 42%. Please advise Dec 28, 2019 at 16:44
  • You need to ask this as a new question.
    – samdev
    Dec 28, 2019 at 17:09
  • Thanks samdev I have been able to solve my issue and now I am able to get 100% coverage but thanks again for helping me to correct my trigger. Dec 29, 2019 at 15:33

Not the answer you're looking for? Browse other questions tagged .