Salesforce Apex类使用父级的id更新自定义对象查找字段

时间:2016-08-22 15:04:40

标签: salesforce apex

我是Apex的新手,我正在努力创建一个类来帮助我进行一些数据分析。我有来自第三方(transactions__C)的数据,该数据有一个字段(fin_acct_txt__c),它是指向另一个对象(fin_accounts__C)的指针。我想将来自fin_accounts__C的id的transactions__c更新到查询字段transactions__c.fin_acct__c中。 我想在一个类而不是触发器中执行此操作,因为每月将从第三方加载数千条记录。我认为批量做这件事会更有效率。 我的想法是我为transactions__c创建一个列表,为fin_accounts__c创建一个映射。使用fin_acct_txt__c = fin_accounts__c.name,我将能够获得fin_accounts__c.id并使用该数据更新transactions__c.fin_acct__c。 但是对Apex来说,新手似乎会给我带来一些我不确定如何解决的问题。 这是我迄今所做的一些副本:

public class updateTxnFinAcctID {

// Build map of financial accts since that is unique

map<string ,fin_acct__c> finAccts = new map<string, fin_acct__c>
    ([select id,name from fin_acct__c where name!=null]);

//Iterate through the map to find the id to update the transactions
{
for(fin_acct__c finAcct: finAccts.values())
{
    if (finAcct.name != Null)
    {
        finAccts.put(finAcct.name, finAcct);
    }

// Find all records in transaction__c where fin_acct__c is null 
//and the pointer is the name in the map

list<Transaction__c> txns =[
    select id,fin_acct_txt__c from Transaction__c where fin_acct__c = null
    and fin_acct_txt__c=:finaccts[0].name];

//create the list that will be used to update the transaction__c

list <Transaction__c> txnUpdate = new list <Transaction__c>();
{
//Find the id from fin_acct__c where name = fin_acct_txt__c
for (Transaction__c txn: txns){
    finacct[0].Id =txn.fin_acct__c;
    txnUpdate.add(txn);
}
//3. Update transaction with ID

}
}
   // if (txnUpdate.size()>0 { update txnUpdate};
    system.debug(txnUpdate.size());
}
}

我似乎陷入了厄运循环。我得到的错误是“表达式必须是列表类型:映射”指向列表txns = [...]。但由于这不是唯一的,它必须是列表。但我相信我在这里遇到了一些结构错误,这是一个更大问题的症状。

感谢。

1 个答案:

答案 0 :(得分:0)

我试图了解应该如何处理您的代码,我有一些提示,可能有助于解决您的问题:

1)在地图if (windows) { commandLine 'cmd', '/c', commandFromFile } else { commandLine 'sh', '-c', commandFromFile } 的值的第一个循环中,您真的不需要使用finAccts进行验证,因为您已经在SOQL查询(if (finAcct.name != Null))中添加了它。 / p>

2)这是一种不好的做法 - 将关键字映射到不同的实体(例如,ID和名称)。我的意思是当你向地图where name!=null查询fin_acct__c时,地图的键是fin_acct__c的ID。然后在第一个循环中,只使用其名称作为键,将相同的对象放在同一个映射中。如果你确实需要这样的带有名称的地图作为键,那么最好创建新地图并将数据放在那里。

3)您对Transaction__c对象执行SOQL查询到循环中。它可能是与SF限制相关的异常的原因(特别是如果您确定代码将处理大量数据)。最好收集列表中的所有fin_acct__c名称,并在循环中使用finAccts代替IN在条件中移出SOQL查询。

如果我理解正确=字段包含名称而不是ID,那么您的类应该类似于:

fin_acct_txt__c

它可能包含一些拼写错误,但这是一个常见的想法。

相关问题