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
Ruth RushtonRuth Rushton 

apex trigger insert custom object 1 field from custom object 2 field

Hi

Can someone please help as I have no Apex experience and I've tried various different previous posts to help but I'm not getting anywhere!

I have two Custom objects, CustomObj1 has a field which is a lookup of CustomObj2, CustomObj1 also has two other fields which I would like to be populated with details from CustomObj2 depending on which lookup is selected.

I think I need a Trigger on CustomObj1 but I don't know how to write the code (I keep getting errors to do with < symbols).

Please could someone help with what code I should be using, I have a Sandbox set-up with data in to test.

Thanks
Best Answer chosen by Ruth Rushton
Steven NsubugaSteven Nsubuga
TEXT(Category__r.Recurrence__c)

 

All Answers

Steven NsubugaSteven Nsubuga
You can do this with 2 formula fields on CustomObj1, no need for a trigger.
Ruth RushtonRuth Rushton
OK - I'm not great at formulae either (but do have some experience!).

The lookup field on obj1 is called Category and I want the Recurrence field on Obj1 to equal the Recurrence field on Obj2

I haven't had much sleep and my brain isn't working properly!
Steven NsubugaSteven Nsubuga
The syntax for the formula field on obj1 would be  
Category__r.Recurrence__c
Ruth RushtonRuth Rushton
Thanks - almost there however Recurrence is a picklist field on Obj2 and when I check Syntax I get the message

Error: Field Recurrence__c is a picklist field. Picklist fields are only supported in certain functions
Steven NsubugaSteven Nsubuga
TEXT(Category__r.Recurrence__c)

 
This was selected as the best answer
Ruth RushtonRuth Rushton
Thank you so much - it works! The only other question I have is if Obj2 is amended, will this be updated on Obj1 if will I need a Flow/Apex Trigger for that?
Steven NsubugaSteven Nsubuga
The only other question I have is if Obj2 is amended, will this be updated on Obj1?
Yes, it will automatically be updated in obj1, that's the beauty of formulas. Try it out!!
Ruth RushtonRuth Rushton
Amazing - thank you so much for your help, and much easier than Apex coding!