salesforce比较标准obj和自定义对象并赋值

时间:2018-05-03 20:55:25

标签: triggers apex-code

我有自定义obj" A"和标准obj案例。案例标准obj查找自定义obj" A"。两个对象之间有一个名为Customer_ID__c的字段。我写了一个触发器(在插入之前,在更新之前)将案例记录与正确的现有自定义对象关联起来" A"记录是否" Case.Custom_Id__c"匹配Custom obj" A"中的那个。不幸的是,它没有发生,我不确定在哪里看。

trigger IAACaseRelateASAP on Case (before insert, before update) {

    Id recordtypes = [Select Id, name
                              From RecordType 
                              Where SobjectType = 'Case'
                              AND Name = 'I Buy'
                              LIMIT 1].Id;

    Set<String> casId = new Set<String>();
    for(Case cs : Trigger.new)
    {
      if(cs.RecordtypeId == recordtypes && cs.Type == 'Contact Me')
      {
        if(cs.custm_Obj_A_Name__lookupfield__c == null && (cs.Customer_ID__c != null || cs.Customer_ID__c !='0'))
        {
          casId.add(cs.Customer_ID__c);
        }
      }
    }
    system.debug('Case Set Ids' + casId);
    List<A__c> aList = [Select Customer_ID__c, Id
                                         From A__c
                                         Where Customer_ID__c IN: casId
                                         AND
                                         A__c != 'Provider'];
    System.Debug('equals' + aList);

    Map<String, A__c> aMapId = new Map<String, A__c>();                  
    for(A__c aAcct : aList)
    {
      aMapId.put(aAcct.Customer_ID__c, aAcct);
    } 
    for(Case cas : Trigger.new)
    {
      if(cas.RecordtypeId == recordtypes && cas.Type == 'Contact Me')
      {
        if(cas.custm_Obj_A_Name__lookupfield__c == null && (cas.Customer_ID__c != null || cas.Customer_ID__c !='0'))
        {
          if(aMapId.containsKey(cas.Customer_ID__c))
          {
            A__c aAcct = aMapId.get(cas.Customer_ID__c);
            System.Debug('Case IAA ASAP Account value: ' + asapAcct);
          }
        }
      }
    }  

}

1 个答案:

答案 0 :(得分:0)

最好在循环遍历您的案例时构建一组Customer_ID__c ID,以构建一个包含客户ID的案例列表,这样您就不必再次遍历整个新列表。一般来说,触发器还有其他几个问题,但我会忽略这些问题,只关注你的问题。认为您的问题是您实际上没有在此区域设置案例字段:

      if(aMapId.containsKey(cas.Customer_ID__c))
      {
        A__c aAcct = aMapId.get(cas.Customer_ID__c);
        System.Debug('Case IAA ASAP Account value: ' + asapAcct);
      }

应该是:

      if(aMapId.containsKey(cas.Customer_ID__c))
      {
        cas.custm_Obj_A_Name__lookupfield__c = aMapId.get(cas.Customer_ID__c).Id;
      }