需要优化代码以将代码映射到描述

发布于 2025-01-18 01:42:45 字数 1230 浏览 1 评论 0原文

我有一个文本字段,其中有分号分隔的代码。这些代码必须替换为描述。我有单独的地图,其中包含代码和说明。有一个触发器可以用其描述替换代码。数据将使用该字段中的数据加载器加载。恐怕它可能不适用于大量数据,因为我必须使用内部 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界等同你 2025-01-25 01:42:45

应该没事的。您有一个基于地图的访问,就像词典一样,在循环外有一个查询。编写一个单位测试,该测试填充了近200个帐户(这就是在每个数据加载器迭代中调用触发器的方式)。如果您在该status__c中有数千个值,可能会有一些担忧,但是没有什么可以进行优化。

但是我想问你三件事。

  1. 您写它的方式更新description将始终包含最后一个解码的值。您确定不想编写UpdatedDescription + = datamap.get(code) +';';之类的东西,或者也许将它们添加到list&lt; string&gt;然后调用字符串。看起来很奇怪。如果您真正想要第一个或最后一个元素 - 我会添加break;或实际上只是访问拆分的最后一个元素(然后您是对的,您正在删除内部循环)。但是像这样写的是...怪异。

  2. 您是否考虑过多次跑步。我的意思是,如果有工作流规则/流程/过程构建器 - 您可以再次输入此代码。而且由于您正在覆盖领域,所以我认为这会完全搞砸您。

Map<String, String> mapping = new Map<String, String>{
    'one' => '1',
    'two' => '2',
    'three' => '3',
    '2' => 'lol'
};
String text = 'one;two';

List<String> temp = new List<String>();
for(String key : text.split(';')){
    temp.add(mapping.get(key));
}
text = String.join(temp, ';');
System.debug(text); // "1;2"

// Oh noo, a workflow caused my code to run again.
// Or user edited the account.

temp = new List<String>();
for(String key : text.split(';')){
    temp.add(mapping.get(key));
}
text = String.join(temp, ';');
System.debug(text); // "lol", some data was lost

// And again
temp = new List<String>();
for(String key : text.split(';')){
    temp.add(mapping.get(key));
}
text = String.join(temp, ';');
System.debug(text); // "", empty
  1. 您甚至确定需要此代码吗? Salesforce具有单独的选择列表标签(用户可见)和API值(已保存到数据库,在Apex中引用的数据库,验证规则...)。也许您根本不需要这种转变。也许您的公司应该研究翻译工作台。甚至完全抛弃此代码,并在调用数据加载器之前进行一些搜索替代,在某些真实的ETL工具(甚至MS Excel)中

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.

  1. The way you wrote it the updatedDescription will always contain the last decoded value. Are you sure you didn't want to write something like updatedDescription += DataMap.get(Code) + ';'; or maybe add them to a List<String> and then call String.join on it. It looks bit weird. If you truly want first or last element - I'd add break; 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.

  2. 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.

Map<String, String> mapping = new Map<String, String>{
    'one' => '1',
    'two' => '2',
    'three' => '3',
    '2' => 'lol'
};
String text = 'one;two';

List<String> temp = new List<String>();
for(String key : text.split(';')){
    temp.add(mapping.get(key));
}
text = String.join(temp, ';');
System.debug(text); // "1;2"

// Oh noo, a workflow caused my code to run again.
// Or user edited the account.

temp = new List<String>();
for(String key : text.split(';')){
    temp.add(mapping.get(key));
}
text = String.join(temp, ';');
System.debug(text); // "lol", some data was lost

// And again
temp = new List<String>();
for(String key : text.split(';')){
    temp.add(mapping.get(key));
}
text = String.join(temp, ';');
System.debug(text); // "", empty
  1. Are you even sure you need this code. Salesforce is perfectly fine with having separate picklist labels (what's visible to the user) and api values (what's saved to database, referenced in Apex, validation rules...). Maybe you don't need this transformation at all. Maybe your company should look into Translation Workbench. Or even ditch this code completely and do some search-replace before invoking data loader, in some real ETL tool (or even MS Excel)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文