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
WalidWalid 

Trigger to delete Public Group Member when Junction record deleted?

Hi there,

I would like to create a Trigger that deletes a user from a Public Group based when a Junction obkject record is also deleted. 

I have an Object Campaign that has many to many relationship to User through a Junction object called Campaign Agent. I also have a Public Group for each Campaign that has the name "Campaign ID + Agents". So, for example, if a Campaign is called My Campaign has a Campaign_Id__c field called CMP123, then the public group name is "CMP123 Agents". Whenever I add an Agent to this Campaign via the Junction object, it is automatically added to the group using Process Builder and a Time based action (after 0 hours) that calls a FLow. The Flow makes a lookup to the Campaign object, gets the Campaign Id value, then another lookup to the Group object to get the Id of the public group name, finally, it creates a GroupMember record with UserOrGroupId = User Id, and GroupId = the queried public Group Id.  

I need to do the opposite when I remove a Campaign Agent from a Campaign: I need to remove the user from the public Group when this happens.

I know  that the objct GroupMember controls which user accesses which public group using the field groupId and UserorGroupId. 

The Trigger should fire on before or after delete on the Campaign Agent object. 

Any hints to create it?