+ Start a Discussion
Mounika BuddabathinaMounika Buddabathina 

Unexpected Error on Bulk Apex Triggers

Hello Community,
 I'm Working on the Bulk Apex Triggers Trailhead and its throwing me the following error.
Question:
Create an Apex trigger for Opportunity that adds a task to any opportunity set to 'Closed Won'.
To complete this challenge, you need to add a trigger for Opportunity. The trigger will add a task to any opportunity inserted or updated with the stage of 'Closed Won'. The task's subject must be 'Follow Up Test Task'.
The Apex trigger must be called 'ClosedOpportunityTrigger'
With 'ClosedOpportunityTrigger' active, if an opportunity is inserted or updated with a stage of 'Closed Won', it will have a task created with the subject 'Follow Up Test Task'.
To associate the task with the opportunity, fill the 'WhatId' field with the opportunity ID.
This challenge specifically tests 200 records in one operation.
Here is my code:
trigger ClosedOpportunityTrigger on Opportunity (after insert,after update) {
    List<task> tasktoInsert = new List<task>();
    for(Opportunity opp : Trigger.New){
        if(opp.stageName == 'Closed Won'){
        Task t = new task();
        t.subject = 'Follow Up Test Task';
         t.WhatId = 'opp.Id';
            tasktoInsert.add(t);
        }
    }
    if(tasktoInsert.size()>0) {
        insert tasktoInsert ;
    }
}

 Error:
Challenge Not yet complete... here's what's wrong: 
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ClosedOpportunityTrigger: execution of AfterInsert caused by: System.StringException: Invalid id: opp.Id Trigger.ClosedOpportunityTrigger: line 7, column 1: []

Hope you guys can help me.
Thanks in advance,
Mounika.
Best Answer chosen by Mounika Buddabathina
Sagar PatilSagar Patil
Hi Mounika

You are getting this error might be due to there is active process builder/trigger/workflow on Opportunity object. Please paste below URL and replace your Salesforce domain name and see which component is casuing this issue and deactivate it.

https://yourinstance.my.salesforce.com/designer/designer.apexp#Id=3011N000000lZMo

Reference links
https://success.salesforce.com/answers?id=9063A000000DapGQAS

Regards,
Sagar

All Answers

Sagar PatilSagar Patil
Hi Mounika,

Try below code.
 
trigger ClosedOpportunityTrigger on Opportunity (after insert,after update) {
    List<task> tasktoInsert = new List<task>();
    for(Opportunity opp : Trigger.New){
        if(opp.stageName == 'Closed Won'){
        Task t = new task();
        t.subject = 'Follow Up Test Task';
         t.WhatId = opp.Id;
            tasktoInsert.add(t);
        }
    }
    if(tasktoInsert.size()>0) {
        insert tasktoInsert ;
    }
}

Note: You have written opp.id in quotes in trigger, hence you are getting the error.

Kindly mark this answer as best answer if it solves your problem.

Regards,
Sagar
Mounika BuddabathinaMounika Buddabathina
Hi Sagar,
I tried doing that and it's showing me the following Error

Challenge Not yet complete... here's what's wrong: 
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_EXECUTE_FLOW_TRIGGER, The record couldn’t be saved because it failed to trigger a flow. A flow trigger failed to execute the flow with version ID 3011N000000lZMo. Flow error messages: An unhandled fault has occurred in this flow
An unhandled fault has occurred while processing the flow. Please contact your system administrator for more information. Contact your administrator for help.: []

I deactivated all the flows that i have created before!
Thanks,
Mounika.
Sagar PatilSagar Patil
Hi Mounika

You are getting this error might be due to there is active process builder/trigger/workflow on Opportunity object. Please paste below URL and replace your Salesforce domain name and see which component is casuing this issue and deactivate it.

https://yourinstance.my.salesforce.com/designer/designer.apexp#Id=3011N000000lZMo

Reference links
https://success.salesforce.com/answers?id=9063A000000DapGQAS

Regards,
Sagar
This was selected as the best answer
Mounika BuddabathinaMounika Buddabathina
Hi Sagar,
it worked! Thank you so much for your Assistance.

Regards,
Mounika.
Sagar PatilSagar Patil
Glad to know it worked :)
Michael PeddycordMichael Peddycord
Hey all. I'm getting the exact same error as Mounika "Error Occurred: This error occurred when the flow tried to create records: REQUIRED_FIELD_MISSING: Required fields are missing: [Name]." on the Bulk Apex Triggers Trailhead. Can anyone help me troubleshoot this issue?
Mounika BuddabathinaMounika Buddabathina
Hi Michael,
Did you tried it by deactivating he flow? If not deactivate the flow because the error states taht it occurred when the flow tried to create records.
Hope this is helpful.

Thanks,
Mounika.
Michael PeddycordMichael Peddycord
Which flow am I deactivating? If I deactivate "Renew Opportunity", I won't be able to use the flow in Process Builder.
Mounika BuddabathinaMounika Buddabathina
Hi Michael,
you can activate your flow after you passed this challenge.
Michael PeddycordMichael Peddycord
Mounika,

    That didn't work. I get the following error:

"Challenge Not yet complete... here's what's wrong: 
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_EXECUTE_FLOW_TRIGGER, The record couldn’t be saved because it failed to trigger a flow. A flow trigger failed to execute the flow with version ID 3016A000000M0aQ. Flow error messages: An unhandled fault has occurred in this flow
An unhandled fault has occurred while processing the flow. Please contact your system administrator for more information. Contact your administrator for help.: []"
Mounika BuddabathinaMounika Buddabathina
Hi Michael,
You might get this error if the SObject (that you're inserting ) has an active Process Builder that expects certain fields to have values and those field values are NOT supplied before inserting them.
Please paste below URL and replace your Salesforce domain name and see which component is casuing this issue and deactivate it.

https://yourinstance.my.salesforce.com/designer/designer.apexp#Id=3016A000000M0aQ