You need to sign in to do that
Don't have an account?
akash
Help me to write apex handler and test class for this trigger
Create an Apex trigger for Opportunity that adds a task to any opportunity set to 'Closed Won'
trigger ClosedOpportunityTrigger on Opportunity(after insert, after update) {
List<Task> oppList = new List<Task>();
for (Opportunity a : [SELECT Id,StageName,(SELECT WhatId,Subject FROM Tasks) FROM Opportunity
WHERE Id IN :Trigger.New AND StageName LIKE '%Closed Won%']) {
oppList.add(new Task( WhatId=a.Id, Subject='Follow Up Test Task'));
}
if (oppList.size() > 0) {
insert oppList;
}
}
trigger ClosedOpportunityTrigger on Opportunity(after insert, after update) {
List<Task> oppList = new List<Task>();
for (Opportunity a : [SELECT Id,StageName,(SELECT WhatId,Subject FROM Tasks) FROM Opportunity
WHERE Id IN :Trigger.New AND StageName LIKE '%Closed Won%']) {
oppList.add(new Task( WhatId=a.Id, Subject='Follow Up Test Task'));
}
if (oppList.size() > 0) {
insert oppList;
}
}
Try this code, this should work.
Can you try the below test class. It gives 100% code coverage
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
As this seems to other question. Can you post it as new question so it avoids confusion.
Thanks,
You can use this test class for this trigger.
Please mark it as best answer if it helps.
Thanks