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
Alex.AcostaAlex.Acosta 

Lead Conversion with Opportunity EntitySubscription

Hi All,

 

Been having an issue here with salesforce and my trigger. It pretty much boils down to this... If I create an Opportunity my trigger works fine, but if I convert a Lead and have Salesforce generate my Opportunity I get this error:

FATAL_ERROR|System.DmlException: Insert failed. First exception on row 2; first error: DUPLICATE_VALUE, duplicate value found: <unknown> duplicates value on record with id: <unknown>: []

Now my code consists of 2 triggers. First trigger generates Teams and Team Members for each Opportunity Created.

Second trigger consists of each time a Team Member is created they are set to auto Follow that Opportunity.

 

First Trigger Example:

trigger OpportunityTeams on Opportunity (after insert) {
    
    List<Team__c> teams = new List<Team__c>();
    List<Team_Member__c> teamMembers = new List<Team_Member__c>();
// Please make sure you have more than 2 users in your system. (so at least 3 users) to demostrate this issue
List<User> users = [SELECT id FROM USER WHERE id != :UserInfo.getUserId()];
for(Opportunity opp : trigger.new){ Team__c team1 = new Team__c(); team1.Opportunity__c = opp.id; team1.Name = 'Team 1'; teams.add(team1); Team__c team2 = new Team__c(); team2.Opportunity__c = opp.id; team2.Name = 'Team 2'; teams.add(team2); } if(teams.size() > 0){ insert teams;

Integer userPosition = 0; // loop through all teams to add all their team members for(Team__c t : teams){ Team_Member__c tm = new Team_Member__c(); tm.Opportunity__c = t.Opportunity__c; tm.Team__c = t.Id; tm.User__c = UserInfo.getUserId();
tm.User__c = users.get(userPosition);
userPosition++; teamMembers.add(tm); } if(teamMembers.size() > 0) insert teamMembers; } }

 

Second Trigger to Auto add Team Members. Please keep in mind, Opportunity__c is a lookup to Opportunity and User__c is a lookup to User, and this trigger only runs on After Insert

trigger TeamMemberChatterAutoFollow on Team_Member__c (after insert) {

// Opportunity Id Key, with a set of User Id Value Map<Id, Set<Id>> userOpp = new Map<Id, Set<Id>>(); // Collection of EntitySubscriptions to follow our record List<EntitySubscription> subscriptions = new List<EntitySubscription<>(); // Sets collection of users based on the Opportunity as the parent / key. for(Team_Member__c tm : trigger.new){ //This section of code is to keep track of the Oppornity and it's team members. Set<Id> members = new Set<Id>(); if(userOpp.containsKey(tm.Opportunity__c)){ members = userOpp.get(tm.Opportunity__c); } members.add(tm.User__c); // Recreate our Map to hold our proper Values userOpp.put(tm.Opportunity__c, members); } // loop through our Opp map so we can force follow all of the associated team members for(Id t : userOpp.keySet()){ for(EntitySubscription es : ChatterUtils.addFollowers(userOpp.get(t), t)){ subscriptions.add(es); } } if(subscriptions.size() > 0){ system.debug('list of subscriptions: ' + subscriptions); Database.insert(subscriptions, false); }

 

ChatterUtils Class:

static public List<EntitySubscription> addFollowers(Set<Id> userIds, Id objectToFollowId) {
    EntitySubscription[] es = new List<EntitySubscription>();
    for (Id userId : userIds) {
         EntitySubscription e = new EntitySubscription();
         e.subscriberId = userId;
         e.parentId = objectToFollowId;
         es.add(e);
    }
    return es;
}

 

Any help will be greatly appreciated.

 

Best Answer chosen by Admin (Salesforce Developers) 
Alex.AcostaAlex.Acosta

According to Salesforce they pointed at me towards the ConvertLead Operation which states the following:

 

Automatic feed subscriptions: When you convert a lead into an account, contact, and (optionally) an opportunity, the owner of the generated records is automatically subscribed and the lead owner is unsubscribed from the lead record. Any users that were subscribed to the lead are now subscribed to the generated records and unsubscribed from the lead. This means that the lead owner and other users that were subscribed to the lead see any changes to the account, contact, and opportunity records in their news feed. The subscription occurs unless the user has selected the Stop automatically following records checkbox in Your Name | Setup | My Chatter Settings | My Feeds. A user can subscribe to a record so that changes to the record are displayed in the news feed on the user's home page. This is a useful way to stay up-to-date with changes to records in Salesforce.

 

From here all I did was write an IF conditional which did not allow the currently logged in user to be added to the EntitySubscriptions that were being generated to follow my object which was an Opportunity.

All Answers

Alex.AcostaAlex.Acosta

I've opened up a case with Salesforce. If I get any news, I'll make sure to post it here.

Again, any help will be greatly appreciated!

Alex.AcostaAlex.Acosta

According to Salesforce they pointed at me towards the ConvertLead Operation which states the following:

 

Automatic feed subscriptions: When you convert a lead into an account, contact, and (optionally) an opportunity, the owner of the generated records is automatically subscribed and the lead owner is unsubscribed from the lead record. Any users that were subscribed to the lead are now subscribed to the generated records and unsubscribed from the lead. This means that the lead owner and other users that were subscribed to the lead see any changes to the account, contact, and opportunity records in their news feed. The subscription occurs unless the user has selected the Stop automatically following records checkbox in Your Name | Setup | My Chatter Settings | My Feeds. A user can subscribe to a record so that changes to the record are displayed in the news feed on the user's home page. This is a useful way to stay up-to-date with changes to records in Salesforce.

 

From here all I did was write an IF conditional which did not allow the currently logged in user to be added to the EntitySubscriptions that were being generated to follow my object which was an Opportunity.

This was selected as the best answer