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
Paddy dPaddy d 

Posting of a feed to the chatter group is happening twice on updating a record

Hi,

Requirement :

  1.  To Create a field called ‘chatter Group’ in opportunity creation page and the details page .Once the user enters the value in the field, along with other mandate fields and on saving the opportunity , a chatter group need to be created( with the name entered in the chatter group field) with the owner of the opportunity being the collaboration member to the group. Chatter group is not a mandatory field and the group can be created while updating the opportunity as well. A post feed should come to the group on  insertion and every time the opportunity is updated.
  2. Once a user is added to the ‘opportunity Team Member’ of the opportunity , he should automatically be added to the created group.

 

Problem : On Implementing the following  trigger , creation of the chatter group is happening . The opportunity team members are added to the chatter group .

Posting of a feed to the group is happening once an opportunity is created . But on editing an opportunity , the feed post to the group is happening twice .

 

Kindly let us know the solution to get feed post to the group to come only once on updating the opportunity.

 

Please find below the trigger that has been written to achieve the same.

 

 

trigger CrtGrpFeed on Opportunity (after update) {
    List<FeedItem> filist = new List<FeedItem>();
    List<Opportunity> opp = new List<Opportunity>();
    String strGroupFlag ;
    String strGroupName ;
    String strGroupId ;
    ID GrpId ;
    ID OppOwnerId ;
 
    try
    {
   
        for (Opportunity oppnew:Trigger.new)
        {
             opp = [select Name, OwnerId ,Chatter_Group__c , Chatter_group_flag__c , Chatter_Group_Id__c from Opportunity where Id =: oppnew.Id];   
             strGroupFlag = opp[0].Chatter_group_flag__c ;
             strGroupName = opp[0].Chatter_Group__c ;
             strGroupId = opp[0].Chatter_Group_Id__c ;   
             OppOwnerId = opp[0].OwnerId ;
             
             system.debug('@@@@@@@@@@@@@@@@@@@ Chatter Flag @@@@@@@@@@@@@@@@@@' +oppnew.Chatter_group_flag__c);
              if( oppnew.Chatter_group_flag__c != '1'  && oppnew.Chatter_Group__c <> null )
                 {
                          List<FeedItem> filist1 = new List<FeedItem>();
                          FeedItem post = new FeedItem();
                           String strName = [Select Name from User where Id =: oppnew.LastModifiedById].Name;
                            post.parentid = oppnew.id;
                            post.Body = 'New Chatter Group  '  +  oppnew.Chatter_Group__c +  'has been created for this pursuit ' +    'on ' +                            oppnew.LastModifiedDate;
                             filist1.add(post);
                           Database.insert(filist1,false);      
                           
                      system.debug('*************** CreateGroupInitiate *********************'); 
                      for (Opportunity op:opp)
                      {
                         CollaborationGroup grp= new CollaborationGroup();
                         grp.Name = strGroupName;
                         grp.CollaborationType = 'Private';
                         grp.OwnerId = OppOwnerId ;
                         system.debug('*************** Owner added *********************' + grp.OwnerId);
                         insert grp;
                         system.debug('*************** Group Inserted *********************');
                         GrpId = grp.Id;
                         strGroupId = grp.Id;
                         op.Chatter_Group_ID__c = GrpId;
                         op.Id = oppnew.Id ;  
                         op.Chatter_group_flag__c = '1'; 
                         upsert op ;
                        
                     List<CollaborationGroupMember> grpMrList = new List<CollaborationGroupMember>();
                         List<OpportunityTeamMember> OppList1 = [Select UserId , Opportunityid from OpportunityTeamMember where opportunityId= :oppnew.Id];
                         if(!OppList1.isEmpty())
                         { 
                         
                             for(OpportunityTeamMember member: OppList1)
                             {
                                   system.debug('*************** Member added initiation *********************');
                                   CollaborationGroupMember grpMr = new CollaborationGroupMember() ;
                                   grpMr.memberid = member.UserId;
                                   system.debug('@@@@@@@@@@@@@@@ UserId @@@@@@@@@@@@@@@' + grpMr.memberid);

                                   grpMr.CollaborationGroupId = GrpId ;
                                   system.debug('*************** Member added *********************' + member.UserId);    
                                   grpMrList.add(grpMr);
                                   system.debug('@@@@@@@@@@@@@@@ UserId @@@@@@@@@@@@@@@' + grpMrList );
                             
                             }
                             system.debug('@@@@@@@@@@@@@@@@@ CG Group members before insrt after loop@@@@@@@@@@@@@@@@@@@@'  ) ;
                             insert grpMrList;
                             system.debug('*************** Group members added *********************' ) ;
                         }
                     
                     }             
                
        }
   
      
         if( oppnew.Chatter_group_flag__c == '1'  && oppnew.Chatter_Group__c <> null &&  oppnew.Chatter_group_flag__c  == '1')
         {
           system.debug('*************** FeedPostInitiate *********************');
                 List<FeedItem> filist2 = new List<FeedItem>();
                 FeedItem post2 = new FeedItem();
                 post2.ParentId = strGroupId; //Chatter Group ID
                 String strName = [Select Name from User where Id =: oppnew.LastModifiedById].Name;
             
                 system.debug('*************** strName *********************' + strName);
            
                 post2.Body = 'Please find below the pursuit details ' +  '\n This was updated by '
                 + strName + ' at ' + oppnew.LastModifiedDate + '\n Pursuit Stage :' + oppnew.StageName 
                 + '\n Pursuit Type:' + oppnew.Type + '\n Estmated Pursuit value :' + oppnew.Estimate_Proposal_Value__c
                 + '\n Pursuit Currency : ' + oppnew.CurrencyIsoCode ;
                 system.debug('***************Currency Code Flag *********************'+ oppnew.CurrencyIsoCode  );
                 filist2.add(post2);
             
                       insert filist2;
       
        }
     }
  
   
    }
    
    catch (Exception e) 
    {
       // Generic exception handling code here Contact.AccountId
       system.debug('Exeption ->  ' + e );
    }
  

}

Best Answer chosen by Admin (Salesforce Developers) 
gedeefgedeef

I would try to disable this workflow and test the trigger again.

If the post happens only once, you'll have found the cause : the workflow modifies the opport. what causes the trigger to run again.

Then you could set the opp. record type in the trigger instead of a workflow.

All Answers

gedeefgedeef

don't you have a workflow who updates a field on the opportunity ? maybe that could make the trigger run twice ?

Paddy dPaddy d

Hi,

we have workflow rules under opportunities . In field updates we are not updating any field but we are giving the opportunity record type to update and based on that record type we are calling different page layouts .

 

gedeefgedeef

I would try to disable this workflow and test the trigger again.

If the post happens only once, you'll have found the cause : the workflow modifies the opport. what causes the trigger to run again.

Then you could set the opp. record type in the trigger instead of a workflow.

This was selected as the best answer
Paddy dPaddy d

Thanks a lot .. This worked :)