需要优化代码以将代码映射到描述
我有一个文本字段,其中有分号分隔的代码。这些代码必须替换为描述。我有单独的地图,其中包含代码和说明。有一个触发器可以用其描述替换代码。数据将使用该字段中的数据加载器加载。恐怕它可能不适用于大量数据,因为我必须使用内部 for 循环。有什么办法可以在没有内部 for 循环的情况下实现这一目标?
public static void updateStatus(Map<Id,Account> oldMap,Map < Id, Account > newMap)
{
Map<String,String> DataMap = new Map<String,String>();
List<Data_Mapper__mdt> DataMapList = [select Salseforce_Value__c,External_Value__c from Data_Mapper__mdt where
active__c = true AND Field_API_Name__c= :CUSTOMFIELD_MASSTATUS AND
Object_API_Name__c= :OBJECT_ACCOUNT];
for(Data_Mapper__mdt dataMapRec: DataMapList){
DataMap.put(dataMapRec.External_Value__c,dataMapRec.Salseforce_Value__c);
}
for(Account objAcc : newMap.values())
{
if(objAcc.Status__c != ''){
String updatedDescription='';
List<String> delimitedList = objAcc.Status__c.split('; ');
for(String Code: delimitedList) {
updatedDescription = DataMap.get(Code);
}
objAcc.Status__c = updatedDescription;
}
I have a Text field that has semicolon separated codes. These code has to be replaced with the description. I have separate map that have code and description. There is a trigger that replace the code with their description. the data will loaded using the dataloader in this field. I am afraid, it might not work for large amount of data since I had to use inner for loops. Is there any way I can achieve this without inner for loops?
public static void updateStatus(Map<Id,Account> oldMap,Map < Id, Account > newMap)
{
Map<String,String> DataMap = new Map<String,String>();
List<Data_Mapper__mdt> DataMapList = [select Salseforce_Value__c,External_Value__c from Data_Mapper__mdt where
active__c = true AND Field_API_Name__c= :CUSTOMFIELD_MASSTATUS AND
Object_API_Name__c= :OBJECT_ACCOUNT];
for(Data_Mapper__mdt dataMapRec: DataMapList){
DataMap.put(dataMapRec.External_Value__c,dataMapRec.Salseforce_Value__c);
}
for(Account objAcc : newMap.values())
{
if(objAcc.Status__c != ''){
String updatedDescription='';
List<String> delimitedList = objAcc.Status__c.split('; ');
for(String Code: delimitedList) {
updatedDescription = DataMap.get(Code);
}
objAcc.Status__c = updatedDescription;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该没事的。您有一个基于地图的访问,就像词典一样,在循环外有一个查询。编写一个单位测试,该测试填充了近200个帐户(这就是在每个数据加载器迭代中调用触发器的方式)。如果您在该
status__c
中有数千个值,可能会有一些担忧,但是没有什么可以进行优化。但是我想问你三件事。
您写它的方式
更新description
将始终包含最后一个解码的值。您确定不想编写UpdatedDescription + = datamap.get(code) +';';
之类的东西,或者也许将它们添加到list&lt; string&gt;
然后调用字符串。看起来很奇怪。如果您真正想要第一个或最后一个元素 - 我会添加break;
或实际上只是访问拆分的最后一个元素(然后您是对的,您正在删除内部循环)。但是像这样写的是...怪异。您是否考虑过多次跑步。我的意思是,如果有工作流规则/流程/过程构建器 - 您可以再次输入此代码。而且由于您正在覆盖领域,所以我认为这会完全搞砸您。
It should be fine. You have a map-based access acting like a dictionary, you have a query outside of the loop. Write an unit test that populates close to 200 accounts (that's how the trigger will be called in every data loader iteration). There could be some concerns if you'd have thousands of values in that
Status__c
but there's not much that can be done to optimise it.But I want to ask you 3 things.
The way you wrote it the
updatedDescription
will always contain the last decoded value. Are you sure you didn't want to write something likeupdatedDescription += DataMap.get(Code) + ';';
or maybe add them to aList<String>
and then call String.join on it. It looks bit weird. If you truly want first or last element - I'd addbreak;
or really just access the last element of the split (and then you're right, you're removing the inner loop). But written like that this looks... weird.Have you thought about multiple runs. I mean if there's a workflow rule/flow/process builder - you might enter this code again. And because you're overwriting the field I think it'll completely screw you over.