+ Start a Discussion
Akshay ShrivastavaAkshay Shrivastava 

how to get data from contact

I have a custom object.
if Name field of Custom object is same as the name field of Contact object.then email of that contact should be in custom object email field
Best Answer chosen by Akshay Shrivastava
ANUTEJANUTEJ (Salesforce Developers) 
trigger custom_o_email on customobject__c(before insert)
{
if(trigger.isbefore && trigger.isinsert)
{
list<contact> clist= [select name,email,phone from contact];
map<string,string> emailstringmap=new map<string,string>();

for(contact c: clist)
{
if(emailstringmap.containskey(c.name)) {continue;}
else {emailstringmap.put(c.name,c);}
}

for(CustomObject__c temprec : trigger.new)
{
Contact tempC= emailstringmap.get(temprec.name);
temprec.email=tempC.email;
temprec.phone= tempC.phone;
}

}
}

I am assuming there are unique contact names and the duplicate rules are active under the contact object. Please make appropriate changes as needed.

Let me know if it helps you and close your query by marking it as the best answer so that it can help others in the future.  

Thanks.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Akshay,

You can have a trigger on the custom object and implement it like below:
 
trigger custom_o_email on customobject__c(before insert)
{
if(trigger.isbefore && trigger.isinsert)
{
list<contact> clist= [select name,email from contact];
map<string,string> emailstringmap=new map<string,string>();

for(contact c: clist)
{
if(emailstringmap.containskey(c.name)) {continue;}
else {emailstringmap.put(c.name,c.email);}
}

for(CustomObject__c temprec : trigger.new)
{
String emailC= emailstringmap.get(temprec.name);
temprec.email=emailC;
}

}
}

Please note you need to modify it to fit your needs and you need to modify names as present in your org.

Additionally, I would suggest you to have a look at the best practices as well: https://developer.salesforce.com/forums/?id=906F0000000DBl8IAG

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Akshay ShrivastavaAkshay Shrivastava
if I also want Phone No.
then
Suraj Tripathi 47Suraj Tripathi 47
Hi Akshay Shrivastava,

If two contacts have the same name then out of 2 which contact email should be populated in the email field of the custom object?


Thanks & Regards
Suraj Tripathi
ANUTEJANUTEJ (Salesforce Developers) 
trigger custom_o_email on customobject__c(before insert)
{
if(trigger.isbefore && trigger.isinsert)
{
list<contact> clist= [select name,email,phone from contact];
map<string,string> emailstringmap=new map<string,string>();

for(contact c: clist)
{
if(emailstringmap.containskey(c.name)) {continue;}
else {emailstringmap.put(c.name,c);}
}

for(CustomObject__c temprec : trigger.new)
{
Contact tempC= emailstringmap.get(temprec.name);
temprec.email=tempC.email;
temprec.phone= tempC.phone;
}

}
}

I am assuming there are unique contact names and the duplicate rules are active under the contact object. Please make appropriate changes as needed.

Let me know if it helps you and close your query by marking it as the best answer so that it can help others in the future.  

Thanks.
This was selected as the best answer