I really appreciate your help. New to Stack Exchange and also coding
Use case: 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: I created a custom field Key_Fields_Populated__c on lead. Wrote a trigger which counts the fields populated. Now I want to create task for each key field populated. So in this case three tasks.

Where I am stuck is "the name of each task should be: verify the (key field name) field. 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){
                for(integer i = 0; i >= 3; i++){
                    Task T = New Task();
                    T.WhatId = L.Id;

1 Answer 1


First, Key_Fields_Populated__c:

This isn't required in order to handle the creation of the tasks. Additionally, it certainly doesn't need to be a field calculated in a trigger.

Even if you needed this field, it would be better as a formula field like:

IF(ISBLANK(FirstName), 0, 1) + IF(ISBLANK(LastName), 0, 1) + IF(ISBLANK(Website), 0, 1)

This adds 0 or 1 for each of the key fields based on whether they are non-null or null (strictly whether they are or are not blank, and there are some subtle differences).

Now the generation of Tasks:

All you need to do is check each key value in turn. Assuming this code will be invoked from a trigger (e.g. after create for the relevant object, where all the fields will always be populated for you), you simply need something like:

List<Task> tasks = new List<Task>();

for (Lead lead : leads) {
    if (lead.FirstName != null) {
        tasks.add(createTask(lead, 'FirstName'));

    if (lead.LastName != null) {
        tasks.add(createTask(lead, 'LastName'));

    if (lead.Website != null) {
        tasks.add(createTask(lead, 'Website'));

insert tasks;

The createTask method is something like:

private Task createTask(Lead lead, String fieldName) {
    return new Task(Name='Verify the ' + fieldName + ' field',
                    WhatId = lead.Id);

Now, there's a bunch of stuff I've ignored here, such as localization, and I didn't add anything clever around using a list of the "key fields" to make the code more configurable, but this should get you on the way. Remember to apply best practice and:

  • Do not inline code in your trigger, put it in a helper class. You'll find details via an internet search on this, just look for "apex trigger best practices".
  • Avoid using static methods; these cannot be mocked during unit testing. See StubProvider for some details.
  • thank you kindly for answering the question and advising me. Commented Jun 2, 2021 at 9:41
  • If you find an answer helpful, upvote it. If it solves your problem well mark it as best answer. That way people searching later can see whether an answer was a solution very easily.
    – Phil W
    Commented Jun 2, 2021 at 9:46

You must log in to answer this question.

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