1

I am going to write a trigger on the Associated Market object. This trigger will use the email address on the Associated Market record and look for a matching email address on the contact record. If a matching email address is found, then the Associated Market record Contact field should be populated with the contact found.

Below is my trigger:

trigger EmailMatchwithContactEmail on Associated_Market__c (before insert) {

   Set<Id> conEmailId = new Set<Id>();

   for(Associated_Market__c assMarket : trigger.New) {
      conEmailId.add(assMarket.Email__c);
   }

   List<contact> conList = [select Id, Email, Name from contact where Email in : conEmailId];

   Map<Id, contact> EmailMatchToContactMap = new Map<Id, contact>();

   for(Associated_Market__c assMarket : conList){
      EmailMatchToContactMap.put(assMarket.Email__c, assMarket);
   }

   for(Contact c : conList) {
       if(c.Email != null){
           c.Name = EmailMatchToContactMap.get(c.Email).id;
       }
   }
}
7
  • What error you are getting, it seems like compilation error in your trigger, please write specific problem. Commented Jul 29, 2015 at 10:14
  • Error Error: Compile Error: Incompatible value type Associated_Market__c for Map<Id,Contact> at line 14 column 9
    – Nihar
    Commented Jul 29, 2015 at 10:15
  • It should be for(Contact assMarket : conList){ Commented Jul 29, 2015 at 10:17
  • The Email can be repeat in Associated_Markert__c? The Associated_Market__c object has a Lookup to contact?
    – Torsen
    Commented Jul 29, 2015 at 10:20
  • Yes both object have same field. when someone insert Email address and that email address is already in contact's Email than that Associated object's contact lookup field will populated @Torsen
    – Nihar
    Commented Jul 29, 2015 at 10:24

4 Answers 4

0

I create a Map to store the Associated Market by Email. After that i get the existing contacts with the Email in the map's keyset. And I associate the contact's id to associated Market.

Code bellow:

trigger EmailMatchwithContactEmail on Associated_Market__c (before insert) {
    Map<String,Associated_Market__c[]> assotiactedMarketByEmail = new Map<String,Associated_Market__c[]>();

    for(Associated_Market__c assMarket : trigger.New) {
        if(!assotiactedMarketByEmail.containsKey(assMarket.Email__c)){
            assotiactedMarketByEmail.put(assMarket.Email__c, new Associated_Market__c[]{});
        }

        assotiactedMarketByEmail.get(assMarket.Email__c).add(assMarket);
    }

    for(Contact c : [select Id, Email, Name from contact where Email in : assotiactedMarketByEmail.keySet()]){
        for(Associated_Market__c assMarket : assotiactedMarketByEmail.get(c.Email)){
            assMarket.Contact__c = c.Id;
        }
    }
}
0

This is because you have used map in EmailMatchToContactMap. You should use Map when you are storing "Emails" in the key of the map.

Thanks!

1
  • you should use map<String,Contact>.
    – Ruchi
    Commented Jul 29, 2015 at 10:18
0

I agree with the Torsen answer but i have done by another trigger have a look into it

trigger EmailMatchwithContactEmail on Associated_Market__c (before insert) {

    set<string> assotiactedMarketByEmail = new set<string>();
    Map<String, String> contactEmailIdMap = new Map<String, String>();

    for(Associated_Market__c assMarket : trigger.New) {
        assotiactedMarketByEmail.add(assMarket.Email__c);
    }

    for(Contact c : [select Id, Email, Name from contact where Email in : assotiactedMarketByEmail Limit 10000]){
        contactEmailIdMap.put(c.Email,c.Id);
    }

    for(Associated_Market__c assMarket : trigger.New){
        assMarket.Contact__c = contactEmailIdMap.get(assMarket.Email__c);
    }
}
0
trigger EmailMatchwithContactEmail on Associated_Market__c(before insert) {
   Set<String> allEmails = new Set<String>();

   for(Associated_Market__c newAssociate : Trigger.new) {
      if(newAssociate.Email__c != null || newAssociate.Email__c != ' ') {
          allEmails.add(newAssociate.Email__c);
      }
   }

   List<Contact> lstContacts = [select Id, Name, Email from Contact where Email In :allEmails];

   Map<String, Contact> emailToContactMap = new Map<String, Contact>();
   for (Contact contactIterate : lstContacts) {
      emailToContactMap.put(contactIterate.Email, contactIterate);
  }

  for(Associated_Market__c newAssociate : Trigger.new) {
      if(newAssociate.Email__c != null || newAssociate.Email__c != ' ') {
          Contact contactVar = emailToContactMap.get(newAssociate.Email__c);
          if(contactVar != null) {
             newAssociate.Contact__c = contactVar.Id;
          }
      }
   }
}
1
  • can you explain how this code solves the question asked.. Commented Jul 31, 2015 at 11:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .