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

How to write a trigger one object with value in another object when two objects have no relation?

I am trying to write a trigger that fires when my custom object "Vehicles" is either updated or new record is inserted.

If any of its two particular fields (In_service__c, Manufacturer__c)have been updated or new record has been inserted, then I want to compare these values to the fields Name__c and Subscriber__c of my custom object "Company".

If the values of the two fields in “Vehicles” match with the fields in “Company”, then assign the value in the field
Channel__c.Vehicles = Group__c.Company
This is what I have so far:
trigger VehicleUpdate on Vehicle_custom_object__c (before insert, before update){
    map<id, string, string> vehicleIDs = new map<id, string,string>();
      for(Vehicle_custom_object__c v:{
        // ?? vehicleIDs.add(v.Vehicle_ID__c);(it should now have a collection of all ID's and also the values od the two fields which I am trying to compare with
      Map<Id, Company__c> mapOfCompanies = new Map<Id,Company__c>();
      for (Company__c comp: [select  Name, Subscriber__c from Company__c where ??
       //this is where I am stuck at
       //if the values of Name__c.Company and Subscriber__c.Company match with the values of In_service__c.Vehicle and Vehicle.Manufacturer__c
      //then Vehicle.Channel__c = Company.Group__c

Thanks for your help.
Shashikant SharmaShashikant Sharma

1. For this you have to create a set of values that will go in where clause

like in this case one would be

Set<String> setCompanyName = new Set<String>();

2. then loop over Vehicle_custom_object__c list

for(Vehicle_custom_object__c vObj :{
// populate the setCompanyName  appropriately
        if( vObj.FieldAPIName__c != null ) {
         setCompanyName.add( vObj.FieldAPIName__c );

3. then do SOQL on the object that you want to fetch with a Where clause on the field which you want to compare, use in clause for set setCompanyName like

Where FieldToCompare__c in: setCompanyName 

4. Loop over the list returned by SOQL and prepare a map

Map<String, Object__c> mapNameToRecord = new Map<String, Object__c>();

in above Object__c will be the name of object that you queries

5. then loop over Vehicle_custom_object__c list again and use the map to find value of Object in step 4

for(Vehicle_custom_object__c vObj :{
      if( vObj.FieldAPIName__c != null ) {
            if( mapNameToRecord.containsKey( vObj.FieldAPIName__c )) {
            // find related object based on value
           Object__c obj = mapNameToRecord.get( vObj.FieldAPIName__c );

Let me know if you have queries in above.
Hello Shahikant, thanks for your reply.
I am trying to compare two fields of one custom object with the fields of another custom object with no relationship between the two objects.
As I understand your code,it  only takes one field for comparison?
My question is how to make it work for more than one field.
Shashikant SharmaShashikant Sharma
You could create another map for the other field and check contacins condition for that as well. Based on your need you could have OR ( || ) or AND ( && ) operators between both the conditions.