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
ebikiebiki 

compare th email and phone through trigger

Hi all,
I'm new to slaesforce can anyone help me to write a trigger to compare the email and phone number in leads with custom object while inserting, if bothe are same then I want to update the field in my custom object.

Thanks in advance.
Regards,
ebiz.
Best Answer chosen by ebiki
vishnu Rvishnu R
hi kiran,

try this code...

trigger leadDatavalidation on Lead (after insert) {

    list<string> Email = new    list<string> ();
    list<string> phone = new    list<string> ();
    list<shobithapp__Test_Object__c> test  = new    list<shobithapp__Test_Object__c> ();
    list<shobithapp__Test_Object__c> test1  = new    list<shobithapp__Test_Object__c> ();
    list<shobithapp__Test_Object__c> test2  = new    list<shobithapp__Test_Object__c> ();
 for(Lead L :trigger.new){
if (L.Email!= null){
Email.add(L.Email);
}
if (L.Phone!= null){
phone.add(L.Phone);
}
}
test1=[SELECT id,shobithapp__email__c,shobithapp__Phone__c,shobithapp__Record_Type__c   FROM shobithapp__Test_Object__c WHERE shobithapp__email__c IN : Email ];
test=[select shobithapp__Phone__c,shobithapp__email__c,shobithapp__Record_Type__c from shobithapp__Test_Object__c where Id IN: test1];
system.debug('////'+test);
for(shobithapp__Test_Object__c t :test){
if(test.size()!=null){
t.shobithapp__Record_Type__c='duplicate';
test2.add(t);
}
else
{
t.shobithapp__Record_Type__c='original';
test2.add(t);
}
}
update test2;   
}

In the last If condition ..you can change the condition upon your requirement....

hope this helps you..

thanks

Vishnu R

All Answers

vishnu Rvishnu R
hi kiran,

can you explain it further because i am not getting your requirement
 
Harish RamachandruniHarish Ramachandruni
Hi,


Add this code in custom object trigger .

 
trigger Duplicate_email on Customobject__c (before insert, before update) {

    list<string> Email = new    list<string> ();
   list<string> phone = new    list<string> ();

   list<string> id  = new    list<string> ();


    for(Customobject__c   cust  : system.trigger.new){

if (cust.Email__c != null){

Email.add(cust.Email__c);

}

if (cust.phone__c != null){

phone.add(cust.phone __c);

}


    }
 


If(Email.size() != null ){
    for(Lead lead : [SELECT Email FROM Lead WHERE Email IN : Email ]){

   id.add( Lead.id)

    } 


}
If(phone.size() != null ){

    for(Lead lead : [SELECT Email FROM Lead WHERE Email IN : Phone ]){

id.add( Lead.id)

    } 
}

If (id.size() != null )
{
cust.duplicate = true ;
}
}



Regards ,
Harish.R

 
ebikiebiki
Hi Vishnu,
I don't want to prevent the duplicate record, I want to verify the email and phone number of the Lead object is matching  with any of the custom object email and phone number. If they are matched then update the picklist filed in my custom object.
 
Harish RamachandruniHarish Ramachandruni
Hi,


So ucan modify last lines  below code 



If (id.size() != null )
{

cust.picklistapi  = 'enter your values ' ; 

}



Ask me any issue . 



Reagrds ,
Harish.R
ebikiebiki
getting error as Variable doesn't exit.
vishnu Rvishnu R
hi kiran,

try this code..it is working for me....

trigger Datavalidationtrigger on shobithapp__Test_Object__c (before insert,before update) {
    list<string> Email = new    list<string> ();
    list<string> phone = new    list<string> ();
    list<Id> ids  = new    list<Id> ();

 for(shobithapp__Test_Object__c   t :trigger.new){
if (t.shobithapp__email__c != null){    //shobithapp__email__c  ---field in test object api name(shobithapp__Test_Object__c ) 
Email.add(t.shobithapp__email__c);
}
if (t.shobithapp__Phone__c != null){
phone.add(t.shobithapp__Phone__c);   //shobithapp__Phone__c---field in test object api name(shobithapp__Test_Object__c )
}
If(Email.size() != null ){
for(Lead l : [SELECT Email FROM Lead WHERE Email IN : Email ]){
ids.add( l.id);

}
If(phone.size() != null ){
    for(Lead le : [SELECT phone FROM Lead WHERE phone IN : Phone ]){
ids.add( le.id);

}
If (ids.size() != null )
{
t.shobithapp__Record_Type__c='duplicate';
}
else{t.shobithapp__Record_Type__c='original';
}
}    
}


origal and duplicate are the picklist values (shobithapp__Record_Type__c is the piclist field api name)


mark it as best answer if it resolved your issue

thanks 
vishnu R
ebikiebiki
Hi Vishnu,
Thanks for sharing the Code.But
I want to verify the Lead email and phone  fields while inserting the Lead not the custom object, anyway i changed the code as per my scenario,  but i want to update the picklist field in mycustom object not in the same object. Is it possible ..to update the field value in other object?
vishnu Rvishnu R
Hi kiran, It is possible to update a field in other object. What I did is I have a created a custom object with name phone email and picklist fields . When I am entering some test data I wrote the trigger to check the data whether the data is from lead or not...so if it is from lead it will update the picklist value as duplicate while if it is not from lead then it will update as original.
ebikiebiki
Hi Vishnu,
You are saying that if data is matched with lead data then picklist in the custom object is updating with duplicate accoring to your scenario. But i want to update the picklist value in the custom objectwhile inserting lead data, if data is matched with custom object data then i have to update the vlaue in the custom object data.
vishnu Rvishnu R
hi kiran,

it is not my scenario..i just  did for you.
your scenario is to check whether the phone and email are there in leads or not....if it is there you have to update a piclist in a custom object..right?


 
ebikiebiki
NO, When creating the Lead if the lead Phoen and Lead Email are to be verified with custom object Email and Phone. If matched then update the picklist in custom object after inserting the lead record.
vishnu Rvishnu R
hi kiran,

try this code...

trigger leadDatavalidation on Lead (after insert) {

    list<string> Email = new    list<string> ();
    list<string> phone = new    list<string> ();
    list<shobithapp__Test_Object__c> test  = new    list<shobithapp__Test_Object__c> ();
    list<shobithapp__Test_Object__c> test1  = new    list<shobithapp__Test_Object__c> ();
    list<shobithapp__Test_Object__c> test2  = new    list<shobithapp__Test_Object__c> ();
 for(Lead L :trigger.new){
if (L.Email!= null){
Email.add(L.Email);
}
if (L.Phone!= null){
phone.add(L.Phone);
}
}
test1=[SELECT id,shobithapp__email__c,shobithapp__Phone__c,shobithapp__Record_Type__c   FROM shobithapp__Test_Object__c WHERE shobithapp__email__c IN : Email ];
test=[select shobithapp__Phone__c,shobithapp__email__c,shobithapp__Record_Type__c from shobithapp__Test_Object__c where Id IN: test1];
system.debug('////'+test);
for(shobithapp__Test_Object__c t :test){
if(test.size()!=null){
t.shobithapp__Record_Type__c='duplicate';
test2.add(t);
}
else
{
t.shobithapp__Record_Type__c='original';
test2.add(t);
}
}
update test2;   
}

In the last If condition ..you can change the condition upon your requirement....

hope this helps you..

thanks

Vishnu R
This was selected as the best answer
vishnu Rvishnu R
hi kiran.
check this code...
trigger LD_Check on Lead (After insert, After Update) {
    list<Lead_Duplicate__c> LDlist =new List<Lead_Duplicate__c>();
    LDlist =[Select Id,Name,LD_Email__c,LD_Phone__c,LD_Pick_List__c from Lead_Duplicate__c];
    for(Lead l : Trigger.New){
        for(Lead_Duplicate__c ld : LDlist){
            if(l.Email == ld.LD_Email__c && l.Phone == ld.LD_Phone__c ){
                ld.LD_Pick_List__c= 'LD true' ;   
            }
        } 
    }
    update LDlist;
}

thanks
Vishnu R
ebikiebiki
Hi Vishnu ,
Thats worked like charm.  Thank you.
And can we update the lookup filed through trigger...?
vishnu Rvishnu R
hi kiran,

yes you can update a look up field via trigger..
check this link https://developer.salesforce.com/forums/ForumsMain?id=9060G000000XZd4QAG

hope this helps you
Thanks
Vishnu R
ebikiebiki
H Vishnu,
Can you help hoe to validate teh phoen number field in the custom page befor redirecting the page.