【问题标题】:Apex Code to retrieve value like vlookup but in Salesforce?Apex 代码在 Salesforce 中检索类似 vlookup 的值?
【发布时间】:2016-05-05 03:46:05
【问题描述】:

所以我有两个自定义对象:Workload_Unit_Score__c 对象是一个映射表,每个行引用都有一个引用字段 (WLU_Combination_Value__c) 和一个值 (Score__c)。我们每天都会收到处理合同协议的请求。为每个请求在 Agreement_Title__c 对象上创建一条新记录。每个记录都根据其 WLU_Combination_Value__c 分配一个 Workload_Unit_Score__c。

我基本上想做一些类似于 excel vlookup 的事情——每次我们收到一个请求并创建一个新的 Agreement_Title__c 记录时,我想要一个触发器来获取 WLU_Combination_Value__c,从 Workload_Unit_Score__c 对象中检索一个 Score__c,并填充该值在 Workload_Unit_Score__c 字段中。这两个自定义对象不相关。以下是这些字段的摘要。

-Workload_Unit_Score__c 对象(排序为“定义”或 “参考”表)

  • 姓名
  • 杂项字段1
  • MiscField2
  • MiscField3
  • WLU_Combination_Value__c(连接 MiscField1 + MiscField2 + MiscField3 的公式字段)
  • 分数__c(为每个唯一的 WLU_Combination_Value__c 指定的分数)

-Agreement_Title__c 对象(合同协议)

  • 姓名
  • 杂项字段1
  • MiscField2
  • MiscField3
  • WLU_Combination_Value__c(连接 MiscField1 + MiscField2 + MiscField3 的公式字段)
  • Workload_Unit_Score__c(给每个唯一 WLU_Combination_Value__c 的分数)

我已经运行了下面的代码,但我得到“编译错误:期待右花括号,在第 22 行第 0 列找到''”但我认为代码可能存在其他问题,无法正常工作。

有人可以帮忙吗?有没有更简单的方法来做到这一点?

trigger updateWLUvalue on Agreement_Title__c (before insert) {

    Map<String,Agreement_Title__c[]> relatedScores = new Map<String, Agreement_Title__c[]>();

    for (Agreement_Title__c agmtt : trigger.new) {
        if(!relatedScores.containsKey(agmtt.WLU_Combination_Value__c)){

            relatedScores.put(agmtt.WLU_Combination_Value__c, new Agreement_Title__c[]{});
    }

    relatedScores.get(agmtt.WLU_Combination_Value__C).add(agmtt);

    for(Workload_Unit_Score__c wus : [Select Id, Score__c, WLU_Combination_Value__c 
                                          FROM Workload_Unit_Score__c 
                                          WHERE WLU_Combination_Value__c 
                                          IN : relatedScores.keySet()]){
        for(Agreement_Title__c agmtt2 : relatedScores.get(wus.WLU_Combination_Value__c)){
            agmtt.Workload_Unit_Score__c = wus.Score__c;
            }
        }
}

【问题讨论】:

    标签: for-loop triggers salesforce apex-code apex


    【解决方案1】:

    首先是末尾缺少的大括号。您的第一个 IF 语句没有结束。 补丁代码:

    trigger updateWLUvalue on Agreement_Title__c (before insert) {
    
        Map<String,Agreement_Title__c[]> relatedScores = new Map<String, Agreement_Title__c[]>();
    
        for (Agreement_Title__c agmtt : trigger.new) {
            if(!relatedScores.containsKey(agmtt.WLU_Combination_Value__c)) {
    
                relatedScores.put(agmtt.WLU_Combination_Value__c, new Agreement_Title__c[]{});
            }
        }
    
        relatedScores.get(agmtt.WLU_Combination_Value__C).add(agmtt);
    
        for(Workload_Unit_Score__c wus : [Select Id, Score__c, WLU_Combination_Value__c 
                                              FROM Workload_Unit_Score__c 
                                              WHERE WLU_Combination_Value__c 
                                              IN : relatedScores.keySet()])
        {
            for(Agreement_Title__c agmtt2 : relatedScores.get(wus.WLU_Combination_Value__c)) {
                agmtt.Workload_Unit_Score__c = wus.Score__c;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多