对于其他任何试图做同样事情的人,这里是最终完成任务的代码......
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;
}
}
}
}
}