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
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:
trigger TriggerOnLead on Lead (before Update) {

    if(trigger.isBefore && trigger.isUpdate) {
        TriggerOnLead_handler.checkLeadStatus(trigger.new);
    }
}
handler:
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.
                        newConlist.add(obj);
                        
                        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;
                        oppList.add(opp);
                    }
                    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.
Thanks,
Ajay Dubedi