Booster15
Booster15

Reputation: 1

How to update related record field on delete of record?

I have 2 Objects based on Lookup relationship i.e 1) Assign 2) Inventory.

Relation is 1 inventory can be assigned to many (1 to many).

Inventory has picklist values called "status" contains 2 values i.e "Assigned" and "Backup". So when any inventory is assigned, value changes from "Backup" to "Assigned".

Whenever any record of assign object is deleted, automatically related object (Inventory) field "status" should get updated from "Assigned" to "Backup".

Or else I will need to manually change the status of inventory object from "assigned" to "backup" whenever any record is deleted from assign, which I don't want.

I tried the flow, but I am confused exactly which option I should select. For reference I had attached the snaps.enter image description here enter image description here enter image description here

Upvotes: 0

Views: 543

Answers (2)

Nivetha Mohan
Nivetha Mohan

Reputation: 1

  1. Create a Trigger on the Assign Object: You will create a trigger that executes whenever a record in the Assign object is deleted.
  2. Check for the Deleted Record: The trigger will check for deleted records and identify the related Inventory record.
  3. Update the Inventory Status: The trigger will update the status field of the related Inventory record from "Assigned" to "Backup".

Upvotes: 0

Carlos Carvalho
Carlos Carvalho

Reputation: 1

You need to create a trigger with BEFORE DELETE context.

trigger Assign on Assign__c (  before delete ) {

  if( Trigger.isBefore )
  {
    if( Trigger.isDelete )
    {
      List< Inventory__c > lstInventory = [Select Id, Status__c From Inventory__c Where Assign__c =: newMap.kseyset()];
      for( Inventory__c i : lstInventory ){
        i.Status__c = 'Backup';
      }
      update lstInventory;
    }
  }
 }

You need to check if newMap is filled when delete records context is fired if not try to use oldMap.

Don't forget to use best pratices to develop your code.

I hope to help you.

Upvotes: 0

Related Questions