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
Chitral ChaddaChitral Chadda 

triggr populate field

// Automatically populate a lookup field to the Rival object

on account we have rival_picklist__c and rival__c is a lookup field relation to rival__c custom object
now when we select the picklist value from rival_picklist__c  , it should match that name from rival__c object and  populate that name  in rival__c field

trigger PopulateRival on Account (after insert,after update)
{
  set<id> accid = new set<id>();
  for( account acc: trigger.new)
  {
    accid.add(acc.id);
  }
  
  list<Rival__c> rvl = [select Name__c ,id from Rival__c];
  map<string,id> mapRival  = new map<string,id>();
  for(Rival__c r :rvl)
  {
  mapRival.put(r.Name,r.id);
  }

  list<account> acclist = [select id,Rival_Picklist__c,Rival__c from account where id in: accid];
  list<account> updateaccount = new list<account>();

  for(account a: acclist)
  {
  if(mapRival.containsKey(a.Rival_Picklist__c))
  {
  if(a.Rival__c=null || a.Rival__c!=mapRival(a.Rival_Picklist__c))
  {
  a.Rival__c = mapRival.get(a.Rival_Picklist__c);
  updateaccount.add(a);
  }
  }
  }

update updateaccount;
}

i m able to save the trigger but its not working.
Ramu_SFDCRamu_SFDC
The problem seems to be with this line if(a.Rival__c=null || a.Rival__c!=mapRival(a.Rival_Picklist__c))

I am confused with the second condition syntax. Can you please confirm what you are checking in the second condition?