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
Ramya BalakrishnanRamya Balakrishnan 

How to update Account record in account object TriggerHandler class

Hello All
I have scenario where i need to calculate two fields Data_quality_score and Data_quality_desc on account object when a new account is created or updated based on other field values.
I have the following trigger handler class. In which i have created one method called PopulateDataQualityScoreFields to calculate the data quality score field values before saving the record. 

public class AccountTriggerHandler extends TriggerFrameWork {
    private List<Account> newAccountList;
    private Map<Id, Account> oldAccountMap;

    public AccountTriggerHandler() {
        this.newAccountList = (List<Account>) Trigger.new;
        this.oldAccountMap = (Map<Id,Account>) Trigger.oldmap; 
    }
    public override void beforeInsert() {
        for(Account account : newAccountList) {
            if(account.Firm_Revenue_Last_Known__c != null) {
                account.Firm_Revenue__c = account.Firm_Revenue_Last_Known__c;
            }
            if(account.Firm_Revenue_Last_Known__c == null && account.Firm_Revenue_2_Years_Ago__c != null) {
                account.Firm_Revenue__c = account.Firm_Revenue_2_Years_Ago__c;
            }
            if(account.Firm_Revenue_Last_Known__c == null && account.Firm_Revenue_2_Years_Ago__c == null
                    && account.Firm_Revenue_3_Years_Ago__c != null) {
                account.Firm_Revenue__c = account.Firm_Revenue_3_Years_Ago__c;
            }
            if(account.Lead_Type__c == 'Household') {
                account.recordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Household').getRecordTypeId();
            }
            if(account.Lead_Type__c == 'Firm') {
                account.recordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Firm').getRecordTypeId();
            }
        }
        PopulateDataQualityScoreFields();
    }
    public override void beforeUpdate() {
        for(Account account : newAccountList) {
            if(account.Firm_Revenue_Last_Known__c != null && account.Firm_Revenue_Last_Known__c != oldAccountMap.get(account.Id).Firm_Revenue_Last_Known__c) {
                account.Firm_Revenue__c = account.Firm_Revenue_Last_Known__c;
            }
            if(account.Firm_Revenue_Last_Known__c == null && account.Firm_Revenue_2_Years_Ago__c != null
                    && account.Firm_Revenue_2_Years_Ago__c != oldAccountMap.get(account.Id).Firm_Revenue_2_Years_Ago__c) {
                account.Firm_Revenue__c = account.Firm_Revenue_2_Years_Ago__c;
            }
            if(account.Firm_Revenue_Last_Known__c == null && account.Firm_Revenue_2_Years_Ago__c == null
                    && account.Firm_Revenue_3_Years_Ago__c != null
                    && account.Firm_Revenue_3_Years_Ago__c != oldAccountMap.get(account.Id).Firm_Revenue_3_Years_Ago__c) {
                account.Firm_Revenue__c = account.Firm_Revenue_3_Years_Ago__c;
            }
        }
        PopulateDataQualityScoreFields();
    }

 public void PopulateDataQualityScoreFields( )
   {
    String DQDescription;
    Integer DQScore;
    for(Account account : newAccountList) {
    if (account.Lead_Type__c == 'Firm') {
        if(account.Data_Quality_Score__c <> 100){
          DQDescription = 'Missing: ';
          if(account.Owner.Profile.Name == null || account.Owner.Profile.Name == 'Integration Profile'){
             DQDescription = DQDescription + 'Owner,';
             DQScore = DQScore + 10;
          }
          if(account.AuMine_External_Id__c <> null && (account.Regional_Firms_Broker__c == null ||
             account.Regional_Firms_Broker__c == 'Integration Profile')){
             DQDescription = DQDescription + 'RF Broker';
             DQScore = DQScore + 5;
          } 
          if(account.Related_Contacts__c == null || account.isVictim__c == True){
            DQDescription = DQDescription + 'Contact, ';
            DQScore = DQScore + 15;
          }
          if(account.Phone_MDM__c == null || account.Phone_MDM__c.length() !=10){
            DQDescription = DQDescription + 'Phone, ';
            DQScore = DQScore + 15;
          } 
          if(account.Firm_Revenue__c == null || account.Firm_Revenue__c == 0){
            DQDescription = DQDescription + 'Firm Revenue, ';
            DQScore = DQScore + 15;
          } 
          if(account.AuMine_External_Id__c <> null && account.Tax_ID__c == null){
             DQDescription = DQDescription + 'Tax ID, ';
             DQScore = DQScore + 5;
          }
          if(account.hasBusinessAddress__c == null || account.hasBusinessAddress__c == 0){
            DQDescription = DQDescription + 'Biz Address,';
            DQScore = DQScore + 15;
          } 
          if(account.Last_Known_PL_Expiration_Date__c == null){
            DQDescription = DQDescription + 'Last Known PL Expiration Date, ';
            DQScore = DQScore + 10;
          }
          if(account.Missing_Renewal_Opportunities__c > 0){
            DQDescription = DQDescription + account.Missing_Renewal_Opportunities__c + 'Renewal Opportunity';
            DQScore = DQScore + 10;
          }
          account.Data_Quality_Score__c = DQScore ; 
          account.Data_Quality_Description__c = DQDescription;
                                                    
       }
    } 
   } 
      
   } 
    
}

But it is not updating. The list newAccountList doesnt have any value in the PopulateDataQualityScoreFields method in the debug log. So the loop exits without calculating. I dont see the constructor statements executed in the debug log. Whats wrong in the code ? Can anybody help with this ?
Which is the best way to achieve this Triger or Flow ?
Thanks All.

Thnaks
Ramya Balakrishnan
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Usually the best way would be using Flow because you can change it in production or you can turn off this requirement directly if not needed. 

I will check the logic of trigger and let you know the issue.

Thanks,
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

While calling the method can you call the method with newlist as parameter as below.
 
PopulateDataQualityScoreFields(newAccountList);

Also change the method to accept parameter as below.
 
public void PopulateDataQualityScoreFields(List<Account>newAccountList  )

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
Ramya BalakrishnanRamya Balakrishnan
Hi Praveen

 Thanks for the reply. I have tried passing newAccountList as a parameter like you suggested. I am getting the error (attached the error info)User-added image
Also I have created flow for this requirement. But the flow has lot of decision element and also i need to calculate the Data_quality_score and data_quality_desc for different conditions like below

if(account.Related_Contacts__c == null || account.isVictim__c == True){
            DQDescription = DQDescription + 'Contact, '
}
else{
            DQScore = DQScore + 15;
   }

(Please ignore PopulateDataQualityScoreFields method body in my previous post). I have to modify the PopulateDataQualityScoreFields for all the condition like above . I am not sure how will i achieve this if else condition in Flow decision element(I have attached my flow screenshot). That is why i am using trigger. Please suggest best solution for this scenario.

User-added image
Thanks .
Ramya Balakrishnan
Ramya BalakrishnanRamya Balakrishnan
Hi Praveen
    Sorry I was trying to add a update statement in the  PopulateDataQualityScoreFields method(that is not needed ). That is why i got the error. I removed that . But still the fields are not getting updated. I have a empty newAccountList in the debug log .

Thanks
Ramya 
      
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Ramya,

Is it possible to connect over gmeet so can debug and check the issue ?

Thanks,
 
Ramya BalakrishnanRamya Balakrishnan
Hi Praveen

   Sorry it is not possible to connect over Gmeet.
Thanks
Ramya