You need to sign in to do that
Don't have an account?
Stuck in Trailhead challenge for bulk trigger,Help please
trigger ClosedOpportunityTrigge on Opportunity (after update, after insert) { list<task> tasklist = new list<task>(); list<opportunity> opplist = new list<opportunity>(); opplist = [select id,stagename from opportunity where stagename = 'Closed Won' and id in :trigger.newmap.keyset()]; for (opportunity opp : opplist){ if(opp.stagename == 'Closed Won'){ task addtask = new task(); addtask.subject = 'Follow Up Test Task'; addtask.WhatId = opp.Id; tasklist.add(addtask); } } update tasklist; }
Hi,
The challenge is
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.
I feel the logic is right, but it is showing me error as :
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: FIELD_CUSTOM_VALIDATION_EXCEPTION, Account name is required: []
Is there something i am doing wrong, please point out if i made any mistakes.
also. i have just started with salesforce and apex 2 weeks ago, if i am missing anythin please give explaination, it would help me alot in understanding.
Thanks,
Nitin
1. The error is because you have a custom validation rule created for Opportunity on that org which mandates having an AccountId associated with an Opportunity while creating the Opportunity record. Since you do not have any Account recordId associated with an Opportunity the vaidation rule restricts you from creating the record. You can deactivate the validation rule and the trigger code should work.
I believe you must have been using this org for some other RnD however, it is recommended to have a fresh Dev org or a new Trailhead Playground created while completing the challenges.
2. In order to create Tasks you need to use the insert or Database.insert statements and not the update statement. Modify line # 16 in your code as follows
Please mark this as the best answer and upvote it if it helps you resovle your issue.
All Answers
Please try below trigger:
Just change trigger name : ClosedOpportunityTrigger
Thanks
Varaprasad
@ Varaprasad Sorry , its a typo in the above section, in the code it is actually the right spelling for the trigger.
its not working
@ Deepak i tried your code too but not working.its showing the same error.
1. The error is because you have a custom validation rule created for Opportunity on that org which mandates having an AccountId associated with an Opportunity while creating the Opportunity record. Since you do not have any Account recordId associated with an Opportunity the vaidation rule restricts you from creating the record. You can deactivate the validation rule and the trigger code should work.
I believe you must have been using this org for some other RnD however, it is recommended to have a fresh Dev org or a new Trailhead Playground created while completing the challenges.
2. In order to create Tasks you need to use the insert or Database.insert statements and not the update statement. Modify line # 16 in your code as follows
Please mark this as the best answer and upvote it if it helps you resovle your issue.