+ Start a Discussion
Arpitha GowdaArpitha Gowda 

how to write a trigger when lead status is changed to closed converted - it should Auto populate in the opportunity and custom object called new contact

Hello all
i am trying to design a trigger - when lead status is closed converted - The Fields  (Company,phone,email) in the lead object should be auto populated inside the fields of custom object new contact and opportunity objects (New_Contacts__c and Opportunity)

Cab anyone please help
Ajay K DubediAjay K Dubedi
Hi Ravi,
Try this code:
trigger TriggerOnLead on Lead (before Update) {

    if(trigger.isBefore && trigger.isUpdate) {
public class TriggerOnLead_handler {
    public static void checkLeadStatus(List<Lead> leadList) {
        try {
            List<New_Contacts__c > newConlist = new List<New_Contacts__c >();
            List<Opportunity> oppList = new List<Opportunity>();
            if(leadList.size() > 0) {
                for(Lead l : leadList) {
                    if (l.Status == 'Closed-Converted') {
                        New_Contacts__c obj = new New_Contacts__c();
                        Opportunity opp = new Opportunity();
                        obj.Name = 'Test';
                        obj.Company__c = l.Company;
                        obj.Phone__c = l.Phone;
                        obj.Email__c = l.Email;
                        //All required field.
                        opp.Name = 'Test';
                        opp.CloseDate = System.today();
                        opp.StageName = 'Closed Won';
                        opp.Company__c = l.Company;
                        opp.Phone__c = l.Phone;
                        opp.Email__c = l.Email;
                    insert newConlist;
                    insert oppList;
        } catch (Exception ex) {
                system.debug('Exception---ofLine--->' + ex.getLineNumber() + ex.getMessage());

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Ajay Dubedi