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
Marcelo Velame 1Marcelo 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:
  • Object 1: Properties__c
Fields:
  • 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
Object 2: PropertyInformation__c

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?
PrabhaPrabha
Such a detailed question... I appreciate it and I wonder if ChatGPT can answer this... pls try for fun... It also can crate a test class for that trigger...

One followup question: What should the trigger do if that object 2 record is not created yet?

 
Marcelo Velame 1Marcelo Velame 1
Hi, I tried through chatgpt but the same is out
ChatGPT is at capacity right now
PrabhaPrabha

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:

Trigger:

trigger PropertiesTrigger on Properties__c(
    before insert,
    after insert,
    before update,
    after update,
    before delete,
    after delete,
    after undelete
) {
    if (Trigger.isAfter) {
        if (Trigger.isInsert) {
            PropertiesTriggerHelper.createPropInfo(Trigger.newMap);
        }
        if (Trigger.isUpdate) {
            PropertiesTriggerHelper.updatePropInfo(Trigger.newMap, Trigger.oldMap);
        }
    }
}
Helper Class:
public without sharing class PropertiesTriggerHelperHelper {
    
    public static void createPropInfo(Map<Id, Properties__c> properties) {
        List<PropertyInformation__c> listPropertyInformation = new List<PropertyInformation__c>();
        for(Properties__c property : properties.values()){
            listPropertyInformation.add(new PropertyInformation__c(
                ResponsibleBroker__c = property.Id,
                PropertyType__c = property.PropertyType__c,
                PropertyForSaleOrRent__c = property.PropertyForSaleOrRent__c,
                NumberOfRooms__c = property.NumberOfRooms__c
            ));
        }
        if(listPropertyInformation.size()>0) insert listPropertyInformation;
        
    }
    public static void updatePropInfo(Map<Id, Properties__c> newProperties, Map<Id, Properties__c> oldProperties) {
        List<PropertyInformation__c> existingPropInfo = [SELECT ID, ResponsibleBroker__c, PropertyType__c, PropertyForSaleOrRent__c, NumberOfRooms__c FROM PropertyInformation__c WHERE ResponsibleBroker__c IN : newProperties.keySet()];
        List<PropertyInformation__c> listPropInfo = new List<PropertyInformation__c>();
        for(PropertyInformation__c prodInfo : existingPropInfo){
            Boolean propertyChanged = false;
            Properties__c newProperty = newProperties.get(prodInfo.ResponsibleBroker__c);
            if(newProperty.PropertyType__c != oldProperties.get(newProperty).PropertyType__c){
                propertyChanged = true;
                prodInfo.PropertyType__c = newProperty.PropertyType__c;
            }
            if(newProperty.PropertyForSaleOrRent__c != oldProperties.get(newProperty).PropertyForSaleOrRent__c){
                propertyChanged = true;
                prodInfo.PropertyForSaleOrRent__c = newProperty.PropertyForSaleOrRent__c;
            }
            if(newProperty.NumberOfRooms__c != oldProperties.get(newProperty).NumberOfRooms__c){
                propertyChanged = true;
                prodInfo.PropertyType__c = newProperty.PropertyType__c;
            }
            if(propertyChanged) listPropInfo.add(prodInfo);
        }

        if(listPropInfo.size()>0) update listPropInfo;
        
    }
}
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...

It might need few changes as per your needs... Let me know .
PrabhaPrabha

Also, you can do this now in Flows as well, 

https://admin.salesforce.com/blog/2022/automate-this-update-related-records-with-flow