You need to sign in to do that
Don't have an account?
Zen Newman 16
Scheduled Apex Not Updating Records
Hello- I'm writing a scheduled Apex class that grabs all Accounts that are listed as 'Active' in a custom field, find's the most recent closed won Opportunity and passes a value from a lookup field on that opp back to another lookup on the Account. The class isn't flagging any problems for me, but throws an error when it tries to run upon being scheduled.
The error seems to indicate that it couldn't find any records to update. I verified that there are records that meet the criteria, and when I run the SOQL query in isolation it finds those records. When I try to debug it in the Execute Annonymous window, it also doesn't return any system.debug messages. I appreciate any help figuring out where I'm going wrong!
The error:
The error seems to indicate that it couldn't find any records to update. I verified that there are records that meet the criteria, and when I run the SOQL query in isolation it finds those records. When I try to debug it in the Execute Annonymous window, it also doesn't return any system.debug messages. I appreciate any help figuring out where I'm going wrong!
The error:
Sandbox Apex script unhandled exception by user/organization: 0053i000002hKdg/00D1g0000002qa7 Source organization: 00D3i000000ttF8 (null) Scheduled job 'Se Update2' threw unhandled exception. caused by: System.QueryException: List has no rows for assignment to SObject Class.UpdateSEOnAccount.execute: line 8, column 1The Apex Class:
global class UpdateSEOnAccount implements Schedulable{ global void execute (SchedulableContext ctx){ UpdateSE(); } public void UpdateSE(){ List<Account> Actlst = new list<account>(); for(Account a : [SELECT Id FROM Account WHERE Customer_Status__c = 'Active']){ a.Sales_Engineer__c = [SELECT Id, SE__c, AccountId FROM Opportunity WHERE AccountId = : a.id AND StageName = 'Closed Won' ORDER BY CloseDate DESC LIMIT 1].SE__c; system.debug('a is '+ a.Id); if(a.SE__c <> NULL){ Actlst.add(a); } system.debug('size of list is '+ Actlst.size()); } if(Actlst.size()>0){ update Actlst; } } }
try below code. I'm sure it will help you.
don't forget to mark it as best answer.
Thank you
All Answers
Please try the below Schedule Class:
Thanks,
Maharajan.C
try below code. I'm sure it will help you.
don't forget to mark it as best answer.
Thank you
I didn't copy any code. the query of @Zen Newman 16 was simple. he was doing query of opportunity and getting records but it's clear his way was wrong he should have written opportunity query in account so I just shown him a way.
this is common way.