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
Ricki ReayRicki Reay 

Apex Trigger: Update Custom Checkbox on Financial Account Record if Linked to "Closed Won" Opportunity

Hi All,

For context and background, I am working with an organization that is using Financial Services Cloud. Within FSC, there is a custom object called "Financial Accounts" and within this object there is a record type called "Credit Card".

Within opportunities, we are creating a workflow where users are forced to link a valid credit card (financial account) record in order to close their credit card-related opportunities. I am looking to create a trigger (and test class for eventual deployment) on the financial account record so that a custom checkbox field called "Linked To Won Opportunity" (API: LinkedToWonOpp__c) is set to TRUE when it has been linked to an opportunity record where the Stage = Closed Won. Conversely, I want create the trigger so it also updates this field "Linked To Won Opportunity" to FALSE if the opportunity record in which it was linked is either changed back from Closed Won to any open status OR if the link between the financial account record and opportunity no longer exists (i.e. the user deleted the card record ID from the Financial Account (lookup) field placed on the opportunity).

Any help or direction is GREATLY appreciated.


shaik murthujavalishaik murthujavali
As per my understanding I think one trigger logic is enough.
Try the below code.
trigger sampleaccounttrig on opportunity (before insert, before update) {
    set<Id> Ids_tocheckTrue = new set<Id>();
    set<Id> Ids_tocheckFalse = new set<Id>();
        for(Opportunity opp{
            if(opp.StageName == 'Closed Won' && opp.Financial_Account__c != null && opp.Financial_Account__c != ''){
        if(trigger.oldmap.get(opp.Id).StageName != trigger.newmap.get(opp.Id).StageName || 
           trigger.oldmap.get(opp.Id).Financial_Account__c != trigger.newmap.get(opp.Id).Financial_Account__c){
               for(Opportunity opp{
                   if(trigger.newmap.get(opp.Id).StageName == 'Closed Won' && trigger.newmap.get(opp.Id).Financial_Account__c != '' 
                      && trigger.newmap.get(opp.Id).Financial_Account__c != null){
                   if((trigger.oldmap.get(opp.Id).StageName == 'Closed Won' && trigger.newmap.get(opp.Id).StageName != 'Closed Won') ||
                      trigger.oldmap.get(opp.Id).Financial_Account__c !=  trigger.newmap.get(opp.Id).Financial_Account__c){
    List<Financial_Account__c> list_toUpdate = new list<Financial_Account__c>();
    for(Financial_Account__c fin_acc :[Select Id, checkbox__c from Financial_Account__c where Id in :Ids_tocheckTrue]){
        fin_acc.checkbox__c = True;
    for(Financial_Account__c fin_acc :[Select Id, checkbox__c from Financial_Account__c where Id in :Ids_tocheckFalse]){
        fin_acc.checkbox__c = False;
    update list_toUpdate;

If works for you pls mark it as a best answer so that it helps others too.