You need to sign in to do that
Don't have an account?
List has no rows for assignment to SObject in Task Trigger
Hi,
Im writing a tigger on task .Im trying to update the task related to opportunity but it showing error like this.
Apex trigger Update_completed_activities_in_Opportunity caused an unexpected exception, contact your administrator: Update_completed_activities_in_Opportunity: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.Update_completed_activities_in_Opportunity: line 26, column 1
trigger Update_completed_activities_in_Opportunity on Task (after update) { Opportunity O; /* List<Id> oId = new List<Id>(); for(Task T : Trigger.new) { oId.add(T.Whatid); } */ for (Task T : trigger.new) { O = [select id, Welcome_Call_Completed__c, Welcome_Kit_Dispatched__c, Agreement_Amount_Received__c, Construction_Agreement_Received__c, Sale_Agreement_Received__c from Opportunity where id =: T.Whatid]; if(T.Status == 'completed' && T.Subject == 'Welcome Call' && !O.Welcome_Call_Completed__c) { O.Welcome_Call_Completed__c = true; } if(T.Status == 'completed' && T.Subject == 'Dispatch Welcome Kit' && !O.Welcome_Kit_Dispatched__c) { O.Welcome_Kit_Dispatched__c = true; } if(T.Status == 'completed' && T.Subject == 'Collect Agreement Amount' && O.Agreement_Amount_Received__c == Null) { O.Agreement_Amount_Received__c = System.today(); } if(T.Status == 'completed' && T.Subject == 'To check physical receipt of Agreement Copies' && !O.Construction_Agreement_Received__c && !O.Sale_Agreement_Received__c) { O.Construction_Agreement_Received__c = true; O.Sale_Agreement_Received__c = true; } } update O; }
O = [select id, Welcome_Call_Completed__c, Welcome_Kit_Dispatched__c, Agreement_Amount_Received__c, Construction_Agreement_Received__c, Sale_Agreement_Received__c from Opportunity where id =: T.Whatid];
I understood it is because im not able to map task id with opportunity.How to map task id with opportunity.Im not so familiar in trigger .any one suggest me a solution for this asap
Thanks
If the WhatId on the task is pointing to an opportunity, then you should have no problem.
But if the trigger runs on a task where the WhatID is null or points to something other than an opportunity, you will get that error. You are saying "Give me the opportunity that is referenced by this Task", so if the Task is not referencing an opportunity it will break.
Possible solution (depends what you want to do, though):
Because it gets the results of the query out into a list, you can check if the list is empty before trying to assign it to an object.
All Answers
If the WhatId on the task is pointing to an opportunity, then you should have no problem.
But if the trigger runs on a task where the WhatID is null or points to something other than an opportunity, you will get that error. You are saying "Give me the opportunity that is referenced by this Task", so if the Task is not referencing an opportunity it will break.
Possible solution (depends what you want to do, though):
Because it gets the results of the query out into a list, you can check if the list is empty before trying to assign it to an object.
Hi,
Think you are getting this error because your SOQL return no record. Try the below code:
trigger Update_completed_activities_in_Opportunity on Task (after update)
{
Opportunity O;
/*
List<Id> oId = new List<Id>();
for(Task T : Trigger.new) {
oId.add(T.Whatid);
}
*/
for (Task T : trigger.new)
{
O = [select id,
Welcome_Call_Completed__c,
Welcome_Kit_Dispatched__c,
Agreement_Amount_Received__c,
Construction_Agreement_Received__c,
Sale_Agreement_Received__c from Opportunity where id =: T.Whatid];
if(O != null )
{
if(T.Status == 'completed' && T.Subject == 'Welcome Call' && !O.Welcome_Call_Completed__c)
{
O.Welcome_Call_Completed__c = true;
}
if(T.Status == 'completed' && T.Subject == 'Dispatch Welcome Kit' && !O.Welcome_Kit_Dispatched__c)
{
O.Welcome_Kit_Dispatched__c = true;
}
if(T.Status == 'completed' && T.Subject == 'Collect Agreement Amount' && O.Agreement_Amount_Received__c == Null)
{
O.Agreement_Amount_Received__c = System.today();
}
if(T.Status == 'completed' && T.Subject == 'To check physical receipt of Agreement Copies' && !O.Construction_Agreement_Received__c && !O.Sale_Agreement_Received__c)
{
O.Construction_Agreement_Received__c = true;
O.Sale_Agreement_Received__c = true;
}
}
}
update O;
}
Thanks
Thanks.Seems working fine now