+ Start a Discussion
amritamrit 

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

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sfcksfck

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):

trigger Update_completed_activities_in_Opportunity on Task (after update) {
List<Opportunity> OppList;
Opportunity Opp;

for (Task T : trigger.new)
    {
        OppList = [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 (OppList.isEmpty())
        {
             return; // the task is not referring to an opportunity so exit the trigger
        }
        Opp = Opplist[0]; // the zero'th, i.e. first (only) opportunity in the list
        // ...... now carry on from here ...........
    

 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

sfcksfck

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):

trigger Update_completed_activities_in_Opportunity on Task (after update) {
List<Opportunity> OppList;
Opportunity Opp;

for (Task T : trigger.new)
    {
        OppList = [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 (OppList.isEmpty())
        {
             return; // the task is not referring to an opportunity so exit the trigger
        }
        Opp = Opplist[0]; // the zero'th, i.e. first (only) opportunity in the list
        // ...... now carry on from here ...........
    

 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.

This was selected as the best answer
Navatar_DbSupNavatar_DbSup

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;
}

amritamrit

Thanks

amritamrit

Thanks.Seems working fine now