function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Giancarlo AmatiGiancarlo Amati 

Exception error on Opty Trigger After Update

Hi Team,
I have a strange situation: I have an opportunity trigger like the one below:
 
trigger OppClosedTrigger on Opportunity (after update) {
    for (Opportunity opp : Trigger.New) {
        if (opp.IsClosed && opp.IsWon) {
            ProvisionBCAccount.provisionAccountForOpportunity(opp);       
        }
    }
}

and yesterday we were getting the following exception:
Error:Apex trigger OppClosedTrigger caused an unexpected exception, contact your administrator: OppClosedTrigger: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: ()

I don't understand what this might due to? Are the Opty Triggers async? or is there something else happening? 

Thank you for your help.

GC

Best Answer chosen by Giancarlo Amati
Abhishek BansalAbhishek Bansal
Hi Giancarlo,

You were getting this error because the following line returned no records:
String bcAccountId = [SELECT BCAccountId__c FROM BCAccount__c acct WHERE acct.Id = :opp.Deal_Flow_Trial_Account_ID__c].BCAccountId__c;

When your trigger executed on that day there were no matching account records so it resulted in an error and the next day the opportunity record has the matching account so it doesn't throw any error.
I will advise you to change this line and add an if condtion which will check if there are any matching records than only assign the account id otherwise return garbage value as mentioned in your code which will never happen in the current scenario.

Let me know if you need any further help or information on this.

Thanks,
Abhishek Bansal.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Giancarlo,

Could you please share the code for ProvisionBCAccount.provisionAccountForOpportunity .

Also, the trigger code won't be async unless it is specified as batch or schedulable or if future method is called from the trigger you can check this link (https://trailhead.salesforce.com/en/content/learn/modules/asynchronous_apex)

Looking forward for your response.

Regards,
Anutej 
rohit rahulrohit rahul
Thank you very much for the nice share, i willl be super glad to participate in the lowes store discounts and login portal.
https://myloweslife.xyz/
Abhishek BansalAbhishek Bansal
Hi Giancarlo,

You were getting this error because the following line returned no records:
String bcAccountId = [SELECT BCAccountId__c FROM BCAccount__c acct WHERE acct.Id = :opp.Deal_Flow_Trial_Account_ID__c].BCAccountId__c;

When your trigger executed on that day there were no matching account records so it resulted in an error and the next day the opportunity record has the matching account so it doesn't throw any error.
I will advise you to change this line and add an if condtion which will check if there are any matching records than only assign the account id otherwise return garbage value as mentioned in your code which will never happen in the current scenario.

Let me know if you need any further help or information on this.

Thanks,
Abhishek Bansal.
This was selected as the best answer