You need to sign in to do that
Don't have an account?
HNT_Neo
Trigger Before Insert Salesforce APEX to Update Field
Not sure if my syntax is correct, what I'm trying to achive is updating a text field (X80_20__c), where the data is coming from a formula text field (X80_20_Yes_or_No__c). The output from the formula field produces a Yes or No value.
This is the Apex trigger I attempted but its not populating the X80_20__c field. Any help would be appreciated or would a Apex Class be better to do in this situation? Also, I'm avoiding in using a workflow trigger or process builder for this.
This is the Apex trigger I attempted but its not populating the X80_20__c field. Any help would be appreciated or would a Apex Class be better to do in this situation? Also, I'm avoiding in using a workflow trigger or process builder for this.
trigger a8020 on Account (before insert,before update) { List obj = [SELECT Id,Name,X80_20_Yes_or_No__c,X80_20__c FROM Account WHERE (X80_20_Yes_or_No__c = 'Yes' OR X80_20_Yes_or_no__c = 'No') AND Outlet_Type__c = 'Home Depot' AND Has_1_2_3x5_DULO__c = 'Yes' ]; for (Account a: obj) { string s = a.X80_20_Yes_or_No__c; a.X80_20__c = s; } update obj; }
Is it necessary to use the APEX trigger in this scenario.??
Need one clarification, will the field X80_20__c contain any value other than that in X80_20_Yes_or_No__c.?
If not so, then i would suggest you to create X80_20__c field as a formula field and get the value from X80_20_Yes_or_No__c.
Formula should be something like this:
Can you please check the above formula. Please let me know if it works or you need any more assistance.
Mark this as the solution it it solved your purpose.
Thanks,
Jainam Contractor,
Salesforce Consultant,
Varasi LLC
www.varasi.com
All Answers
please note that you are using before insert and before update for the trigger so querying data at that point will produce record information unaffected by whatever data you might expect to show up after the insert or update process.
if that was intentional, then you might want to confirm first if the query produces the expected result from the debug log.
Please note that formula field does NOT invoke an update of the record, that's not how it works so if you were thinking that this trigger should be invoked when formula field decides to produce a YES or a NO after it evaluates itself, then that trigger is not going to work until you cause an update (for example by editing the Account record and saving it because the formula field X80_20_Yes_or_No__c is not going to cause an update on Account by itself)
note as well that this is not going to work on before Insert because in before Insert trigger... the Account object in question has yet to exist in the database so querying it will not give you the record.
you can try editing and saving the Account record (no need to change any value), and that should invoke the trigger above.
Is it necessary to use the APEX trigger in this scenario.??
Need one clarification, will the field X80_20__c contain any value other than that in X80_20_Yes_or_No__c.?
If not so, then i would suggest you to create X80_20__c field as a formula field and get the value from X80_20_Yes_or_No__c.
Formula should be something like this:
Can you please check the above formula. Please let me know if it works or you need any more assistance.
Mark this as the solution it it solved your purpose.
Thanks,
Jainam Contractor,
Salesforce Consultant,
Varasi LLC
www.varasi.com