The trigger that updates another object's field after inserting new record

I am new to Scaleforce. I created a new app, added three custom objects (Client, Property, Client_Property) and created tabs for them. So now CRUD is working correctly but I want to change Available field in the Property object to false (type of this field is boolean ) when adding a new record to the Client_Property object using a trigger in the Client_Property object .

Design of trigger seems to me like that:

  1. Get the Property Id field from the Property field in Client_Property object and save in some variable ( id_clpr ).
  2. Make SQL request like this: SELECT Available FROM Property WHERE id_clpr = Propery Id.
  3. And then change the field to false and somehow commit it.

In the Client_Property object the field is not an actual Property Id like in the Property object. I think I need to get Id so that I would be able to execute SQL request to find specific record. And also I don’t now how to commit update in the Property object in the trigger.

    trigger ChangeAvailable on Client_Property__c (before insert) {
    //initialize variable to store Property Id

    //find Property field
    for(Client_Property__c cp : Trigger.New){
        //get Property Id from Property field
    }

    //Make SQL request to Property object, get appropriate record, change value of
    //Available field to false
}

Could you suggest solution, please?

%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA

Solution:

trigger ChangeAvailable on Client_Property__c (after insert) {
   // initialize variable to store Property Id - 
   // let it be **propId**

   // your other logic

   List<Property> propsForUpdate = new List<Property>();
   Property prop = new Property(Id = propId);
   prop.Available = false;
   propsForUpdate.add(prop);

   // your other logic

    update propsForUpdate;
}

Best practice:
Update related records - in AFTER trigger

2 Вподобання

Thank you. But how can I get Propery Id from Property field in the Client_Property object so that I would be able to find appropriate record in the Property object to change Available field?

How can I assign Id to the propId?

I saw very similar topic in the forum:


:slight_smile:

Would you like to use trigger for the solution. The best practice says that trigger is the “last resort” and should be used if requirements have complex logic or some trigger already exists.

SOQL to select property id might be:
[SELECT id, (SELECT Property__c FROM Client_property__r) FROM Client__c WHERE id in :Trigger.newMap.keySet()]

Link to docs:

2 Вподобання
// ---------------------

List<Property__c> propsForUpdate = new List<Property__c>();
// your other logic
for (Client_Property__c clPoperty : Trigger.new) {
   // based on Master-detail relation in Client_Property (your schema)
   // object - field Property__c cannot be null
   propId = clProperty.Property__c;

   Property__c prop = new Property__c(Id = propId);
   prop.Available__c = false;
   propsForUpdate.add(prop);
}
// your other logic
update propsForUpdate;

// ---------------------

About Master-detail:

1 Вподобання

Thank you, it is the great solution!

trigger ChangeAvailable on Client_Property__c (after insert) {
    
    List<Property__c> propsForUpdate = new List<Property__c>();
    
    for (Client_Property__c clProperty : Trigger.new) {
       String propId = clProperty.Property__c;
    
       Property__c prop = new Property__c(Id = propId);
       
       prop.Available__c = false;
       propsForUpdate.add(prop);
    }
    update propsForUpdate;
}
1 Вподобання

Thank u for this solution it worked.