You need to sign in to do that
Don't have an account?
elkid_24
Trigger to update one field with another
Hello:
I've been searching for something I believe is simple but I have not been able to find a trigger that helps me reproduce what I want. I have an object TerritoryChange__c. In here, I have two fields, a dropdown ProductChange__c and a lookup field called SubProduct__c. When a user updates the ProductChange__c, I'd like to insert/update the SubProduct__c field with the value from ProductChange. Does anyone have a trigger example that does this?
I've been searching for something I believe is simple but I have not been able to find a trigger that helps me reproduce what I want. I have an object TerritoryChange__c. In here, I have two fields, a dropdown ProductChange__c and a lookup field called SubProduct__c. When a user updates the ProductChange__c, I'd like to insert/update the SubProduct__c field with the value from ProductChange. Does anyone have a trigger example that does this?
All Answers
Note : Please fill "......" with right object and field names.
trigger ChangeSubProduct on TerritoryChange__c (before insert, before update) {
Set<String> setProductChange = new Set<String>();
for(TerritoryChange__c iterator : Trigger.New) {
if(Trigger.oldMap != null) {
TerritoryChange__c objOld = Trigger.oldMap.get(iterator.Id);
if(String.isNotBlank(Iterator.ProductChange__c) && Iterator.ProductChange__c != objOld.ProductChange__c) {
setProductChange.add(Iterator.ProductChange__c);
}
}
else {
if(String.isNotBlank(Iterator.ProductChange__c) ) {
setProductChange.add(Iterator.ProductChange__c);
}
}
}
if(setProductChange.size() > 0) {
map<String, Id> mapSubProduct = new map<String, Id>();
list<.......> lstSubProduct = [Select .... from....where .... IN:setProductChange];
for(..... iterator : lstSubProduct) {
mapSubProduct.put(iterator.-------, iterator.-------);
}
for(TerritoryChange__c iterator : Trigger.New) {
if(String.isNotBlank(Iterator.ProductChange__c) && mapSubProduct.size() > 0 && mapSubProduct.get(Iterator.ProductChange__c) != null) {
iterator.SubProduct__c = mapSubProduct.get(Iterator.ProductChange__c);
}
}
}
}
Thank you so much for the example for the response. I wish I had the more experience with triggers to complete this but I think I just confused myself even more :)
I've attempted to fill out the code below with what I believed to be correct values but I'm getting compile errors. I've tried several combinations but I cannot solve this. I wasn't sure what would go in the ------- section.
For reference: The SubProduct__c field name is SubProduct and it is related Object Name CEQ_Sub. Let me know if this clarifies the naming. Thanks again for your help
Thank you so much. I was able to save the trigger and I was very happy. Until I tested it and received the following message:
Review all error messages below to correct your data.
Apex trigger ChangeSubProduct1 caused an unexpected exception, contact your administrator: ChangeSubProduct1: execution of BeforeInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: CEQ_Sub__c.Name: Trigger.ChangeSubProduct1: line 22, column 1
Any ideas on why?
Review all error messages below to correct your data.
Apex trigger ChangeSubProduct1 caused an unexpected exception, contact your administrator: ChangeSubProduct1: data changed by trigger for field SubProduct: id value of incorrect type: a0H8000000KCfm7EAD
Thank you for offereing. I'm in back to back meetings through 3pm est. :(
I could certainly chat with you over skype and share screenshots with you. Skype:dakid24