function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
HNT_NeoHNT_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.
 
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; 
}

 
Best Answer chosen by HNT_Neo
Jainam ContractorJainam Contractor
Hi JH_Neo,

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:
 
X80_20__c = 
IF(
AND(Outlet_Type__c = 'Home Depot', Has_1_2_3x5_DULO__c = 'Yes'), IF(X80_20_Yes_or_No__c = 'Yes'), 'Yes', 
IF(X80_20_Yes_or_No__c = 'No'), 'No', NULL), NULL)
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

Leonardi KohLeonardi Koh
the syntax itself appears to be correct, but are you sure the obj query returns anything?
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.
HNT_NeoHNT_Neo
Since I"m trying to move data from one field (formula field) to a text field, would it be after update? I'm also not trying to insert any new records, so maybe my syntax is incorrect? 
Leonardi KohLeonardi Koh
The syntax is not incorrect but that is assuming your intention is for this to happen only if the Account record is updated or inserted.

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.
Jainam ContractorJainam Contractor
Hi JH_Neo,

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:
 
X80_20__c = 
IF(
AND(Outlet_Type__c = 'Home Depot', Has_1_2_3x5_DULO__c = 'Yes'), IF(X80_20_Yes_or_No__c = 'Yes'), 'Yes', 
IF(X80_20_Yes_or_No__c = 'No'), 'No', NULL), NULL)
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
This was selected as the best answer