Skip to main content Join us at TDX in San Francisco or on Salesforce+ on March 5-6 for the Developer Conference for the AI Agent Era. Register now.

#Trailhead Challenges2,391 discussing

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

We tried to insert Opportunity records as part of the challenge check, but the insert failed. Error: thException: OPP_INSERT | System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ClosedOpportunityTrigger: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0 with id 00TWU0000002p852AA; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] Trigger.ClosedOpportunityTrigger: line 13, column 1: []

 

I get this error on checking the Bulk Apex Trigger challenge in the Apex Triggers Badge. Reading the error, it seems the test script is inserting an opportunity with Id. Anyone else getting this error? My trigger is inserting tasks without any Id and works like a charm, even on newly created opportunity.

 

#Trailhead Challenges  #Trailhead

11 answers
  1. Jan 23, 2024, 3:31 PM

    Hi, @Yvonne Riemeijer

     

    Nothing to worry about, everything is fine. I also try to relax on weekends, but weekdays are busy with work.

    So, thank you for the provided trigger code. I ran it in my system and encountered a similar error.Hi, Nothing to worry about, everything is fine. I also try to relax on weekends, but weekdays are busy with work.So, thank you for the provided trigger code.I made some changes to it, and now everything is working. Please use this code, and the challenge will be completed.

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

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

    for(Opportunity oppty : Trigger.New) {

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

    Task nwTask = new Task();

    nwTask.Subject = 'Follow Up Test Task';

    nwTask.WhatId = oppty.Id;

    tasksToCreate.add(nwTask);

    }

    }

    if (!tasksToCreate.isEmpty()){

    insert tasksToCreate;

    }

    }

    And, by the way, here's where the mistake was:

    It was necessary to simply write outside the loop:1.jpg2.jpg

    It was really interesting =)

     

    Sincerely,

    Mykhailo Vdovychenko

    Bringing Cloud Excellence with IBVCLOUD OÜ

0/9000
I faced issue  "We can't find an activated order that includes a 'FREE Bouquet' order item." in  Use Apex to Automate Business Processes  (Create and Test a Trigger) module but  i have already activated order include free bouquet.I faced issue
5 answers
  1. Khyati Mehta (Speridian Technologies) Forum Ambassador
    Jun 14, 2021, 4:57 AM
    Hi Apoorv,

     

    I am sure you would have followed the exact steps that are being provided in the Trailhead module. If not, then follow these steps:

     

    1)In the Path, click Activated.

     

    2)Click Mark as Current Status. You should see that the order has a new item, which has a unit price of $0. If you don’t see it, refresh your browser.

     

    And, if you have followed these steps, then try demarking the status and marking it again, it does work sometimes. Also, don't forget to refresh the page while demarking.

     

    Hope this helps!
0/9000

 Could not find the required Chatter post when inserting a Communications record with Number__c set to 1;  

 

#Trailhead Challenges

2 answers
  1. Feb 8, 9:32 PM

    My daughter helped me find the answer. When setting up the Trigger, I had selected under Condition Requirements:

     

    "All Conditions Are Met (AND) instead of

    Any Condition Is Met (OR)

    It was as simple as that. 

0/9000

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 answers
  1. Divya Chauhan (Kcloud Technologies) Forum Ambassador
    Jan 16, 6:26 AM

    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

When I check my challenge - I get the error message The trigger ClosedOpportunityTrigger is using events in the 'before' context. Make sure to use after insert and after update.

 

Here is my trigger below - So I don't understand why it's saying I'm using before context.  I've deleted this trigger once and re-made it only to receive the same error on submission 

 

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

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

//get Opp Ids from Closed Won Opps

for(Opportunity opp : [SELECT ID FROM Opportunity WHERE StageName = 'Closed Won' IN :Trigger.New]) {

//create follow up task against IDS that are stored in opp variable

taskToUpdate.add(new Task(Subject = 'Follow up Test Task',

WhatID=opp.ID));

}

insert taskToUpdate;

}

2 answers
0/9000

when i am copying the code given in the trailhead challenge to developer console, for this code i am getting the below error:

public class RestrictContactByName {

    trigger RestrictContactByName on Contact (before insert, before update) {

  //check contacts prior to insert or update for invalid data

  for(Contact c : Trigger.New) {

    if(c.LastName == 'INVALIDNAME') {

      //invalidname is invalid

      c.AddError('The Last Name "'+c.LastName+'" is not allowed for DML');

    }

  }

}

I am getting this error:Expecting '}' but was: 'trigger'

 

#Trailhead Challenges

0/9000

I get the following error when performing the npm install:

 

PS C:\Users\michael.allen\Documents\Dreamhouse> npm install

npm error code ERESOLVE

npm error ERESOLVE unable to resolve dependency tree

npm error

npm error While resolving: salesforce-app@1.0.0

npm error Found: eslint@9.12.0

npm error node_modules/eslint

npm error   dev eslint@"^9.9.1" from the root project

npm error   peer eslint@"^7.5.0 || ^8.0.0 || ^9.0.0" from @babel/eslint-parser@7.25.7

npm error   node_modules/@babel/eslint-parser

npm error     peer @babel/eslint-parser@"^7" from @lwc/eslint-plugin-lwc@1.8.2

npm error     node_modules/@lwc/eslint-plugin-lwc

npm error       dev @lwc/eslint-plugin-lwc@"^1.1.2" from the root project

npm error

npm error Could not resolve dependency:

npm error peer eslint@"^7 || ^8" from @lwc/eslint-plugin-lwc@1.8.2

npm error node_modules/@lwc/eslint-plugin-lwc

npm error   dev @lwc/eslint-plugin-lwc@"^1.1.2" from the root project

npm error

npm error Fix the upstream dependency conflict, or retry

npm error this command with --force or --legacy-peer-deps

npm error to accept an incorrect (and potentially broken) dependency resolution.

npm error

npm error

npm error For a full report see:

npm error C:\Users\michael.allen\AppData\Local\npm-cache\_logs\2024-10-07T22_05_30_595Z-eresolve-report.txt

npm error A complete log of this run can be found in: C:\Users\michael.allen\AppData\Local\npm-cache\_logs\2024-10-07T22_05_30_595Z-debug-0.log

 

My node version is v20.18.0

My npm version is 10.8.2

My package.json is as follows:

 

{

  "name": "salesforce-app",

  "private": true,

  "version": "1.0.0",

  "description": "Salesforce App",

  "scripts": {

    "lint": "eslint **/{aura,lwc}/**/*.js",

    "test": "npm run test:unit",

    "test:unit": "sfdx-lwc-jest",

    "test:unit:watch": "sfdx-lwc-jest --watch",

    "test:unit:debug": "sfdx-lwc-jest --debug",

    "test:unit:coverage": "sfdx-lwc-jest --coverage",

    "prettier": "prettier --write \"**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}\"",

    "prettier:verify": "prettier --check \"**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}\"",

    "postinstall": "husky install",

    "precommit": "lint-staged"

  },

  "devDependencies": {

    "@lwc/eslint-plugin-lwc": "^1.1.2",

    "@prettier/plugin-xml": "^3.2.2",

    "@salesforce/eslint-config-lwc": "^3.2.3",

    "@salesforce/eslint-plugin-aura": "^2.0.0",

    "@salesforce/eslint-plugin-lightning": "^1.0.0",

    "@salesforce/sfdx-lwc-jest": "^5.1.0",

    "eslint": "^9.9.1",

    "eslint-plugin-import": "^2.25.4",

    "eslint-plugin-jest": "^28.8.1",

    "husky": "^9.1.5",

    "lint-staged": "^15.1.0",

    "prettier": "^3.1.0",

    "prettier-plugin-apex": "^2.0.1"

  },

  "lint-staged": {

    "**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}": [

      "prettier --write"

    ],

    "**/{aura,lwc}/**/*.js": [

      "eslint"

    ]

  }

}

 

Any help would be appreciated

0/9000