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
bookerawardbookeraward 

trigger doesn't work?

 I am trying to fire a trigger on object ‘Router_custom_object__c' when either ‘In_service__c’ || ‘Channel_designation_del__c' || ‘Router_designation' have been updated or new object is created.
IF the fields are updated or new object is created, then I compare the 3 fields of 'Router_custom_object__c '(concatenated together) with field ‘all_fields' of custom object ‘TRU_Groups__c' 
and if they match then 
 Router_custom_object__c. TRU_c = TRU_Groups__c .Name
This is how my values of field ‘all_fields__c' looks like:
 No, Aftermarket, Subscriber
My code:
Trigger TruFieldUpdate on Router_custom_object__c (before insert, before update) {
  Map<Id, Router_custom_object__c> changedRouters = new Map<Id, Router_custom_object__c>();
  Map<Id, String> afTargets = new Map<Id, String>(); // Map of Router->All_field__c 
    
    // Build a list of routers whose groups MAY have changed
    if(trigger.isInsert) {
        // All new routers
        for(Router_custom_object__c newR: trigger.new) {
            changedRouters.put(newR.Id, newR);
        }
    } else {
        // Some updated routers, if the relevant fields have been updated
        for(Router_custom_object__c oldR: trigger.old) {
            Router_custom_object__c newR = trigger.newMap.get(oldR.Id);
            if( (oldR.In_service__c != newR.In_service__c) || (oldR.Channel_designation_del__c != newR.Channel_designation_del__c) || (oldR.Router_designation__c != newR.Router_designation__c) ) {
                changedRouters.put(newR.Id, newR);
            }
        }
    }
    
    // The string we're building here matches the computation done for TRU_Groups__c.all_fields__c
    // Map this computer field to router Id.
    for(Router_custom_object__c oneR: changedRouters.values()) {
        afTargets.put(oneR.Id, oneR.In_service__c+','+oneR.Channel_designation_del__c+','+oneR.Router_designation__c);
    }
    
    // Get all the group records matching our afTargets values.
    List<TRU_Groups__c> foundGroups = [select Name, all_fields__c from TRU_Groups__c where all_fields__c in :afTargets.values()];
    
    // For each router in afTargets, find the new Group value
    for(Id rId: afTargets.keySet()) {
        String routerFields = afTargets.get(rId);
        for(TRU_Groups__c oneG: foundGroups) {
            if(oneG.all_fields__c == routerFields) {
                // finally, change the router record.
                Router_custom_object__c r = changedRouters.get(rId);
                r.TRU__c = oneG.Name;
                break;
            }
        }
    }
    
}

 
Jai ChaturvediJai Chaturvedi
Hi,

Whats the problem?
Jai ChaturvediJai Chaturvedi
Its because you are creating new instance of Router_Custom_Object__c:
Router_custom_object__c r = changedRouters.get(rId);


Try this:
changedRouters.get(rId).TRU__c = oneG.Name;
Jai ChaturvediJai Chaturvedi
Why are using break statement? Can you remove that and check?