• Mark Jones 40
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
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;
}

 
I have two custom fields on Leads, the first is an MSP the second is an MSP with Global Picklist Value Set. I want to map the contents of the first to the second. I tried this in Partial Copy Sandbox (Which does contain all of my Lead records) and it worked fine. I copied the code into my prod org and I get Apex CPU time limit exceeded.

The reason I am doing this in the dev console rather than just using excel is partly just to learn SOQL and DML but I also need to do the same to Campaigns, Accounts and Contacts. I figured I could just re-use this code and change the Object.

Please see my code below...

List<Lead> temp = [SELECT Id,FirstName,LastName,Area_of_Interest__c,Area_of_Interest_Global__c FROM Lead WHERE ConvertedDate = NULL AND Area_of_Interest__c != NULL AND Area_of_interest_global__c = NULL];
    List<Lead> LeadstobeUpdated = new List<Lead>{};
    for (Lead temploop: temp){
        temploop.Area_of_Interest_Global__c = temploop.Area_of_Interest__c;
        LeadstobeUpdated.add(temploop);
    }
    update LeadstobeUpdated;

I am very new to writing code so I may have missed something completely (my first go contained the DML inside the for Loop, now know thats a bad idea).

Thanks for your help!
I have two custom fields on Leads, the first is an MSP the second is an MSP with Global Picklist Value Set. I want to map the contents of the first to the second. I tried this in Partial Copy Sandbox (Which does contain all of my Lead records) and it worked fine. I copied the code into my prod org and I get Apex CPU time limit exceeded.

The reason I am doing this in the dev console rather than just using excel is partly just to learn SOQL and DML but I also need to do the same to Campaigns, Accounts and Contacts. I figured I could just re-use this code and change the Object.

Please see my code below...

List<Lead> temp = [SELECT Id,FirstName,LastName,Area_of_Interest__c,Area_of_Interest_Global__c FROM Lead WHERE ConvertedDate = NULL AND Area_of_Interest__c != NULL AND Area_of_interest_global__c = NULL];
    List<Lead> LeadstobeUpdated = new List<Lead>{};
    for (Lead temploop: temp){
        temploop.Area_of_Interest_Global__c = temploop.Area_of_Interest__c;
        LeadstobeUpdated.add(temploop);
    }
    update LeadstobeUpdated;

I am very new to writing code so I may have missed something completely (my first go contained the DML inside the for Loop, now know thats a bad idea).

Thanks for your help!