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
rgard23rgard23 

Add user to Sales Team on Lead Conversion

This seems easy enough, but it has me stumped.

 

Use Case: Partners create leads in the partner portal. When the lead is ready to convert, our reps change themselves to the owner and convert it. I want to automatically add (since they forget all the time) the partner user to the sales team.

 

Problem: When I convert the lead (i have the trigger running after insert) I get an error saying that the record is locked.

 

It looks to me like Salesforce inserts the opportunity record, but locks it so its uneditable through the conversion. My trigger also depends on a custom field being mapped over, so that's why I run the trigger after insert so that the field will be mapped over.

 

Any thoughts anyone on how to get around this?

 

Here's my code:

 

 trigger addPartnerToSalesTeam on Opportunity (after insert) {
    
    /**
    * Need to get the opportunity and user
    */
    private Opportunity[] newOpty = new Opportunity[1];
    newOpty = Trigger.new;    

    //get userId of lead creator
    String leadCreatorId = newOpty[0].Lead_Creator_Id__c;

    //get lead Creator profile Id
    User leadCreator = [select profileId from user where id =:leadCreatorId];
    
    newOpty[0].description = leadCreator.ProfileId;//testing
    
    //run trigger if the owner of the converted lead was a partner user
    if(leadCreator.ProfileId == '00e20000000uCnj') {
        newOpty[0].description = newOpty[0].description + ' It works!!!' + ' ' + leadCreatorId; //for testing
        addtoSalesTeam.addUser(newOpty[0], leadCreatorId);

    }//end if

        
}//end addPartnerToSalesTeam trigger

 ________________________________________________________________

 public class addToSalesTeam {
    
    /**
    * To run this method, the trigger (or whatever calls it) must pass two variables:
    *
    * @Opportunity opty = the opportunity being shared
    * @User theUser = the user to be shared with
    *
    */
    public static void addUser (Opportunity opty, String userId) {
        
        OpportunityTeamMember optyMemb = new OpportunityTeamMember();

        optyMemb.OpportunityId = opty.Id;       

        optyMemb.UserId = userId;     

        Database.Saveresult sr = Database.insert(optyMemb);

    }//end addPartner method

}//end AddToSalesTeam

RAJIVRGRAJIVRG
Did you figure out a solution. We are kind of running into a similar situation and was curious?
rgard23rgard23

I did get it figured out. One thing is that you have to turn on a feature that allows you to run triggers on lead conversion. I can't remember if you just go to Customize->Lead->Settings to turn it on, or if you have to call support to enable that feature. But here is a link to Help & Training that talks about it: Enable Triggers on Lead Convert.

 

 

Jin D.ax1084Jin D.ax1084

I've put this together to add the Partner User (creator of the lead) to the Sales Team and the Partner related list, but for some reason I can only get 1 or the other to work... can anybody tell me what I'm doing wrong here?

 

trigger NewAddPartnerOnLeadConversion on Lead (after update) {
    
    /*creator id as key and LeadSource as value*/   
    Set<Id> creatorIds = new Set<Id>();
    String PARTNER_ROLE = 'VAR/Reseller';   
    for(Lead l : trigger.new){
        Lead  oldLead = System.Trigger.oldMap.get(l.Id);
        if(l.ConvertedAccountId != null && l.IsConverted && l.ConvertedOpportunityId != null 
           && oldLead != null && oldLead.CreatedById != null)
        { 
                System.debug('Old Lead Creator::' + oldLead.CreatedById);               
                creatorIds.add(oldLead.CreatedById);
        }       
    }
    
    
     List<User> userList = [select id , Name , accountId, UserType from user where id in :creatorIds and UserType = 'PowerPartner'];
     
                                                
    if(userList != null && userList.size()>0){
        Map<Id, User> usrMap = new Map<Id, User>();
        for(User u : userList){
           usrMap.put(u.Id , u);     
        }
        List<Partner> partnerList = new List<Partner>();
        for(Lead l : trigger.new){
            Lead  ol = System.Trigger.oldMap.get(l.Id);
            if(ol != null && usrMap.containsKey(ol.CreatedById) && usrMap.get(ol.CreatedById).accountId != null && usrMap.get(ol.CreatedById).UserType != null 
              && (usrMap.get(ol.CreatedById).UserType).equals('PowerPartner') && l.ConvertedAccountId != null && l.IsConverted && l.ConvertedOpportunityId != null)
            {                
                 Partner obj = new Partner(
                     AccountToId = usrMap.get(ol.CreatedById).accountId,
                     OpportunityId = l.ConvertedOpportunityId,
                     isPrimary = true,
                     Role = PARTNER_ROLE
                 );
                 partnerList.add(obj);
           OpportunityTeamMember optyMemb = new OpportunityTeamMember();
        
        //put acctId into optyMemb object
        optyMemb.OpportunityId = l.ConvertedOpportunityId;
        
        //put userId into optyMemb object
        optyMemb.UserId = ol.CreatedById;
        
        //Insert sharing record into database
        Database.Saveresult sr = Database.insert(optyMemb); }       
        }
        if(partnerList != null && partnerList.size()>0){
            try{ 
                insert partnerList ;      
            }catch(Exception e){
                for(Integer i = 0; i < e.getNumDml(); i++) {
                    System.debug('Message on DML:' + e.getDmlMessage(i)); 
                }   
            }
        }    
        }
}



Jin D.ax1084Jin D.ax1084

Nevermind, it works