You need to sign in to do that
Don't have an account?
Marcelo Velame 1
How to update fields of 2 related custom objects using Apex trigger?
I have a scenario where I want to make an Apex Trigger so that when a record is inserted or updated when I click on save, the trigger is triggered.
The object that the trigger will be started will be in a custom object and updated in another custom object.
I created a lookup field in object 2, in this case the PropertyInformation__c object relating to object 1, Properties__c.
Objects:
Lookup Field: ResponsibleBroker__c
How could I be creating the apex trigger to be able to update the fields in object 2?
The object that the trigger will be started will be in a custom object and updated in another custom object.
I created a lookup field in object 2, in this case the PropertyInformation__c object relating to object 1, Properties__c.
Objects:
- Object 1: Properties__c
- PropertyType__c - data type: picklist with values (Apartment, Commercial, Land, Home, Office)
- PropertyForSaleOrRent__c - data type: picklist with values (Rent, Sell)
- NumberOfRooms__c - number data type
Lookup Field: ResponsibleBroker__c
- Fields to be updated:
- PropertyType__c
- PropertyForSaleOrRent__c
- NumberOfRooms__c
How could I be creating the apex trigger to be able to update the fields in object 2?
One followup question: What should the trigger do if that object 2 record is not created yet?
ChatGPT is at capacity right now
I didnt get the answer for my question above...
So assuming that we will be creating object2 while object1 gets created... here is a sample code:
Helper Class: I wrote the second method in the helper class in such a way that it wont make unnecessary updates if those three fields are not updated...Trigger:
It might need few changes as per your needs... Let me know .
Also, you can do this now in Flows as well,
https://admin.salesforce.com/blog/2022/automate-this-update-related-records-with-flow