0

I am attempting to refresh an account list that I utilize within the execute function of a batch class. I am trying to do this by wrapping the query in a function. My purpose in doing this is so I do not have to repeat the query code when it needs to be refreshed, but instead just execute the function.

I have tried public, private, returning and non-returning values. I receive an unexpected token when I try to state public/private and when leaving it as just the function, it's expecting ; like a variable declaration. What am I missing here?

global void execute(Database.BatchableContext BC, List < Customer_Obj__c > custObjList) {

    List < Account > accList = new List < Account > ();
    List < Contact > conList = new List < Contact > ();

    //QUERY SALESFORCE FOR MATCHES
    for (integer i = 0; i < custObjList.size(); i++) {

        List < Contact > relConList = new List < Contact > ();
        List < Account > relAccList = new List < Account > ();

        //FIND RELATED ACCOUNTS
       queryAccountMatches() {
            relAccList = [select Id, Name from Account where Name =: customObjList[i].Custom_Field__c];
       }

        //REFRESH relAccList
        queryAccountMatches();
        }

1 Answer 1

2

Unlike JavaScript, you can't put a function in a function. You'll need to move it "outside" of the execute method:

global void execute(Database.BatchableContext BC, List < Customer_Obj__c > custObjList) {

  List < Account > accList = new List < Account > ();
  List < Contact > conList = new List < Contact > ();

  //QUERY SALESFORCE FOR MATCHES
  for (integer i = 0; i < custObjList.size(); i++) {
    List < Contact > relConList = new List < Contact > ();
    //REFRESH relAccList
    List < Account > relAccList = queryAccountMatches( customObjList[i].Custom_Field__c);
  }
}

    //FIND RELATED ACCOUNTS
public static Account[] queryAccountMatches(String param) {
  return [select Id, Name from Account where Name =:param];
}

Note: of course, you shouldn't have queries inside of a loop if you can help it, but this answer is directly for the purpose of answering the question.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .