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
NANCY1NANCY1 

Update a text field based on lookup field using the Trigger??

Hi All,

 

I have a lookup field Proposal_RRF__c from (Resource_Requirement__c object) in the Custom object i.e.  Proposal__c, based on this fields value selection  i want to extract RRF_Request_By__c field from Resource_Requirement__c object but i am getting the following error:

 

RRFDetails: execution of AfterUpdate caused by: System.FinalException: Record is read-only:

 

Trigger Code:

 

trigger RRFDetails on Proposal__c (after update)
{
    List<Id> proids = new List<Id>();
    Map<Id,Id> promap = new Map<Id,Id>();
    for(Proposal__c c : Trigger.new) {
        proids.add(c.id);
    }
    List<Resource_Requirement__c> pros = [SELECT id, RRF_Request_By__c from Resource_Requirement__c where ID IN :proids];
    for(Resource_Requirement__c o : pros) {
        promap.put(o.id,o.RRF_Request_By__c);
    }
   
    for(Proposal__c c : Trigger.new) {
      //if(c.Proposal_RRF__c != null)
        //{
        c.RRF_Requested_By__c = promap.get(c.RRF_Requested_By__c);
        //}
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You can't change the records in the trigger in an after update trigger, you have to update them anew:

 

E.g. 

 

 

  List<Proposal__c> toUpdate=new List<Proposal__c>();

  for(Proposal__c c : Trigger.new) {
      //if(c.Proposal_RRF__c != null)
        //{
        Proposal__c pro=c.clone();
        pro.RRF_Requested_By__c = promap.get(c.RRF_Requested_By__c);
        toUpdate.add(pro);
        //}
    }

   update (toUpdate);

 

Or you could make this a before update trigger, in which case you can change the records.

 

All Answers

bob_buzzardbob_buzzard

You can't change the records in the trigger in an after update trigger, you have to update them anew:

 

E.g. 

 

 

  List<Proposal__c> toUpdate=new List<Proposal__c>();

  for(Proposal__c c : Trigger.new) {
      //if(c.Proposal_RRF__c != null)
        //{
        Proposal__c pro=c.clone();
        pro.RRF_Requested_By__c = promap.get(c.RRF_Requested_By__c);
        toUpdate.add(pro);
        //}
    }

   update (toUpdate);

 

Or you could make this a before update trigger, in which case you can change the records.

 

This was selected as the best answer
NANCY1NANCY1

but still.. i am not able to fetch the related field from another custom  object...

 

trigger RRFDetails on Proposal__c (after insert,after update)
{
    List<Id> proids = new List<Id>();
    Map<Id,string> promap = new Map<Id,string>();
    for(Proposal__c c : Trigger.new) {
        proids.add(c.Proposal_RRF__c);
    }
    List<Resource_Requirement__c> pros = [SELECT id, RRF_Request_By__c from Resource_Requirement__c where ID IN :proids];
    for(Resource_Requirement__c o : pros) {
        promap.put(o.id,o.RRF_Request_By__c);
    }
   List<Proposal__c> toUpdate=new List<Proposal__c>();

    for(Proposal__c c : Trigger.new) {
        Proposal__c pro=c.clone();
        pro.RRF_Requested_By__c = promap.get(c.RRF_Requested_By__c);
        toUpdate.add(pro);
        }
}

bob_buzzardbob_buzzard

Can you give a little more detail as to which part of the trigger isn't working?  I'm assuming you aren't getting an error when it executes, but you aren't seeing the results you expect.