You need to sign in to do that
Don't have an account?
AlSawtooth
Straightforward field update trigger
My trigger seems to work in the debug log but doesn't update the value in the UI - I have no idea why! There is a lookup on the Opp to a custom Segment object; I'm trying to update the Source Code from the Segment onto a Source Code field on the Opp.
trigger UpdateSourceCode on Opportunity (before insert, before update) { List<Opportunity> opp = [select Id, Segment__c from Opportunity where Id in :trigger.new and Source__c = null and Segment__c != null]; List<Segment__c> segments = [select Id, Source_Code__c from Segment__c where Source_Code__c != null]; Map<Id, Segment__c> segmap = new Map<Id, Segment__c>(segments); for(Opportunity o : opp){ if(segmap.containskey(o.Segment__c)){ System.debug('Does segmap contain key: ' + segmap.containskey(o.Segment__c)); List<Segment__c> OneSeg = [select Source_Code__c from Segment__c]; System.debug('oneseg size: ' + OneSeg.size()); if(OneSeg.size()>0){ o.Source__c = OneSeg[0].Source_Code__c; System.debug('opp source code: ' + o.Source__c); System.debug('effort segment source code: ' + OneSeg[0].Source_Code__c); //these two debugs are the same (correct) value - so why isn't it updating? } } } }
Firstly I am not seeing any reason why it is not working. It should work. Secondly I could not resist to optimize/bulkify your code. Here is what I would write, It is same code so the end result should be same. I think you should check for some items.
1. See if there is any workflow rule that is causing this value to reset to null.
2. See if there is any other trigger active on the same object.
3. Check the entire log to see if there is any code that is causing any rollback of the entire update (this is applicable for update only.)
4. Double check if your are checking the correct field. You may try workbench to query related Opportunity records "Select Source__c From Opportunity Where Id='<put a Id that you updated>'.
Let me know if you find anything.
--Akram
All Answers
Firstly I am not seeing any reason why it is not working. It should work. Secondly I could not resist to optimize/bulkify your code. Here is what I would write, It is same code so the end result should be same. I think you should check for some items.
1. See if there is any workflow rule that is causing this value to reset to null.
2. See if there is any other trigger active on the same object.
3. Check the entire log to see if there is any code that is causing any rollback of the entire update (this is applicable for update only.)
4. Double check if your are checking the correct field. You may try workbench to query related Opportunity records "Select Source__c From Opportunity Where Id='<put a Id that you updated>'.
Let me know if you find anything.
--Akram
Also, ask Akram mentioned, you need to bulkify your code. Make sure there aren't any SQOL Queries in for loops, make sure you detect changes before processing code (i.e. if there isn't a change go Source_Code__c don't run queries that you don't need). Definitely read through this: https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices
I would also strongly suggest you get this code out of a trigger and into a trigger handler class.
@AlSawtooth
Your code seems to be workable except the query you wrote in the for loop,not sure why you wrote a duplicate kind of query in the for loop, which will definite hit governor limit in bulk update operation with limit > 100 ?? . Can you please share the complete debug log? just to see if there is any other trigger/workflow rule is firing that over writes the value you write into the field.
Thanks,
balaj
Glad that it worked. Now I have a suggestion for you. In my opinion this kind of simple field update should be handled by workflow rules. Trigger is for advanced/complex kind of update.
- Create a WF rule on Opportunity.
- Add a criteria Source__c = null and Segment__c != null.
- Add a field update action. Chose field to update as Source__c and value to be a formula, Segment__r.Source_Code__c. I am assuming the relationship name to be Segment__r. If it is different please change it.
--Akram@Craig: I'm hoping to be able to edit this field on the Opp, so a formula field won't work for me.
@Akram: Thank you - this is perfect. I'm so glad I don't have to use code for this!! Thanks for all your help.