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
Ajitkumar Pradhan 9Ajitkumar Pradhan 9 

write a trigger for lead convert to copy from one field to another

Best Answer chosen by Ajitkumar Pradhan 9
SandhyaSandhya (Salesforce Developers) 
Hi,

Refer below sample 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.Status);
        }
    }
    List<Contact> conContacts = [select Id from Contact WHERE Contact.Id IN :leadStatus.keySet()];
    for ( Contact c : conContacts) {
        c.Type__c = leadStatus.get(c.Id);
    }
    update conContacts;
}

Also refer below link for similar discussion.

https://developer.salesforce.com/forums/?id=906F00000008ve5IAA
 
Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
 
Best Regards
Sandhya
 
 

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi,

Refer below sample 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.Status);
        }
    }
    List<Contact> conContacts = [select Id from Contact WHERE Contact.Id IN :leadStatus.keySet()];
    for ( Contact c : conContacts) {
        c.Type__c = leadStatus.get(c.Id);
    }
    update conContacts;
}

Also refer below link for similar discussion.

https://developer.salesforce.com/forums/?id=906F00000008ve5IAA
 
Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
 
Best Regards
Sandhya
 
 
This was selected as the best answer
Ajitkumar Pradhan 9Ajitkumar Pradhan 9
Hi,
Much Appreciated.

I have a custom field Home Phone in Lead and also in Contact. But after converting it should go to standard field Phone.

How this will work in code?
could you suggest.

Regards
Ajit
Ajitkumar Pradhan 9Ajitkumar Pradhan 9
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?
Ajitkumar Pradhan 9Ajitkumar Pradhan 9
I am new to salesforce please help.