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
Kris WebsterKris Webster 

Somewhat Simple (I think) TRIGGER HELP

I am not finding a way to solve this issue. I am trying to write a trigger that will populate a contact lookup field with a specific contact. I am struggling to figure out how to code the specific contact into the trigger. WHEN i say a specific contact I mean 1 contact record. SO I want the contact Kris Webster to fill the field Project_Manager__c on the campaign object when a new campaign record is created.. IS THIS POSSIBLE? would I need to build a SOQL of contcats first or what?? I cant seem to find an answer anywhere.. 

Here is what I have so far. 
trigger CampaignTrigger on Campaign (after insert) {

	for(Campaign camp:Trigger.New){
		if(camp.Project_Manager__c == null)
		camp.Project_Manager__c = WHAT DO I PUT HERE?? THE USERS ID? 
	}



}

 
RbnRbn
Hi Kris,

You can just  assign it witht the contact id.

Somrthing like this.::

camp.Project_Manager__c = '0039000002F7yAS'; (Id of Kris Webster contact record).

Let me know if it helps.

Cheers,
Rabindranath
kishore Thudikishore Thudi
Hi Kris,

I would do a query on contacts first, then assign contact Id to Look up field. And, also I would change this to before insert, because your assigning contact Id on the same record. We can also achieve this on after insert, but you need to query Campaign Ids to do the update.
Before Insert :
trigger CampaignTrigger on Campaign (before insert) {    
    contact con = [SELECT Id, Name FROM contact WHERE Name Like 'Kris Webster%' LIMIT 1];    
    for(Campaign camp:Trigger.New){
        if(camp.Project_Manager__c == null)
        camp.Project_Manager__c = con.id; 
    }
}

After Insert :

trigger CampaignTrigger on Campaign (after insert) {
    
    contact con = [SELECT Id, Name FROM contact WHERE Name Like 'kris Webster%' LIMIT 1];
    
    List<Campaign> CampaignsList = new List<Campaign>();
    for(Campaign camp:[SELECT Id,Project_Manager__c FROM Campaign WHERE Id IN : Trigger.New]){
        if(camp.Project_Manager__c == null)
        camp.Project_Manager__c = con.id; 
        CampaignsList.add(camp);
    }
    update CampaignsList;

}

Best,
Kishore.