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
Wayne_ClarkWayne_Clark 

Trigger not updating lookup field

I have 2 objects (NRMA__c)  (ts2__Application__c).  I'm trying to update a look up field (Candidate__c) on the NRMA object with the Candidate field (ts2__Candidate_Contact__c) from the Application object. 

 

I was able to accomplish other field updates like this through workflows, but for some reason, I was unable to update the Contract lookup field on the NRMA.  I need this field to populate the Contact that is listed on the Application object. 

 

Here is another run down of the fields and objects:

NRMA__c - object that includes a lookup to contacts (Candidate__c) - trying to update this field automatically

ts2__Application - object that includes a looup field to Contacts (ts2__Candidate_Contact__c). 

 

When creating a new record on NRMA from Application, I want this to happen before update, before insert:

NRMA.Candiate c = Application.ts2__Candidate_Contact__c

 

Here is the trigger, but it's not working:

 

trigger CandidateTrigger on NRMA__C (before insert, before update) {

  List<String> canList = new List<String>();
  for (NRMA__c n:Trigger.new){
    canList.add(n.Application__c);
  }
 
  List<ts2__Application__c> contactList = [SELECT Id, Name, ts2__Candidate_Contact__r.Id FROM ts2__Application__c WHERE Id in :canList];
 
  Map<String, Id> nameToId = new Map<String, Id>();
  for(ts2__Application__c c : contactList) {
    nameToId.put(c.Name, c.Id);
  }
 
  for (NRMA__c n:Trigger.new){
  n.Candidate__c = nameToId.get(n.Application__r.ts2__Candidate_Contact__r.Id);
  }
 
  }

 

 

Any ideas?  Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

try this

trigger CandidateTrigger on NRMA__C (before insert, before update) {

  List<String> canList = new List<String>();
  for (NRMA__c n:Trigger.new){
    canList.add(n.Application__c);
  }
  
  List<ts2__Application__c> contactList = [SELECT Id, Name, ts2__Candidate_Contact__r.Id FROM ts2__Application__c WHERE Id in :canList];
  
  Map<ID, Id> nameToId = new Map<Id, Id>();
  for(ts2__Application__c c : contactList) {
    nameToId.put(c.id, c.ts2__Candidate_Contact__r.Id);
  }
  
  for (NRMA__c n:Trigger.new){
  //In below statement Application__c should be the reference to Application in NRMA__c  object , please check API Name Application__c
  n.Candidate__c = nameToId.get(n.Application__c);
  }
  
  }

 I hope it will work, Please let me know if any issues in it.

All Answers

Shashikant SharmaShashikant Sharma

try this

trigger CandidateTrigger on NRMA__C (before insert, before update) {

  List<String> canList = new List<String>();
  for (NRMA__c n:Trigger.new){
    canList.add(n.Application__c);
  }
  
  List<ts2__Application__c> contactList = [SELECT Id, Name, ts2__Candidate_Contact__r.Id FROM ts2__Application__c WHERE Id in :canList];
  
  Map<ID, Id> nameToId = new Map<Id, Id>();
  for(ts2__Application__c c : contactList) {
    nameToId.put(c.id, c.ts2__Candidate_Contact__r.Id);
  }
  
  for (NRMA__c n:Trigger.new){
  //In below statement Application__c should be the reference to Application in NRMA__c  object , please check API Name Application__c
  n.Candidate__c = nameToId.get(n.Application__c);
  }
  
  }

 I hope it will work, Please let me know if any issues in it.

This was selected as the best answer
Wayne_ClarkWayne_Clark

That worked great.  Thank you.