2018-05-07

Result: [OPERATION FAILED]: classes/LeadProcessor.cls: Invalid loop variable type expected SObject was Lead

來,先上error_msg:
Result: [OPERATION FAILED]: classes/LeadProcessor.cls: Invalid loop variable type expected SObject was Lead

然後是code:
    global void execute(Database.BatchableContext BC, List l_scope) {
List l_scope_new = new List();
for(Lead l : l_scope)
{
l.leadsource = 'Dreamforce';
l_scope_new.add(l);
count++;
}
update l_scope_new;
}

原因:compiler期待型態: Lead的變數,但是接到的是型態為sObject的變數的時候,就會跳這個error。



解法一:依然通用型變數把目標接進來,然後先使用Lead型態的變數去承接並轉換該目標,接著在區段中使用轉換後變數。
global void execute(Database.BatchableContext BC, List l_scope) {
List l_scope_workspace = l_scope;
List l_scope_new = new List();
for(Lead l : l_scope_workspace)
{
l.leadsource = 'Dreamforce';
l_scope_new.add(l);
count++;
}
update l_scope_new;
}


這個方法治標不治本:在此處沒有使用通用sObject型態的必要。所以先用通用型去接已確認型態,然後再自另一個指定型態的去轉接通用型,這些轉化太多餘。


解法二:一開始就直接用compiler有在吃的變數型態把它接進來。

global void execute(Database.BatchableContext BC, List l_scope) {
List l_scope_new = new List();
for(Lead l : l_scope)
{
l.leadsource = 'Dreamforce';
l_scope_new.add(l);
count++;
}
update l_scope_new;
}



沒有留言:

張貼留言

Check for typo before sending