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
Zen Newman 16Zen 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:
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 1
The 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;
        }
    }
}


 
Best Answer chosen by Zen Newman 16
ravi soniravi soni
hy,
try below code. I'm sure it will help you.
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,(SELECT Id, SE__c, AccountId FROM Opportunities WHERE StageName = 'Closed Won' ORDER BY CloseDate DESC LIMIT 1) FROM Account WHERE Customer_Status__c = 'Active']){    
              if(a.Opportunities.size() > 0){ 
Opportunity opp = a.Opportunities[0]; 
if(opp.SE__c != null){
 a.Sales_Engineer__c = opp.SE__c; 
Actlst.add(a);
 } 
}
}
        if(Actlst.size()>0){
            update Actlst;
        }
    }
}

don't forget to mark it as best answer.
Thank you

All Answers

Maharajan CMaharajan C
Hi Zen,

Please try the below Schedule 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,(SELECT Id, SE__c, AccountId FROM Opportunities WHERE StageName = 'Closed Won' ORDER BY CloseDate DESC LIMIT 1) FROM Account WHERE Customer_Status__c = 'Active']){    
           if(a.Opportunities.size() > 0){
				Opportunity opp = a.Opportunities[0];
				if(opp.SE__c != null){
					a.Sales_Engineer__c = opp.SE__c;
					Actlst.add(a);
				}
		   }
        }
        if(Actlst.size()>0){
            update Actlst;
        }
    }
}

Thanks,
Maharajan.C
ravi soniravi soni
hy,
try below code. I'm sure it will help you.
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,(SELECT Id, SE__c, AccountId FROM Opportunities WHERE StageName = 'Closed Won' ORDER BY CloseDate DESC LIMIT 1) FROM Account WHERE Customer_Status__c = 'Active']){    
              if(a.Opportunities.size() > 0){ 
Opportunity opp = a.Opportunities[0]; 
if(opp.SE__c != null){
 a.Sales_Engineer__c = opp.SE__c; 
Actlst.add(a);
 } 
}
}
        if(Actlst.size()>0){
            update Actlst;
        }
    }
}

don't forget to mark it as best answer.
Thank you
This was selected as the best answer
Maharajan CMaharajan C
@veer soni,  Don't simply copy paste others answres... We are spending some time to solve these querries...
ravi soniravi soni
Hy @Maharajan,
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.

​​​​​​​
Tim SidTim Sid
This iisue is really common and looking for the solid answer. (https://www.jcpkiosk.net/jcpenney-jtime-employee-login/)