You need to sign in to do that
Don't have an account?
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
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.
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?
Nevermind, it works