简体   繁体   English

使用Apex触发器(Salesforce)自动将字段复制到查找字段

[英]Automatically Copy A Field To A Lookup Field With Apex Trigger (Salesforce)

I'm trying to find a way to automatically copy from one field to another field in Salesforce Contacts if at all possible please. 我正在尝试找到一种在所有可能的情况下自动从Salesforce联系人中的一个字段复制到另一个字段的方法。 (ie I have one field called " Contact: Mailing Zip/Postal Code " that I'm trying to automatically copy to another field called " Postcode_Lookup_c " .) (即,我有一个名为“ Contact: Mailing Zip/Postal Code ”的字段,试图将其自动复制到另一个名为“ Postcode_Lookup_c ”的字段。)

The purpose behind it is to automatically copy a field with a contact's postcode/ZIP code to a formula lookup field, that references a custom object I've created called Postcode_Lookup__c . 其背后的目的是自动将包含联系人邮政编码/邮政编码的字段复制到公式查找字段,该字段引用我创建的自定义对象Postcode_Lookup__c This basically links certain field relationships together, to which I've linked a list of local councils & regions in the state. 这基本上将某些领域关系联系在一起,我已经将该州的地方理事会和地区列表链接到了那里。

The idea being that a contact will be auto-populated with their relevant local council and region of the state, based on their mailing address postcode. 这样的想法是,联系人将根据其邮寄地址的邮政编码自动填充其相关的地方议会和州的区域。

Thanks so much for your help, Joe :-) 非常感谢您的帮助,乔:-)

For anyone else that's ever trying to do the same thing, here's the code that ended up accomplishing the task... 对于曾经尝试做同样事情的其他人,这是最终完成任务的代码...

trigger Postcode_Copy on Contact(before insert, before update) {

  Set < String > MailingPostalCodes = new Set < String > ();
  Map < String, Postcode_Lookup__c > postCode_Postal_map = new Map < String, Postcode_Lookup__c > ();

  for (Contact a: Trigger.new) {
    if (a.MailingPostalCode != null && a.MailingPostalCode.trim() != '')
      MailingPostalCodes.add(a.MailingPostalCode);
  }

  for (Postcode_Lookup__c postRec: [Select ID, Name from Postcode_Lookup__c where Name in: MailingPostalCodes]) {
    postCode_Postal_map.put(postRec.Name, postRec);
  }
  if (postCode_Postal_map.size() > 0) {
    for (Contact con: Trigger.new) {
      if (con.MailingPostalCode != null && con.MailingPostalCode.trim() != '') {
        if (postCode_Postal_map.containsKey(con.MailingPostalCode)) {
          con.Postcode_Lookup__c = postCode_Postal_map.get(con.MailingPostalCode).id;
        }
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM