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
Mike@MungosMike@Mungos 

Trigger to update a lookup on a custom object is turning the field blank

Hi

I am trying to write a trigger that will update the lookup field called Most_recent_nomination_tenancy__c field on a custom object called Referral__c when a new Nomination/Tenancy is created.  However at present rather than updating the field with the name of the new Nomination/Tenancy it is turning the field blank.   Does anyone know why this trigger is turning the field blank and not updating it?

trigger UpdateReferralWithNewNomTen on Nomination_Tenancy__c (before insert, before update) {

  for(Nomination_Tenancy__c newRecord : Trigger.new)  {        
      Referral__c Referral = [Select Id, Most_recent_nomination_tenancy__c from Referral__c WHERE Id = :newRecord.Referral__c ];
        Referral.Most_recent_nomination_tenancy__c = newRecord.Name;
        update Referral;               
    }

}

Many Thanks,

Mike
Ramu_SFDCRamu_SFDC
Lookup fields require id's and they will not accept any other data types. Please change the line to the below one to fix the issue

Referral.Most_recent_nomination_tenancy__c = newRecord.id;
Gigi.OchoaGigi.Ochoa
Hi Mike,

I've noticed a couple of things.  

Referral.Most_Recent_Nomination_Tenancy__c = Nomination_Tenancy__c.Id (Not .Name)

Also, best practice is not to include DML inside a for loop.
https://developer.salesforce.com/page/Apex_Code_Best_Practices

Following those best practices here is design I would use:
  • Loop through your Nomination Tenancy records and create a Map of  Referall Ids to Nomination Tenancy Id's.  Ex: refMap.put(newRecord.Referral__c, newRecord.Id)
  • Outside the loop, get a list of Referrals WHERE Id IN :refMap.keyset()
  • Loop through those referral records and populate the Most Recent Nomination Tenancy field using the map. Ex. Referral.Most_Recent_Nomination_Tenancy__c = refMap.get(Referral.Id);
  • Finally, update your list of Referrals (outside any loops)
Hope this helps!

Ramu_SFDCRamu_SFDC
And please change the trigger type to after insert, after update as the new record's id will be created only after the save. The id will not be availble in before triggers.
Mike@MungosMike@Mungos
Thanks for the quick response Ramu_SFDC.  I updated as you said as well as well as changing the trigger to activate after insert and it seems to work now.
Gigi.OchoaGigi.Ochoa
Please keep in mind, if you ever do a bulk load (i.e. data loader), your code will break due to governor limits.  
Mike@MungosMike@Mungos
Thanks Gigi.Ochoa.  This has been taken into account and has been updated as follows.

trigger UpdateReferralWithNewNomTen on Nomination_Tenancy__c (after insert) {

List<Referral__c> referralsToUpdate = new List<Referral__c>{};
 
for(Nomination_Tenancy__c newRecord : Trigger.new)  {         
  Referral__c Referral = [Select Id, Most_recent_nomination_tenancy__c from Referral__c WHERE Id = :newRecord.Referral__c ];
  Referral.Most_recent_nomination_tenancy__c = newRecord.id;
  referralsToUpdate.add(Referral);                   
}

update referralsToUpdate;

}