function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Unique TwoUnique Two 

Assistance required - apex trigger for existing email address

Hi everyone new here as the community has prompted me to ask this forum this question.


I have a custom object that the data is filled from an API connection.  So when that new custom record is created I'd like to check to see if the email address on that record matches to any lead, contact or account that we may already have in Salesforce.

If YES to a match it would be great to populate the URL of that record ID to the custom object record.  

Does anyone have sample apex code or advice or able to assist me please?
Tej PalTej Pal
Hi buddy, You can write before insert trigger on your custom object. Can you provide CustomObject API name & Field API name so can provide sample code.
Tej PalTej Pal
Hi buddy, Please check sample code.
trigger CustomTrigger on CustomTrigger__c(before insert){
    set<string> setEmail = new set<string>();
	for(CustomTrigger__c o: trigger.new){
		setEmail.add(o.email__c);
	}
	
	map<string, Contact> mapCon = new map<string, Contact>();
	for(Contact c: [select id, email from Contact where Email IN : setEmail]){
		mapCon.put(c.Email, c);
	}
    
	for(CustomTrigger__c o: trigger.new){
		if(mapCon.containsKey(o.email__c)){
			o.Link__c = mapCon.get(o.email__c);
		}
	}
}
If this answers your question mark Best Answer it as solution and then hit Like!
Unique TwoUnique Two
Thanks Tej, are the blue the custom object and custom fields? Where do I go to create this in Salesforce? 
Tej PalTej Pal
Yes those are custom fields & object.
From developer console.
User-added image
If this answers your question mark Best Answer it as solution and then hit Like!
Ajay K DubediAjay K Dubedi
Hi,
Using the code below you can achieve your requirement and populate the IDs of Leads and Contacts:-
trigger MatchEmailTrigger on CustomObj__c (before insert) {
            
            set<string> emailSet=new set<string>();
            for(CustomObj__c c:trigger.new)
            {
                emailSet.add(c.Email__c);
            }
            
            list<contact> clist=[select id from contact where email in:emailSet];
            list<lead> leadList=[select id from lead where email in:emailSet];
            
            if(clist.size()>0)
            {
                for(contact c1:clist)
            for(CustomObj__c c:trigger.new)
            {
                c.ExistingEmail__c=c1.id+'/'+c.ExistingEmail__c+'/';
            }
        }
     if(leadList.size()>0)
            {
                for(lead c1:leadList)
            for(CustomObj__c c:trigger.new)
            {
                c.ExistingEmail__c=c1.id+'/'+c.ExistingEmail__c+'/';
            }
        }
        }
Hope it helps,
Mark it as best to close the thread if you find it helpful.

Regards
Ajay
Unique TwoUnique Two
AJAY this looks great but I just don't know how to deploy it as I am newbie at doing a trigger....!!  Do i use process builder ? 
Unique TwoUnique Two
OkI deployed it but where does it reference the ID url to populate ?