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
D-VacD-Vac 

Insert OpportunityShare during lead convert

Does anybody know how to create additional OpportunityShare records when converting a lead where the lead owner is different than the user who is converting the lead? 

 

I am trying to create opportunity share records which share the opportunity with other relevant users besides the opportunity owner and their management hierarchy. 

 

I know salesforce documentation says it runs the opportunity before triggers, etc... during lead convert.   That being said, it looks like it runs after insert triggers on the opportunity as well.  And possibly "after update"?

 

I'm trying to set the oppty shares during the Lead After Update trigger that creates the opportunity.  Everything works fine except that I believe that the opportunity owner "changes" before the Opportunity is committed to the database (i.e. from the running user to the lead owner) and therefore clears out any of the manual sharing added to the opportunity prior to the opportunity being committed to the database.  

 

Does anybody know the true order of execution during lead convert or any workarounds that allow re-triggering of the opportunity after it's been committed to the database so that the opportunityshare records can be added back on?

 

Thanks for any help!

 

 

Kiran  KurellaKiran Kurella

I came across a similar situation couple of years ago, where I was trying to add Account Team Members to the Account, after the Lead is converted. I tried various opportunity, Lead and Account trigger events and nothing worked at that time. I ended up over riding the Account view page with a VF page and executed the logic in the constructor. 

 

In the class constructor, I executed the action, only if the account is created within a minute by the same user (something like this).

 

acct.CreatedById == Userinfo.getUserId() && acct.CreatedDate >= DateTime.now().addMinutes(-1)

 

I hope this is helpful.

D-VacD-Vac

Thanks very much for this.  I don't think I'm ready to go the VF route just yet for this but that's a clever way to get around it.

 

For now, unless anybody has anything else, I wrote another trigger on the opportunity to create the shares if there are no 'Manual' shares on the opportunity when it is edited the first time post-lead convert (as all the custom inserted OpportunityShare's rowCause are 'Manual').

 

We have a field on the opportunity that duplicates the Owner, called Owner_User__c.  The owner and the Owner_User__c are out of sync after the lead convert but the next time the opp is edited, they get in sync.  You could just as easily set a field on the Opp (something like DateTime of Lead Convert) and check for that being != Null and then in the update trigger set that back to null so this trigger doesn't fire again. 

 

This trigger grabs the relevant people from a child object on the Opp called the Opportunity_Split_Reporting_Lens__c:

 

trigger OppSetManualSharesAfterUpdate on Opportunity (after update) {

List<Opportunity> OppsWithOwnersOutofSync = New List<Opportunity>();

For(Opportunity Opp :Trigger.new){
if(trigger.oldmap.get(Opp.id).Owner_User__c != trigger.oldmap.get(Opp.id).ownerid){
OppsWithOwnersOutofSync.add(Opp);
}
}

if(OppsWithOwnersOutofSync != Null && !OppsWithOwnersOutofSync.isEmpty()){

List <OpportunityShare> OppShares = 
[SELECT OpportunityId, RowCause, UserOrGroupId
FROM OpportunityShare 
WHERE RowCause = 'Manual' and OpportunityId in :OppsWithOwnersOutofSync];



         List<Opportunity_Split_Reporting_Lens__c> LensesOnOpps = 
         [select ID, Opportunity_Name__c,
         Is_this_the_Opp_Owner_s_Split_Lens__c, Opportunity_Name__r.Id,Last_Updated_by_Trigger__c,Opp_Split_User__c, Opp_Split_User__r.Commission_Upline_Manager__c, Opportunity_Name__r.OwnerId,Upline_Commissioned_Manager_1__c,
         Opportunity_Name__r.Owner.Commission_Upline_Manager__c ,z_DateTimeOfNewOppOwnerChange__c, CurrencyIsoCode, Opportunity_Name__r.CurrencyIsoCode, Split__c, Live_Exchange_Rate__c,       
         Alliances_Type_Manual_Override__c, z_Alliances_Split_Type_TEXT__c, z_PartnerRecordDerivedFrom__c,  Report_on_this_Split_for_Finance_Manual__c,z_Hidden_ID_of_Split_User_s_Mgr__c,
         z_Hidden_ID_of_Split_User_s_Upline__c,z_OppOwnerId__c
         FROM Opportunity_Split_Reporting_Lens__c 
         WHERE Opportunity_Name__r.id in :OppsWithOwnersOutofSync];

         Map<Id, User>ActiveUserMap = New Map<Id,User>(
         [SELECT Id, IsActive, Commission_Upline_Manager__c, FirstName, LastName, UserRole.Name, Division_Picklist__c, Team_Picklist__c, Sun_Team_Picklist__c
         FROM User
         WHERE IsActive = true]); 

List<OpportunityShare> OppSharestoCreate = New List<OpportunityShare>();

if(LensesOnOpps != Null && !LensesOnOpps.isEmpty()){


for(Opportunity_Split_Reporting_Lens__c Olens :LensesOnOpps){
if(trigger.newmap.get(Olens.Opportunity_Name__c).OwnerId != Olens.Opp_Split_User__c){
OppSharestoCreate.add(new OpportunityShare(OpportunityId = Olens.Opportunity_Name__c,OpportunityAccessLevel = 'Edit',UserOrGroupId = Olens.Opp_Split_User__c));
}
if(Olens.Upline_Commissioned_Manager_1__c != null && Olens.Upline_Commissioned_Manager_1__c !=Trigger.newmap.get(Olens.Opportunity_Name__c).OwnerId){
OppSharestoCreate.add(new OpportunityShare(OpportunityId = Olens.Opportunity_Name__c,OpportunityAccessLevel = 'Edit',UserOrGroupId = Olens.Upline_Commissioned_Manager_1__c));
}
if(ActiveUserMap.get(Olens.z_Hidden_ID_of_Split_User_s_Mgr__c) != null){
OppSharestoCreate.add(new OpportunityShare(OpportunityId = Olens.Opportunity_Name__c,OpportunityAccessLevel = 'Edit',UserOrGroupId = Olens.z_Hidden_ID_of_Split_User_s_Mgr__c));
}
}

    if(OppSharestoCreate != null && !OppSharestoCreate.isEmpty())
    
    Database.insert(OppSharestoCreate);


}

}

}