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 Sales Team member on Lead Convert

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();
        
        //put acctId into optyMemb object
        optyMemb.OpportunityId = opty.Id;
        
        //put userId into optyMemb object
        optyMemb.UserId = userId;
        
        //Insert sharing record into database
        Database.Saveresult sr = Database.insert(optyMemb);

    }//end addPartner method

}//end AddToSalesTeam

Rajesh ShahRajesh Shah

Did you found a solution for this? I too am stuck at the same thing. Only difference being that I am inserting the Sales Team in Lead trigger which is excuted when the Lead is converted. In this case, it doesnt gives me error but doesnt inserts too.

rgard23rgard23

Yes. First, I found a permission that you have to enable. You can talk to support about allowing triggers to fire on lead convert. Once you do this, you will see some options under Setup-->Lead-->Settings related to triggers firing on lead convert.

 

After that you just need to write a trigger on leads when it updates and isConverted = true. Let me know if you are having trouble with the code.

Rajesh ShahRajesh Shah

I do have setting enabled as well as the my trigger is also on Lead Conversion. It still doesn't works. Heres an additional information. Normally for the leads I am converting, I am not the owner (but I am a system admin). In that case, it doesn't adds to the Sales team. However, once I changed the owner of the Lead to me and then converted it. And voila. it added the records to the sales team.

I am not sure how this works or how to use it in my code. If you have the idea, please help.

rgard23rgard23

Are you doing all of your logic in the trigger or do you have a helper class? If you have a helper class you might want to check if you are using the "with sharing" keyword in the class. Using "with sharing" forces Salesforce to follow security rules (which shouldn't be a problem if you are the admin, but i've had trouble with this before). I'd try removing it and re-deploying.

 

Can you post at least part of your code? That might help.....

Rajesh ShahRajesh Shah

Tried that too ... calling an apex class and inserting the records from there. It doesn't works. And I dont think it has anything to do with running in system mode or user mode. I am a system admin, and the code doesn't works even when I am converting. It is something else, that is giving the problem.

 

What I have found is that when the person who owns the Lead converts the Opportunity, the trigger works fine and always inserts the Sales Team. If it someone else who does the insertion, it doesnt works even if he is the system admin.

Heres part of my code.

 

trigger updateOpportunitywithPartnerListAndSalesTeam on Lead (after update) {
	
	// DO some stuff here

	// Insert Sales Team.
	OpportunityTeamMember partnerTeamMember = new OpportunityTeamMember();
	partnerTeamMember.OpportunityId = oppId;
	partnerTeamMember.TeamMemberRole = 'Partner Rep';
	partnerTeamMember.UserId = trigger.new[0].CreatedById;
	
	OpportunityTeamMember rcsdTeamMember = new OpportunityTeamMember();
	rcsdTeamMember.OpportunityId = oppId;
	rcsdTeamMember.TeamMemberRole = 'Regional Channel Sales Director';
	rcsdTeamMember.UserId = trigger.new[0].RCSD_Name__c;
	
	OpportunityTeamMember[] teamMemberList = new List<OpportunityTeamMember>{partnerTeamMember, rcsdTeamMember};
	insert teamMemberList;	
		
}

 

 

rgard23rgard23

I'm pretty sure to make this trigger work on lead conversion you have to use a "Before Update" trigger. Even though the new records (account, opportunity, contact) haven't been created yet, you can access them via the fields on the lead:

 

Lead.convertedOpportunityId

Lead.convertedAccountId

Lead.convertedContactId

 

Try making those changes and it should work.

Rajesh ShahRajesh Shah

That too doesn't works :smileysad:

I think @future is the only thing that remains for me. Thanks a lot for your help into looking at this.

rgard23rgard23

Here's my code from my class that adds to the sales team. All I do first is figure out if the lead is converted with a "before update" trigger. Then I pass the values into this class:

 

public class addToSalesTeam {
	
	/**
	* To run this method, the trigger must pass two variables:
	*
	* @Opportunity opty = the opportunity being shared
	* @User theUser = the user to be shared with
	* @String teamRole = the role of the team member
	*/

	
	public static void addUser (id optyid, String userId, String teamRole) {
		System.debug('opty id is: ' + optyid + ' user id is: ' + userId);
		OpportunityTeamMember optyMemb = new OpportunityTeamMember();
		
		//put Opportunity Id into optyMemb object
		optyMemb.OpportunityId = optyid;
		
		//put userId into optyMemb object
		optyMemb.UserId = userId;
		
		//put teamRole into optyMemb object
		optyMemb.TeamMemberRole = teamRole;
		
		//Insert sharing record into database
		Database.Saveresult sr = Database.insert(optyMemb);

	}//end addPartner method