I have two objects parent object Vehicles__c and a child object RegistrationNumbers__c. When ever a RegistrationNumbers__c is created or updated I have a trigger which will update RegNumbers__c field on Vehicles__c with Number__c field values of child record. as shown below

If I have 1 parent record with 3 child records (Number__c =123,Number__c =245, Number__c =444) then my trigger will update parent RegNumbers__c = 123/245/444

Everything is working fine for Create and update,but I have a requirement if the particular child record is deleted then I should delete only that Number__c value from RegNumbers__c on parent. I tried the below trigger but it is deleting all the values can anyone help me with this ?

Trigger :

trigger deletevals on RegistrationNumbers__c (before delete) {

List<Vehicles__c> vlist = new List<Vehicles__c>();
  Set<Id> vids= new Set<Id>();

  for (RegistrationNumbers__c  r : Trigger.old){


 List<Vehicles__c> v=[Select Id,RegNumbers__c,(Select Id,Number__c from RegistrationNumbers__r) from Vehicles__c where ID IN: vids]; 

 for(Vehicles__c vh : v)
        for (RegistrationNumbers__c  rn: vh.RegistrationNumbers__r) {
           String number =  rn.Number__c ;
           String removeval = vh.RegNumbers__c;          
               removeval = removeval.remove(number);// It is removing all the values 
               vh.RegNumbers__c = removeval; 




    update vlist;


1 Answer 1


Make it easy on yourself if you are not going to use DLRS. Recalculate on insert, update, delete, and undelete.

You can make it more robust by adding recursion checks and if the Reg number actually changed or not to exclude those updates as well

trigger populateRegNumbers on RegistrationNumbers__c (after insert, after update, after delete, after Undelete) {

    //Map of Vehicles to update
    Map<Id, Vehicles__c> vehToUpdate = New Map<Id, Vehicles__c>();

    //populate default value of null so if there are none remaining then the value is updated - If it is possible a vehicle will not have an registration then you will need to skip those with no registrations
    for (RegistrationNumbers__c r : trigger.isDelete ? trigger.old : trigger.new) {
                New Vehicles__c(
                        Id = r.Vehicle__c,
                        RegNumbers__c = null



    for (Vehicles__c v : [Select Id,RegNumbers__c, (Select Id,Number__c from RegistrationNumbers__r) from Vehicles__c where ID IN:vehToUpdate.keySet()]) {
        String [] tmp = New String[]{};
        for (RegistrationNumbers__c rn: v.RegistrationNumbers__r) {

        if (!tmp.isEmpty())
            vehToUpdate.get(v.id).RegNumbers__c = string.join(tmp, '/');

    update vehToUpdate.values();
  • Thanks for your response, I tried your code it is deleting all the values in Regnumbers
    – Learner
    Commented Apr 20, 2017 at 14:56
  • @Learner - I found the issue. Check code now. Update in line after if (!tmp.isEmpty())
    – Eric
    Commented Apr 20, 2017 at 15:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .