You need to sign in to do that
Don't have an account?
Sonia Genesse
Auto update a lookup field with a Trigger on a Customer Object
Hi All,
I have a Custome Object called Service_Record_History__c that requires a Trigger to update a Lookup field named Installed_Product__c.
This is the first trigger I am attempting to write, so please bare with me! :)
Below is the Trigger I have written, and I'm receiving this error: Error: Compile Error: Illegal assignment from List<SVMXC__Installed_Product__c> to Id at line 6 column 13
Also, in my Select statement I'm concerned that it is selecting ALL SVMXC__Installed_Product__c ID's rather than the specific related ID that is needed.
trigger InstallProductID_Trgr on Service_Record_History__c ( before insert, before update ) {
for (Service_Record_History__c c :Trigger.new) {
if(c.Installed_Product__c <> null)
c.Installed_Product__c = [SELECT Name FROM SVMXC__Installed_Product__c];
}
}
I have a Custome Object called Service_Record_History__c that requires a Trigger to update a Lookup field named Installed_Product__c.
This is the first trigger I am attempting to write, so please bare with me! :)
Below is the Trigger I have written, and I'm receiving this error: Error: Compile Error: Illegal assignment from List<SVMXC__Installed_Product__c> to Id at line 6 column 13
Also, in my Select statement I'm concerned that it is selecting ALL SVMXC__Installed_Product__c ID's rather than the specific related ID that is needed.
trigger InstallProductID_Trgr on Service_Record_History__c ( before insert, before update ) {
for (Service_Record_History__c c :Trigger.new) {
if(c.Installed_Product__c <> null)
c.Installed_Product__c = [SELECT Name FROM SVMXC__Installed_Product__c];
}
}
This trigger iterates over all of the new service record history objects, and adds their serial numbers to a set. Then we query for all the products whose name matches that serial number. Then we build a map of serial numbers to products. Then we iterate over all the new service records again and if we find a product that matches then we set the lookup.
All Answers
I have created one scenario related to your query.. It not exactly what u are trying to do.. but its logic is relatedd to your query... I have auto populated the website filed on account if it is null at the time of insertion... and the second logic is on SLA with different values for gold ,silver etc..
*****APEX TRIGGER****
***APEX CLASS***
I hope this will help you a little bit... If you are having still doubt you can go ahead with your new queries... :)
Yes the Installed_Product__c is a lookup relationship and the Service_Record_History__c has a field named Serial_Number__c, it corresponds to the SVMXC__Installed_Product__c object with the field name as Installed_Product_ID. I'm tring to have this field update automattically and not have the end user have to populate this field. So if the Installed_Product_ID (lookup) isn't populated on the
Service_Record_History__c object record, the Related List on the SVMXC__Installed_Product__c object has no data.
The data that is in the Service_Record_History__c object is data that is loaded from a legacy system. I need to see it in a RL on the Installed Product.
Does this help clarify the question?
This trigger iterates over all of the new service record history objects, and adds their serial numbers to a set. Then we query for all the products whose name matches that serial number. Then we build a map of serial numbers to products. Then we iterate over all the new service records again and if we find a product that matches then we set the lookup.
~Sonia~
Looks like the Trigger is working perfectly when I create or clone a record. In the case of the
Service_Record_History__c object these will only be imported (using Data Loader) for historical purposes, and no new records will ever be created.
How then does this Trigger work to support these loaded records?
However, if you are really worried about it, you could update whatever datasource you are importing and set the Installed_Product__c field to be the id of the correct SVMXC__Installed_Product__c prior to data loading and then you would not need the trigger.