0

我有两个全球课程。在 CreateAndModify 类中,我有一个执行函数,它有一个 for 循环,它调用我的另一个类的 processRecord 方法,即 CreateAndModifyProcessor。问题是在我的第一堂课中声明了一个列表集合,我无法在第二堂课中访问。有没有办法做到这一点,这样做甚至是一个好主意吗?

global class CreateAndModify implements
Database.Batchable<sObject>, Database.Stateful{

global void execute(
Database.BatchableContext BC,

List<sObject> listObj){

list <Account__c> inAcc = new list<Account__c>();

for (sObject lo : listObj){
      processor.processRecord(lo);
}

insert inAcc; // The list object which i 
              // am modifying in the processRecord function has to be added to       
              // the database. So, is it a good idea to add/populate the            
              // in other function of other class
}


global class CreateAndModifyProcessor {
global void processRecord(sObject listObj){
Unprocessed_Agreement__c temp = (Unprocessed_Agreement__c)listObj;

Account__c tempAcc = new Account__c();

inAcc.add(tempAcc); // This line throws an error
}
}
4

3 回答 3

3

您的列表inAcc是在 execute 方法中定义的,因此它的范围仅限于该方法,并且在它或该方法所属的类之外不可用。如果您绝对必须保留您已经布置的结构,那么为什么不将帐户列表作为参数传递给下面的处理方法呢?

global class CreateAndModify implements
    Database.Batchable<sObject>, Database.Stateful {

    global void execute(Database.BatchableContext BC, List<sObject> listObj) {

        list <Account__c> inAcc = new list<Account__c>();

        for (sObject lo : listObj) {
            processor.processRecord(lo, inAcc);
        }

        insert inAcc;
    }
}

global class CreateAndModifyProcessor {

    global void processRecord(sObject listObj, list<Account> listAcc) {
        Unprocessed_Agreement__c temp = (Unprocessed_Agreement__c)listObj;
        Account__c tempAcc = new Account__c();
        listAcc.add(tempAcc);
    }
}
于 2012-05-31T23:30:38.927 回答
0

list <Account__c> inAcc = new list<Account__c>();不是全局列表!

你应该把它移到执行方法之外,做这样的事情

global class CreateAndModify implements Database.Batchable<sObject>, Database.Stateful{ 

   global  list <Account__c> inAcc = new list<Account__c>();

  global void execute(Database.BatchableContext BC, List<sObject> listObj){

      for (sObject lo : listObj){
      processor.processRecord(lo);
       }

   insert inAcc; 
 }
}
于 2012-05-31T18:06:29.663 回答
0

您是否考虑过重新构建代码,以从 processRecord 返回帐户或帐户列表并以这种方式将其添加到 inAcc 中?它可能看起来像这样:

list <Account__c> inAcc = new list<Account__c>();

for (sObject lo : listObj){
    inAcc.addAll(processor.processRecord(lo));
}

insert inAcc;

global class CreateAndModifyProcessor {
    global list<Account> processRecord(sObject listObj){
        list<Account> accs = new list<Account>();
        Unprocessed_Agreement__c temp = (Unprocessed_Agreement__c)listObj;
        ... // some code that adds records to accs
        return accs;
    }
}
于 2012-05-31T18:38:25.427 回答