Skip to main content Únase a nosotros en TDX, en San Francisco, o en Salesforce+ los días 5 y 6 de marzo en la conferencia para desarrolladores sobre la era de agentes de IA. Registrarse ahora.

#Trailhead Challenges2410 debatiendo

Hands-on challenges are the “secret sauce” of Trailhead. Before searching for solutions to superbadge challenges, review the Salesforce Certification Program Agreement and Policies.

When I test my code through the "Execute Anonymous Window", it seems to work fine.

When I check the challenge, I get this error.  I don't know what it means.

Developer script exception from State of Wyoming : ClosedOpportunityTrigger : ClosedOpportunityTrigger: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0 with id 00Tbm000004HLlhEAG; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] Trigger.ClosedOpportunityTrigger: line 23, column 1

 

Challenge

Create a Bulk Apex trigger

Create a bulkified Apex trigger that adds a follow-up task to an opportunity if its stage is Closed Won. Fire the Apex trigger after inserting or updating an opportunity.

  • Create an Apex trigger:
    • Name: ClosedOpportunityTrigger
    • Object: Opportunity
    • Events: after insert and after update
    • Condition: Stage is Closed Won
    • Operation: Create a task:
      • Subject: Follow Up Test Task
      • WhatId: the opportunity ID (associates the task with the opportunity)
    • Bulkify the Apex trigger so that it can insert or update 200 or more opportunities

This is my code:

 

trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {

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

    

    List<Opportunity> toProcess = null;

    

    switch on Trigger.operationType {

        when AFTER_INSERT {

        // All inserted Opportunity will need the Opportunity

            toProcess = Trigger.New;

        }

        // Check if the status has changed to Closed Won

        when AFTER_UPDATE {

            toProcess = [SELECT Id,StageName FROM Opportunity

                         WHERE Id IN :Trigger.New];

        }

    }

    for (Opportunity opp : toProcess) {

        // Add a default task for this opportunity

        system.debug('Opportunity: ' + opp.Id + 'StageName: ' + opp.StageName);

        If (opp.StageName == 'Closed Won') {

          taskList.add(new Task(Subject = 'Follow Up Test Task', WhatId = opp.Id));

    }

    if (taskList.size() > 0) {

        system.debug(taskList);

        insert taskList;

    }

  }

}

 

#Trailhead Challenges

3 respuestas
  1. Divya Chauhan (Kcloud Technologies) Forum Ambassador
    16 ene, 6:26

    Hello@Marlene Meiring,

    • Make sure you have saved the code
    • try,

    trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {

    List<Task> taskList = new List <task>();

    for(Opportunity opp : Trigger.New){

    if(opp.StageName == 'Closed Won'){

    taskList.add(new Task(Subject = 'Follow Up Test Task', WhatId = opp.Id));

    }

    }

    if(taskList.size()>0){

    insert taskList;

    }

    }

0/9000