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
dbimndbimn 

Scheduled Job runs in Sandbox but not in Production

Hello All, new to apex coding and running into a production issue.  Scheduled class runs fine in sandbox, deployed successfully with 100% code coverage, but now the job isn't executing in production.  Scheduled with the Schedule Apex tool in UI just as in Sandbox.  The job shows up in Scheduled Jobs but nothing happens, no errors, nothing to see in Apex Jobs.  Anyone see anything wrong with the following...

 

 This is the scheduler

 

1
2
3
4
5
6
global class AddParentCompanyScheduler implements Schedulable{
   global void execute(SchedulableContext SC) {
      AddParentCompany A = new AddParentCompany();
      A.getParents();
   }
}

 

this is the class and method the scheduler should execute.  imnaccount is a custom object.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public  class AddParentCompany {
//If an account has a parent account, any related opportunities or imnaccounts
//should have the same parent.

//Create a list of accounts with parents and loop through each record.
  public void getParents(){
    for (List<Account> acctsWithParents: [Select a.ParentId, 
      a.Id From Account a WHERE a.ParentID != null]){
        
        for(Account currAcct: acctsWithParents){
          
    // For every account in the resulting list, pull a list of opportunities.
    //Update the opportunities with the account's parent account.        
          for(List<Opportunity> oppsWithCurrAcct : [Select o.Parent_Company__c, 
                  o.AccountId, o.Id From Opportunity o 
                  WHERE o.AccountID = :currAcct.Id]){
            for(Opportunity currOpp: oppsWithCurrAcct){
              
              currOpp.Parent_Company__c = currAcct.ParentId;
              
            }
            Database.update (oppsWithCurrAcct);
          }
    // For every account in the resulting list, pull a list of imnaccounts.
    //Update the imnaccounts with the account's parent account.      
          for(List<IMNAccount__c> imnWithCurrAcct : [Select i.Parent_Company__c, 
                  i.Id, i.Company__c From IMNAccount__c i
                  WHERE i.Company__c = :currAcct.Id]){
            for(IMNAccount__c currImn: imnWithCurrAcct){
              
              currImn.Parent_Company__c = currAcct.ParentId;
              
            }
            Database.update (imnWithCurrAcct);
          }
          
          
        }
    }
  }
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
vishal@forcevishal@force

Try the below code:

 

 

public  class AddParentCompany {
//If an account has a parent account, any related opportunities or imnaccounts
//should have the same parent.

//Create a list of accounts with parents and loop through each record.
  public void getParents(){
Set<Id> setAccountIdsWithParents = new Set<Id>();
List<Opportunity> lstOppsToUpdate = new List<Opportunity>();
List<> lstIMNAccountsToUpdate = new List<>();
    for (Account acctsWithParents: [Select a.ParentId,
      a.Id From Account a WHERE a.ParentID != null]){
           setAccountIdsWithParents.add(acctsWithParents.Id);
      }
// For every account in the resulting list, pull a list of opportunities.
    //Update the opportunities with the account's parent account.        
          for(Opportunity oppsWithCurrAcct : [Select o.Parent_Company__c,
                  o.Account.ParentId, o.Id From Opportunity o
                  WHERE o.AccountID IN : setAccountIdsWithParents]){
                             oppsWithCurrAcct.Parent_Company__c = oppsWithCurrAcct.Account.ParentId;
                lstOppsToUpdate.add(oppsWithCurrAcct);
     }

if(lstOppsToUpdate.size() > 0)
    update lstOppsToUpdate;

  // For every account in the resulting list, pull a list of imnaccounts.
    //Update the imnaccounts with the account's parent account.      
          for( IMNAccount__c imnWithCurrAcct : [Select i.Parent_Company__c,        i.Company__r.ParentId,
                  i.Id, i.Company__c From IMNAccount__c i
                  WHERE i.Company__c IN : setAccountIdsWithParents]){
    imnWithCurrAcct.Parent_Company__c = imnWithCurrAcct.Company__r.ParentId;
               lstIMNAccountsToUpdate.add(imnWithCurrAcct);
}
if(lstIMNAccountsToUpdate.size() > 0)
    update lstIMNAccountsToUpdate;
}

 

Let me know if you face any issues or you have any questions regarding the code.

 

Also, for bulkification, never query or have DML statements inside a for loop.

All Answers

dbimndbimn

Figured out one problem was a validation rule.  Took care of that, but now I'm running into SOQL query limits, so I need to bulkify somehow.  Any suggestions are appreciated.

vishal@forcevishal@force

Try the below code:

 

 

public  class AddParentCompany {
//If an account has a parent account, any related opportunities or imnaccounts
//should have the same parent.

//Create a list of accounts with parents and loop through each record.
  public void getParents(){
Set<Id> setAccountIdsWithParents = new Set<Id>();
List<Opportunity> lstOppsToUpdate = new List<Opportunity>();
List<> lstIMNAccountsToUpdate = new List<>();
    for (Account acctsWithParents: [Select a.ParentId,
      a.Id From Account a WHERE a.ParentID != null]){
           setAccountIdsWithParents.add(acctsWithParents.Id);
      }
// For every account in the resulting list, pull a list of opportunities.
    //Update the opportunities with the account's parent account.        
          for(Opportunity oppsWithCurrAcct : [Select o.Parent_Company__c,
                  o.Account.ParentId, o.Id From Opportunity o
                  WHERE o.AccountID IN : setAccountIdsWithParents]){
                             oppsWithCurrAcct.Parent_Company__c = oppsWithCurrAcct.Account.ParentId;
                lstOppsToUpdate.add(oppsWithCurrAcct);
     }

if(lstOppsToUpdate.size() > 0)
    update lstOppsToUpdate;

  // For every account in the resulting list, pull a list of imnaccounts.
    //Update the imnaccounts with the account's parent account.      
          for( IMNAccount__c imnWithCurrAcct : [Select i.Parent_Company__c,        i.Company__r.ParentId,
                  i.Id, i.Company__c From IMNAccount__c i
                  WHERE i.Company__c IN : setAccountIdsWithParents]){
    imnWithCurrAcct.Parent_Company__c = imnWithCurrAcct.Company__r.ParentId;
               lstIMNAccountsToUpdate.add(imnWithCurrAcct);
}
if(lstIMNAccountsToUpdate.size() > 0)
    update lstIMNAccountsToUpdate;
}

 

Let me know if you face any issues or you have any questions regarding the code.

 

Also, for bulkification, never query or have DML statements inside a for loop.

This was selected as the best answer
dbimndbimn

Thank you, vishal.  I knew there was a cleaner way but was stuck in beginner mode, gah.  I'll give this a try and let you know the outcome.  Much obliged!

dbimndbimn

This did the trick.  Thanks for the help!

vishal@forcevishal@force

Happy to help.

 

Happy learning :-)