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
Mark Jones 40Mark Jones 40 

Trigger to add an Id from a record to a text field on a related record

Hi there,

I am hoping you can help I am new to writing Triggers and I could really do with a hand. I have created a custom object called Campaign Team Member (Campaign_Team_Member__c) with a Master detail to Campaigns (Campaign__c) and a Lookup to Users (User__c). I have also added a text field to Campaigns called CampaignTeamIDs (CampaignTeamIDs__c).

What I am trying to do is on insert or update on Campaign Team Member check the values of CampaginTeamIDs__c add the Id in User__c to CampaignTeam_IDs__c if they do not exist and return an error if they do. On delete of the Campaign_Team_Member__c record I want to find the Campaign and remove the User Id from the CampaignTeamIDs__c field.

I then have a formula field that returns a true checkbox if the CampaignTeamIDs__c field contains the current user id, then I can use a list view to return My Team's Campaigns.

This is my code at the moment but I am unsure how to do a check on duplicate values in the CampaignTeamIDs__c before Insert
 
trigger UpdateCampaignTeamMembers on Campaign_Team_Member__c (after insert, after update, after delete, after undelete) {
   
	//If the event is insert or undelete, this list takes New Values or else it takes old values
	List<Campaign_Team_Member__c> CampaignTeamList = (Trigger.isInsert|| Trigger.isUnDelete) ? Trigger.new : Trigger.old;

    //set of Campaign Id's - lists can contain dupilcates.
    set<id> CampaignTeamIds = new set<id>();

    //Loop through the Campaign Team Member object Records to store the Campaign Team Member Id values
    for (Campaign_Team_Member__c Cam_team : CampaignTeamList) {
    	CampaignTeamIds.add(Cam_team.Campaign__c);
    }

	List<Campaign> CampaignList = [select id, (select Campaign__c, User__c from Campaign_Team_Members__r)
								from Campaign where id in :CampaignTeamIds];


	for (Campaign Camp : CampaignList) {

		if(Camp.Campaign_Team_Members__r.size() > 0){
  			Camp.CampaignTeamIDs__c = '';

  			for(integer i=0;i < Camp.Campaign_Team_Members__r.size();i++){
   				Camp.CampaignTeamIDs__c += string.valueOf(Camp.Campaign_Team_Members__r[i].User__c)+ '; ';
  			}
		}else Camp.CampaignTeamIDs__c = null;
    }
	//update the Campaign List
	update CampaignList;
}

 
Vinit JoganiVinit Jogani
Hi Mark,

First of all, I dont really get the idea of adding CampaignTeamIDs (CampaignTeamIDs__c) custom field to Campaign object. You already have Master details of Campaign and Campaign Team Member, you should query and get all Campaign Team Member for given Campaign using SOQL and process it.

Let me know if I am missing anything here.

Second, I dont see your code checking whether the User id already exists in CampaignTeamIDs__c field. You are just iterating and appending to the String. You might want to add th logic at /or around Line 24 in above code.