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
Damien COUENNAUX 7Damien COUENNAUX 7 

Get field update (on parent object) with last record (from a child object)

Hi, I am absolutely not an Apex expert, and my issue cannot be resolved by the Salesfoce Standard. So if you could help me, it would be great!

I would like to get field update (for example "Contract_Type__c") on parent standard object "Contact" with a field ("Contract_Type_Bis") from the last record on a child custom object "Contract". The link between these 2 objects is only a look-up relationship. I can't use the master detaill relationship because i already used it 2 times with Contacts object.

I tried to use an Apex code from an another threat here : http://salesforce.stackexchange.com/a/23566

But it wasn't worked, and I didnt understand very well the logic.

Thanks again!

Best Answer chosen by Damien COUENNAUX 7
KaranrajKaranraj
Hi Damein - You can able to achieve this funatioanlity using Lightning Process builder without writing single line of code. Follow the belwo steps,

In the below example I'm capturing field from the contact and update to the Account Object, so whenever new contact is created for the Account then we will update the Account record with the latest contact record value. You just want to channe the objects and fields to work for your scenarios, the procedure remains same.

1. Go to Setup->Create->Workflow & ApprovalProcess->Processbuilder
2. Click New and set the Name of your Process
3. Select your child object  under the section "Choose Object and Specify When to Start the Process"
4. Select the "Only When record is Created Option"
User-added image
3. Then under the criteria you can set the condition if you want to excute only for certain records[ In the below example I'm checking for the condition Parent record relation ship is not null]
User-added image
5. Then in the immediate action select 'Update Records' under the object chose the object in which you want to update.
6. Then in the 'Set Object' Variable select the child field value which you want to copy and map it to the Parent object field.

User-added image
7. Then Activate to Process.

Thanks,
Karanraj

All Answers

sandeep sankhlasandeep sankhla
Hi Damen,

You can use a trigger for this..query the parent and then in inner query you can query the last insert record by mentioning limit 1 desc creadteddate..

selecte id (select Id from contartc orderby createddate desc limit 1)from Contact where Id In :contactId

Please try with this and let me know if it helps you..

thanks,
Sandeep
KaranrajKaranraj
Hi Damein - You can able to achieve this funatioanlity using Lightning Process builder without writing single line of code. Follow the belwo steps,

In the below example I'm capturing field from the contact and update to the Account Object, so whenever new contact is created for the Account then we will update the Account record with the latest contact record value. You just want to channe the objects and fields to work for your scenarios, the procedure remains same.

1. Go to Setup->Create->Workflow & ApprovalProcess->Processbuilder
2. Click New and set the Name of your Process
3. Select your child object  under the section "Choose Object and Specify When to Start the Process"
4. Select the "Only When record is Created Option"
User-added image
3. Then under the criteria you can set the condition if you want to excute only for certain records[ In the below example I'm checking for the condition Parent record relation ship is not null]
User-added image
5. Then in the immediate action select 'Update Records' under the object chose the object in which you want to update.
6. Then in the 'Set Object' Variable select the child field value which you want to copy and map it to the Parent object field.

User-added image
7. Then Activate to Process.

Thanks,
Karanraj
This was selected as the best answer
Damien COUENNAUX 7Damien COUENNAUX 7
Thanks a lot for your answers!

I tried to execute your code Sandeep but it wasn't a success..

Karanraj, like you I though it was possible to make that with Process Builder. But when i want to select the field required to update in the Parent object, i don't find it in tthe picklist... I can only choose standard fields i guess...

 
KaranrajKaranraj
Damien - You are allowed to select both custom and standard fields, check the below screenshot of the same process, where I have choose custom fields in the both the side 
  • Field3 [Custom field in parent object]
  • Tiara_number3__c [ Custom field in child object]

User-added image
Make sure that you have choose correct object in the update records process.
Damien COUENNAUX 7Damien COUENNAUX 7
Thank you very much, it seems to work! 
Maria FeuMaria Feu
Hi Damien, Karanraj,

I have somewhat similar problem, also related to the Contract object and I am hoping to be able to do it with Process Builder, but not sure how, and I get stuck midway using the example above.
In my case: When a contract is created, a "Primary" checkbox field is auto-checked on.
When I clone a contract, the clone gets the "Primary" checkbox automatically checked. I need to find a way to uncheck the "Primary" checkbox of the clone source.
(Example: I have contract ID000130. I create a clone contract ID000147, the "Contract Cloned" field in this clone contains ID000130. Now ID000147 is Primary, how can I uncheck the "Primary" box for ID000130)?

(For information: I have a lookup relationship from Contract to Contract and everytime a Contract is cloned, I have a field "Contract_Cloned_c" which is populated with the CloneSourceID().)