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
Connor ReillyConnor Reilly 

Auto-converting leads and assigning opp, account, and contact as the same owner

I have a trigger that is auto converting a new lead to a contact, opportunity, and account from a form submission. The leads are auto assigned from hubspot round robin. When it converts the lead, it's assigning it to a new person. I'd like to have the contact account and opportunity owner match the lead owner that it was converted from. below is the trigger currently. Any help would be appreciated. 
trigger ContactTrigger on Contact (after update) {
    if(trigger.isAfter) {
        List<Contact> contacts = trigger.new;
        
        Set<Id> acctIds = new Set<Id>();
        for(Contact contact : contacts) {
            acctIds.add(contact.AccountId);
        }
        List<Account> accounts = [Select Id, Name From Account Where Id in: acctIds];
        List<Opportunity> opportunities = new List<Opportunity>();
        User marketingUser = [Select Id, Name From User Where Name='Marketing' Limit 1];
        if(trigger.isInsert) {
            for(Integer i = 0; i < contacts.size(); i++) {
                if(!String.isEmpty(contacts[i].Which_Manuf_Service_Shall_We_Quot__c)) {
                    opportunities.add(createOpportunity(contacts[i], marketingUser, accounts));
                }
            }
        }
        if(trigger.isUpdate) {
            List<Contact> oldContacts = trigger.old;
            for(Integer i = 0; i < contacts.size(); i++) {
                if(!String.isEmpty(contacts[i].Which_Manuf_Service_Shall_We_Quot__c) && (oldContacts[i].Which_Manuf_Service_Shall_We_Quot__c != contacts[i].Which_Manuf_Service_Shall_We_Quot__c)) {
                    opportunities.add(createOpportunity(contacts[i], marketingUser, accounts));
                }
    		}
        }
        insert opportunities;
    }
    
    public Opportunity createOpportunity(Contact contact, User marketingUser, List<Account> accounts) {
        Date today = System.today();
        String tech = null;
        List<String> services = new List<String> {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'};
        List<String> serviceAbbreviations = new List<String> {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'I','j', 'k', 'l'};    
            for(Integer i = 0; i < services.size(); i++) {
                if(contact.Which_Manuf_Service_Shall_We_Quot__c.equals(services[i])) {
                    tech = serviceAbbreviations[i];
                }
            }
        tech = String.isEmpty(tech) ? 'Other' : tech;
        String accountName = '';
        for(Account account : accounts) {
            if(account.Id == contact.AccountId) {
                accountName = account.Name;
            }
        }
        System.debug('Contact: ' + contact);
        System.debug('Contact Account: ' + contact.Account);
        System.debug('Account Name' + accountName);
        Opportunity opp = new Opportunity();
                    opp.ContactId = contact.Id;
                    opp.AccountId = contact.AccountId;
                    opp.CreatedById = marketingUser.Id;
                    opp.StageName = 'Quoting';
                    opp.Name = 'RFQ - ' + accountName + ' (' + tech + ')';
                    opp.CloseDate = today.addDays(30);
        return opp;
    }
}

 
Danish HodaDanish Hoda
Hi Connar,
If the owner was a user on Lead, on conversion, the same user will be the owner of the converted  Account/Contact/Opportunity if you don't have other logic for owner assignment.
If th user was Queue, then the user converting that lead would be assigned the owner of the converted Account/Contact/Opportunity.