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
cbrocbro 

Need to update lookupRecord field (Date/Time) when relatedRecord is created or updated

I need to update a custom Date/Time field on thelLookupRecord any time I create or update a relatedRecord.

 

How can I build a trigger to do this?

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
cbrocbro

Here is the updated code.  Fixed.  Thanks to some help from my internal team.  -  Sonja, you almost had it - but I didn't do a good job explaining what I needed!

 

The Contract_Header__c field is just a text field.  I have a Contract_Header_LOOKUP__c that is the lookup field - which is what I needed to make it work.

 

Well - I'm still trying to populate that lookup field, but once it is populated, I should be all good.

 

trigger ContractLine on Contract_Line__c (after insert, after update){
   List <Contract_Line__c> cList = new List <Contract_Line__c>();
   List <Contract_Header__c> cHeaderList = new List <Contract_Header__c>();
   for(Contract_Line__c c: [Select Contract_Header__c, Contract_Header_LOOKUP__r.Most_Recent_Update__c from Contract_Line__c where Id IN :Trigger.newMap.keySet()])
   {
   c.Contract_Header_LOOKUP__r.Most_Recent_Update__c = System.today();
       cList.add(c);
       
cHeaderList.add(c.Contract_Header_LOOKUP__r);       }
           
   if(cHeaderList.size()>0)
   {
    try{
     update cHeaderList;
    }catch(DMLException e)
    {
     
    }
   }
}

 

All Answers

sobroachsobroach

Google apex date/time methods but here's an example:

 

myDateField__c = System.now();

 

or store it in a variable like :

 

Date myDate = System.now(); 

myDateField__c = myDate;

 

cbrocbro

I need to build a trigger to do  this whenever relatedRecord is created or updated.  

 

Thanks,

 

sobroachsobroach

You need to make the trigger (after insert, after update) What other criteria do you need in the trigger?

cbrocbro

I need this Date/Time field on the lookupRecord to update every time the relatedRecord is updated or added.

 

Do I need to have an if statement for every field that could be updated on the Contract_Line__c - that then pushes over the DateTime to the Contract_Header__c?

 

or can I just simply send over the DateTime whenever this record is edited (saved/changed)...?

 

This is now where I am- and it's closer, and I am getting this error (Error: Compile Error: Didn't understand relationship 'Contract_Header__r' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 4 column 32):

 

trigger ContractLine on Contract_Line__c (after insert, after update){
   List <Contract_Line__c> cList = new List <Contract_Line__c>();
   
       for(Contract_Line__c c: [Select Contract_Header__c, Contract_Header__r.Most_Recent_Update__c from Contract_Line__c where Id IN: Trigger.keySet()])
   {
   c.Contract_Header__r.Most_Recent_Update__c = System.today();
       cList.add(c);
       }
           
   if(cList.size()>0)
   {
    try{
     update cList;
    }catch(DMLException e)
    {
     
    }
   }
}

 

cbrocbro

Here is the updated code.  Fixed.  Thanks to some help from my internal team.  -  Sonja, you almost had it - but I didn't do a good job explaining what I needed!

 

The Contract_Header__c field is just a text field.  I have a Contract_Header_LOOKUP__c that is the lookup field - which is what I needed to make it work.

 

Well - I'm still trying to populate that lookup field, but once it is populated, I should be all good.

 

trigger ContractLine on Contract_Line__c (after insert, after update){
   List <Contract_Line__c> cList = new List <Contract_Line__c>();
   List <Contract_Header__c> cHeaderList = new List <Contract_Header__c>();
   for(Contract_Line__c c: [Select Contract_Header__c, Contract_Header_LOOKUP__r.Most_Recent_Update__c from Contract_Line__c where Id IN :Trigger.newMap.keySet()])
   {
   c.Contract_Header_LOOKUP__r.Most_Recent_Update__c = System.today();
       cList.add(c);
       
cHeaderList.add(c.Contract_Header_LOOKUP__r);       }
           
   if(cHeaderList.size()>0)
   {
    try{
     update cHeaderList;
    }catch(DMLException e)
    {
     
    }
   }
}

 

This was selected as the best answer