You need to sign in to do that
Don't have an account?
Sankhadeep Biswas
Solution of " Create an Apex trigger for Opportunity that adds a task to any opportunity set to 'Closed Won'." giving error
For the above qus I wrote the below code :
trigger ClosedOpportunityTrigger on Opportunity (after insert,after update) {
List<Task> t1=new List<Task>();
for (opportunity a :[select ID,StageName from opportunity where StageName='Closed Won' and ID IN :Trigger.New])
{
t1.add(new Task(subject= 'Follow Up Test Task', WhatId=a.ID));
if(t1.size() >0 && t1.size()<=200)
{
insert t1;
}
}
}
But getting a error like :
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, trig_conts_opprty: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0 with id 0032800000QkI6aAAF; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] Trigger.trig_conts_opprty: line 10, column 1: []
Anyone please tell me the possible reason for the error ?
trigger ClosedOpportunityTrigger on Opportunity (after insert,after update) {
List<Task> t1=new List<Task>();
for (opportunity a :[select ID,StageName from opportunity where StageName='Closed Won' and ID IN :Trigger.New])
{
t1.add(new Task(subject= 'Follow Up Test Task', WhatId=a.ID));
if(t1.size() >0 && t1.size()<=200)
{
insert t1;
}
}
}
But getting a error like :
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, trig_conts_opprty: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0 with id 0032800000QkI6aAAF; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] Trigger.trig_conts_opprty: line 10, column 1: []
Anyone please tell me the possible reason for the error ?
trigger ClosedOpportunityTrigger on Opportunity (before insert) {
List<Task> listTask=new List<Task>();
for(Opportunity o : Trigger.new){
if(o.StageName.equals('Closed Won')){
Task t=new Task(Subject='Follow Up Test Task', WhatId=o.Id);
listTask.add(t);
}
}
insert listTask;
}
I wait help you.
trigger ClosedOpportunityTrigger on Opportunity (after insert,after update) {
List<Task> taskList=new List<Task>();
for(Opportunity Opp:Trigger.New){
if(Trigger.isInsert || Trigger.isUpdate)
if(opp.StageName=='Closed Won')
taskList.add(new task(Subject='Follow Up Test Task',
WhatId=opp.Id));
}
if(taskList.size()>0)
insert taskList;
}
Thanks.
The solution of " Create an Apex trigger for Opportunity that adds a task to any opportunity set to 'Closed Won' ":
Try the following code:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
list<task> TaskToadd = new list<task>();
for (Opportunity opp: trigger.new)
{
if (opp.StageName =='Closed Won'){
task t = new task();
t.whatID = opp.id;
t.Subject='Follow Up Test Task';
TaskToadd.add(t);
}
}
if(TaskToadd.size() > 0){
database.insert(TaskToadd);
}
}
Thank you
list<task> tasksList = new list<task>();
for(Opportunity opps : [Select Id from Opportunity where (
StageName='Closed Won' AND ID IN :Trigger.new
)]){
task t = new task();
t.Subject = 'Follow Up Test Task';
t.WhatId = opps.id;
tasksList.add(t);
}//for loop over..
if(tasksList.size()>1){
insert tasksList;
}
}
Insert or update any opportunity record stage field as 'Closed Won' and saved it.
It will automatically assign Follow up task with Subject to the opportunity.
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, We can't save this record because the “Opportunity Management” process failed. Give your Salesforce admin these details. The input parameter "oppProducts_Original" can't accept values of type null. Error ID: 1314204860-19430 (-876674778): []
Can someone help!
trigger ClosedOpportunityTrigger on Opportunity (after insert,after update) {
if(trigger.isInsert || trigger.isUpdate){
UpdateOpportunity.checkOpportunityStage(trigger.new);
}
}
TRIGGER HANDLER:
public class UpdateOpportunity{
public static void checkOpportunityStage(List<Opportunity> opportunityList){
if(opportunityList.size()>0){
List<Task> taskList=new List<Task>();
for(Opportunity op:opportunityList){
if(opp.StageName=='Closed Won'){
Task task1=new Task();
task1.Subject='Follow Up Test Task';
task1.WhatId=op.Id;
taskList.add(task1);
}
}
if(taskList.size()>0){
insert taskList;
}
}
}
trigger ClosedOpportunityTrigger on Opportunity (after insert,after update) {
list<Task> tasklist= new list<Task>();
for(opportunity a:[SELECT ID,stagename FROM opportunity WHERE ID IN:Trigger.New AND StageName='Closed Won']){
tasklist.add(new task(Subject='Follow Up Test Task',WhatId=a.id));
}
insert tasklist;
}
List<Task> opp=new List<Task>();
List<Opportunity> newOpportunity=[SELECT id,Name,StageName FROM Opportunity WHERE Id In :Trigger.new AND StageName='Closed won' ];
for(Opportunity op : newOpportunity)
{
opp.add(new Task(WhatId=op.Id,Subject='Follow Up Test Task'));
}
if(Trigger.isInsert)
{
insert opp;
}
else
{
update opp;
}
}
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
List<Task> taskList = new List<Task>();
for(Opportunity o: Trigger.New){
if(o.StageName == 'closed won'){
taskList.add(new Task(Subject='Follow Up Test Task', whatId=o.Id));
}
}
if(taskList.size() > 0){
insert tasklist;
}
}
I had the same issue, I went through logs, and I have seen the test that the challenge is executing, whichi is :
and the error was :
Discount_Percent__c is a field that I have added in a previous unit, which was required, so the test failed in fact in creating the opportinity not in inserting tasks, so checking this field to unrequired have solved my issue :)
Best regards