+ Start a Discussion
elkid_24elkid_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?  
Best Answer chosen by elkid_24
sathishkumar periyasamysathishkumar periyasamy
As discussed, trigger is working if you use "CEQ_Sub1__c" instead of "CEQ_Sub__c". I am glad you issue resolved.

All Answers

sathishkumar periyasamysathishkumar periyasamy
My unserstanding is when ProductChage__c field value changed need to populate SubProduct__c lookup value from "some object"(As of now I don't have lookup field object name). Please refer below sample trigger. Please let me know if you have any question.

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);
}
}
}
}
elkid_24elkid_24
Satishkumar,

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
if(setProductChange.size() > 0) 
{ 
map<String, Id> mapSubProduct = new map<String, Id>(); 
list<CEQ_Sub> lstSubProduct = [Select Id from CEQ_Sub where Name IN:setProductChange]; 
for(SubProduct iterator : lstSubProduct) 
{ 
mapSubProduct.put(iterator.-------, iterator.-------); 
}



 
sathishkumar periyasamysathishkumar periyasamy
@Elkid, can you please confirm "CEQ_Sub__c" is your object name?
sathishkumar periyasamysathishkumar periyasamy
please replace with below code
if(setProductChange.size() > 0) 
{ 
map<String, Id> mapSubProduct = new map<String, Id>(); 
list<CEQ_Sub__c> lstSubProduct = [Select Id from CEQ_Sub__c where Name IN:setProductChange]; 
for(CEQ_Sub__c iterator : lstSubProduct) 
{ 
      mapSubProduct.put(iterator.Name, iterator.Id); 
}

 
elkid_24elkid_24
Hi Sathiskumar,

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:
 
Error: Invalid Data. 
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?
sathishkumar periyasamysathishkumar periyasamy
Use this
list<CEQ_Sub__c> lstSubProduct = [Select Id, Name from CEQ_Sub__c where Name IN:setProductChange];
Instead fof
list<CEQ_Sub__c> lstSubProduct = [Select Id from CEQ_Sub__c where Name IN:setProductChange];


 
elkid_24elkid_24
If feels like progress. Unfortunately, different message based on invalid data type
 
.Error: Invalid Data. 
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
sathishkumar periyasamysathishkumar periyasamy
Do you have time to talk over the phone or skype?
elkid_24elkid_24

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

sathishkumar periyasamysathishkumar periyasamy
NP. Just sent a skype request. Can you please send "TerritoryChange__c" object fields screen shot (Your Name --> Setup --> Build --> Create --> Objects --> "TerritoryChange" object --> take "Custom Fields & Relationships" section and send to me)
sathishkumar periyasamysathishkumar periyasamy
As discussed, trigger is working if you use "CEQ_Sub1__c" instead of "CEQ_Sub__c". I am glad you issue resolved.
This was selected as the best answer
elkid_24elkid_24
Thanks for the help Satishkumar!!  Here is the complete code that worked for me. 
 
trigger ChangeSubProduct1 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<CEQ_Sub1__c> lstSubProduct = [Select Id, Name from CEQ_Sub1__c where Name IN:setProductChange]; 
for(CEQ_Sub1__c iterator : lstSubProduct) 
{ 
      mapSubProduct.put(iterator.Name, iterator.Id); 
}
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);
}
}
}
}