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
sumit dsumit d 

trigger to update a custom field quality score

Hi All,
my requirement is to count the % of quality score when a new account is inserted or updated Account.
i created a map According to record type .And inside it a map for fields and thier scoring.but its not calculating the value of Quality score.
Can you help me out with this trigger?
public without sharing class AccountTriggerHelper {
    public static List<Account> newAccount = new List<Account>();
    public static List<Account> oldAccount = new List<Account>();
    public static Map<Id, Account> newMapAccount = new Map<Id, Account>();
    public static Map<Id, Account> oldMapAccount = new Map<Id, Account>();
    
    public static boolean runTrigger = TRUE;
    
    public static Map<String, Map<String, Integer>> mapRecordTypeToScores = new Map<String, Map<String, Integer>> {
        
        'Cachet' => new Map<String, Integer> {
                        'ACH_Provider__c' => 5,
                        'Payroll_Software__c' => 3, 
                        'HR_Software__c' => 3,
                        'Payroll_Tax_Processor__c' => 3,
                        'Current_Bank__c' =>  3,
                        'Payroll_Business_Consultant__c' => 2,
                        'Time_Attendance__c' => 2,
                        'CPA_Accounting_Firm__c' => 2
                     },
       'Time_Rack' =>  new Map<String, Integer> {
                        'Time_Attendance__c' => 5,
                        'CPA_Accounting_Firm__c' => 3,
                        'Payroll_Software__c' => 3,
                        'Current_Bank__c' =>  3,    
                        'HR_Software__c' => 3,
                        'Payroll_Business_Consultant__c' => 2,
                        'ACH_Provider__c' => 2,
                        'Payroll_Tax_Processor__c' => 2
                      },          
       'PTM' => new Map<String, Integer> {
                        'Payroll_Tax_Processor__c' => 5,
                        'ACH_Provider__c' => 3,
                        'Payroll_Software__c' => 3,
                        'Current_Bank__c' =>  3,
                        'Payroll_Business_Consultant__c' => 2,
                        'Time_Attendance__c' => 2,
                        'HR_Software__c' => 2,
                        'CPA_Accounting_Firm__c' => 2
                    },
       'SBS' => new Map<String, Integer> {
                        'Payroll_Software__c' => 5,
                        'ACH_Provider__c' => 3,
                        'Payroll_Tax_Processor__c' => 3,
                        'Current_Bank__c' =>  3,
                        'Time_Attendance__c' => 3,
                        'Payroll_Business_Consultant__c' => 2,
                        'HR_Software__c' => 2,
                        'CPA_Accounting_Firm__c' => 2
                    }               
    };
         
    //To update Quality Score Field on Account 
    public static void updateQualityScore(){
        //Map<ID,Schema.RecordTypeInfo> rt_Map = Account.sObjectType.getDescribe().getRecordTypeInfosById();
        // List<Account> accListToUpdate = new List<Account>();
         for(Account acc : newAccount){
            //Integer fieldValue = 0;
             Integer totalScoreAchieved = 0;
            Integer totalScorePossible = 0;
            
            String recordTypeDeveloperName = acc.RecordType.developerName;
            if( recordTypeDeveloperName != null ) {
                
                Map<String, Integer> mapFieldAPINameToScore = mapRecordTypeToScores.get( recordTypeDeveloperName );
                
                totalScoreAchieved = 0;
                
                if( mapFieldAPINameToScore != null ) {
                 
                    for( String fieldAPIName : mapFieldAPINameToScore.keySet() ) {
                        Object fieldValue = acc.get( fieldAPIName );
                        Integer scoreForTheField = mapFieldAPINameToScore.get( fieldAPIName );
                        totalScorePossible += scoreForTheField;
                        if( fieldValue != null && String.valueOf( fieldValue ) != 'Other' ) {
                            totalScoreAchieved += scoreForTheField;
                         }
                    }
                }

                if( totalScorePossible > 0 ) {
                    acc.Quality_Score__c = ( totalScoreAchieved * 100 ) / totalScorePossible;   
                }
                //accListToUpdate.add( acc );
            }
        }
       // update accListToUpdate;
    }
what i am missing in it?
Any suggestions?
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Sumit

What is the debug value of totalScorePossible ?

Cheers!!!
sumit dsumit d
Hi ,
the value of total scorePossible is zero
can you tell me where i am going wrong?
 
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Sumit

You will not get the value for 
String recordTypeDeveloperName = acc.RecordType.developerName

Use RecordTypeId in such case or mapping for the same.

Cheers!!!