+ Start a Discussion
Ajitkumar Pradhan 9Ajitkumar Pradhan 9 

Trigger Check(Correct or not)

trigger trigMapFields on Lead (before update) {

    Map<Home_Phone,String> leadHome = new Map<Home_Phone,String>(); // Map of the converted Contact phone and the Lead phone

    for(Lead lead : Trigger.new) {
        if (lead.IsConverted) {
            leadStatus.put(lead.ConvertedContactHome_Phone,lead.Home_Phone);
        }
    }
    List<Contact> conContacts = [select Phone from Contact WHERE Contact.Home_Phone IN :leadHome.keySet()];
    for ( Contact c : conContacts) {
        c.Phone = leadHome.set(Home_Phone);
    }
    update conContacts;
}

is this the correct trigger? can anyone correct me if i am wrong?
Best Answer chosen by Ajitkumar Pradhan 9
v varaprasadv varaprasad
Hi Ajith,

Please check once following code.
 
trigger trigMapFields on Lead (before update) {

    Map<Id,String> leadStatus = new Map<Id,String>(); // Map of the converted Contact ID and the Lead Status

    for(Lead lead : Trigger.new) {
        if (lead.IsConverted) {
            leadStatus.put(lead.ConvertedContactId,lead.Home_Phone__c);
        }
    }
    List<Contact> conContacts = [select Id,phone from Contact WHERE Id IN :leadStatus.keySet()];
    for ( Contact c : conContacts) {
        c.phone = leadStatus.get(c.Id);
    }
    update conContacts;
}

Note: I have not tested above code.please let me know in case of any issues.

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com


 

All Answers

v varaprasadv varaprasad
Hi Ajithkumar,

Your trigger is wrong.Please let me know your requirement clearly.So that we can help you.



Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
Ajitkumar Pradhan 9Ajitkumar Pradhan 9
Hi,

I have a custom field Home Phone both in Lead and Contact. But when lead is converted Lead Home_Phone should be saved in standard Field Phone of Contact.
Could you help me to write a trigger,
 
v varaprasadv varaprasad
Hi Ajith,

Please check once following code.
 
trigger trigMapFields on Lead (before update) {

    Map<Id,String> leadStatus = new Map<Id,String>(); // Map of the converted Contact ID and the Lead Status

    for(Lead lead : Trigger.new) {
        if (lead.IsConverted) {
            leadStatus.put(lead.ConvertedContactId,lead.Home_Phone__c);
        }
    }
    List<Contact> conContacts = [select Id,phone from Contact WHERE Id IN :leadStatus.keySet()];
    for ( Contact c : conContacts) {
        c.phone = leadStatus.get(c.Id);
    }
    update conContacts;
}

Note: I have not tested above code.please let me know in case of any issues.

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com


 
This was selected as the best answer
Ajitkumar Pradhan 9Ajitkumar Pradhan 9
Hi,

Thanks for the code.
What will be the test class for this.

Regards,
Ajit