You need to sign in to do that
Don't have an account?

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
Thanks for your help.
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.CompanyThis 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: trigger.new){ // ?? 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.
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 : trigger.new){
// 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 : trigger.new){
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.
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.
Thank-you
Thanks
Shashikant