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
Inbox OutboxInbox Outbox 

Helper class to create three tasks from lead if lead's firstname, lastname and website are not null

Really appreaciate your input. 

If at least 3 key fields (firstname, lastname and website)are populated, create a new Task for each key field populated: 
The name of each Task should be: Verify the <<key field name>> field.
I can create one task but I am not sure how to create three task for each field. 

Not completed code: Below is my helper class:
public class keyFieldsPopulatedHelper {
   List <task> TaskLead= new list <task>();
    public static void keyFieldsPopulatedHelperMethod(list<Lead> LeadList){
        for(Lead L: LeadList){
            if(L.Key_Fields_Populated__c >=3){
                Task T= New Task();
                T.WhatId= L.Id;
            }
        }       
    }
}

 
Best Answer chosen by Inbox Outbox
Suraj Tripathi 47Suraj Tripathi 47
Hi,

Greetings!    

Please refer to the below link, I hope it would be helpful to you.

https://developer.salesforce.com/forums/?id=9062I000000IP9bQAG

If you find your Solution then mark this as the best answer. 

Thank you!

Regards,
Suraj Tripathi

All Answers

ShivankurShivankur (Salesforce Developers) 
Hi Jaampandu,

Please check out this similar requirement addressed on below thread:
https://developer.salesforce.com/forums/?id=9062I000000g5CBQAY

Hope information over it helps you to implement the same. Please mark as Best Answer so that it can help others in future.

Thanks.
Suraj Tripathi 47Suraj Tripathi 47
Hi,

Greetings!    

Please refer to the below link, I hope it would be helpful to you.

https://developer.salesforce.com/forums/?id=9062I000000IP9bQAG

If you find your Solution then mark this as the best answer. 

Thank you!

Regards,
Suraj Tripathi
This was selected as the best answer
sakhisakhi
Hi ,

Try below sample code from post
https://developer.salesforce.com/forums/?id=9062I000000IP9bQAG
trigger KeyFieldsPopulated on Lead (before insert, before update, after insert, after update) {


    if(Trigger.isBefore) {
        for (Lead l : Trigger.new) {
            // Step 1: Create list of required fields for count
            List<String> fieldsToCount = new List<String>();
            fieldsToCount.add(l.FirstName);
            fieldsToCount.add(l.LastName);
            fieldsToCount.add(l.Email);
            fieldsToCount.add(l.Phone);
            fieldsToCount.add(l.Website);
            fieldsToCount.add(l.Title);

            Integer total = 0;
            for (Integer i = 0; i < fieldsToCount.size(); i++) {
                String populatedFields = fieldsToCount.get(i);
                if (populatedFields != null) {
                    total++;
                }
                l.Key_Fields_Populated__c = total;
            }
        }
    }
    if(Trigger.isAfter){
        List<Task> taskList = new List<Task>();
        for(Lead l: Trigger.new){
            if (l.Key_Fields_Populated__c >= 3){
                for (String eachKeyField: fieldsToCount) {
                    Task t  = new Task();
                    t.Subject     = 'Verify ' + eachKeyField + ' field';
                    t.Status      = 'In Progress';
                    t.Priority    = 'High';
                    t.WhatId      = l.Id;
                    taskList.add(t);
                }
            }
        }
        if(taskList.size()>0){
            insert taskList;
        }
    }
}