0

Hello guy's I'm trying to update the picklist field from this trigger but it is not getting updated when I printed the value it is showing NULL. Below is my trigger Handler class and Trigger. Rest Everything is working as expected.

public with sharing class CreditReportTriggerHandler {

    public static void onBeforeUpdate(List<creditchecker__Credit_Report__c> listCreditReports){
        System.Debug('inside onBeforeUpdate ::');
        updateCreditDecision(listCreditReports);
    }

    public static void onAfterUpdate(List<creditchecker__Credit_Report__c> listCreditReports){
        System.Debug('inside onAfterUpdate ::');
        updateContactAndAccount(listCreditReports);
    }

    private static void updateCreditDecision(List<creditchecker__Credit_Report__c> listCreditReports){      

        if(listCreditReports != null && listCreditReports.size() > 0){
        for(creditchecker__Credit_Report__c oCredit : listCreditReports){
            System.debug('oCredit :: ' + oCredit);
            System.debug('oCredit.Fraud_Alert_Count__c :: ' + oCredit.Fraud_Alert_Count__c);
            System.debug('oCredit.creditchecker__Average_Score__c :: ' + oCredit.creditchecker__Average_Score__c);
            System.debug('oCredit.Decision__c :: ' + oCredit.Decision__c);
            creditchecker__Credit_Report__c oCreditReport = new creditchecker__Credit_Report__c();
            oCreditReport.Id = oCredit.Id;

            if((String.isBlank(oCredit.Decision__c) && oCredit.Fraud_Alert_Count__c == 0 && oCredit.creditchecker__Average_Score__c > Decimal.valueOf(System.Label.CreditDecision)) ||(oCredit.Decision__c != null && oCredit.Decision__c.equalsIgnoreCase('Approve')))
            {oCreditReport.Decision__c = 'Approve';}

            else if((String.isBlank(oCredit.Decision__c) && oCredit.creditchecker__Average_Score__c > 0 && oCredit.creditchecker__Average_Score__c < 600) || (oCredit.Decision__c != null && oCredit.Decision__c.equalsIgnoreCase('Deny')))
            {oCreditReport.Decision__c = 'Deny';}

            if(oCredit.creditchecker__Status_Description__c != null){ 
                if((oCredit.creditchecker__Status_Description__c.Contains('Frozen') || oCredit.creditchecker__Status_Description__c.Contains('Freeze')) && (oCredit.Decision__c != null && (!oCredit.Decision__c.equalsIgnoreCase('Approve') && !oCredit.Decision__c.equalsIgnoreCase('Deny'))))
                    oCreditReport.Decision__c = 'Frozen';
            }
            System.debug('oCreditReport :: ' + oCreditReport);

        }  }        

    }

    private static void updateContactAndAccount(List<creditchecker__Credit_Report__c> listCreditReports){
        Set<Id> setContactId = new Set<Id>();
        Map<Id,creditchecker__Credit_Report__c> mapContactCreditReport = new Map<Id,creditchecker__Credit_Report__c>();
        Map<Id,Contact> mapAccountContact = new Map<Id,Contact>();
        System.debug('listCreditReports :: ' + listCreditReports);
        for(creditchecker__Credit_Report__c oCredit : listCreditReports){
            System.debug('inside updateContactAndAccount -- oCredit :: ' + oCredit);
            System.debug('inside updateContactAndAccount -- oCredit.creditchecker__Status__c :: ' + oCredit.creditchecker__Status__c);
            System.debug('inside updateContactAndAccount -- oCredit.Decision__c :: ' + oCredit.Decision__c);
            if(oCredit.creditchecker__Parent_Record_Id__c != null && oCredit.creditchecker__Parent_Record_Id__c.startsWith('003') && oCredit.creditchecker__Status__c == 'Completed'){
                setContactId.add(oCredit.creditchecker__Parent_Record_Id__c);
                mapContactCreditReport.put(oCredit.creditchecker__Parent_Record_Id__c, oCredit);  
            } 
        }

        List<Contact> lstContacts = new List<Contact>();

        if((mapContactCreditReport != null) && !mapContactCreditReport.isEmpty()){
            for(Id contactId : mapContactCreditReport.keySet()){
                creditchecker__Credit_Report__c oCreditReport = mapContactCreditReport.get(contactId);
                Contact oContact = new Contact();
                oContact.Id = contactId;  
                oContact.Decision__c = oCreditReport.Decision__c;
                oContact.Drivers_License__c = oCreditReport.Drivers_License__c;
                oContact.Relative_Name__c = oCreditReport.Relative_Name__c;
                oContact.Employer__c = oCreditReport.Employer__c;
                oContact.Social_Security_Number__c = '000000000';
                oContact.Birthdate = oCreditReport.creditchecker__Applicants_Date_Of_Birth__c;
                oContact.Fraud_Alert_Count__c = oCreditReport.Fraud_Alert_Count__c;
                lstContacts.add(oContact);             
            }
        }

        if(lstContacts != null && !lstContacts.isEmpty()) {
            try{
                Database.update(lstContacts,false) ;
            }
            catch(Exception ex){
                System.debug('Contact Update Failed!' + lstContacts);
            }

        }
        for(Contact oContact: [Select Id, AccountId, FirstName,Is_Primary__c, LastName, Account.Type,Fraud_Alert_Count__c,Decision__c,creditchecker__Average_Score__c from Contact where Id in :setContactId ]) {
            if(String.isNotBlank(oContact.AccountId)){
                mapAccountContact.put(oContact.AccountId,oContact);
            }           
        }

        List<Account> listAccount = new List<Account>();
        if((mapAccountContact != null) && !mapAccountContact.isEmpty()) {            
            for(Id accountId : mapAccountContact.keySet()) {
                String accountType = mapAccountContact.get(accountId).Account.Type;
                Account oAccount = new Account();
                oAccount.Id = accountId;
                oAccount.creditchecker__Average_Score__c = mapAccountContact.get(accountId).creditchecker__Average_Score__c;
                oAccount.Fraud_Alert_Count__c = mapAccountContact.get(accountId).Fraud_Alert_Count__c;
                oAccount.Decision__c = mapAccountContact.get(accountId).Decision__c;
                if(String.isNotBlank(accountType) && accountType.equalsIgnoreCase('Prospect') && mapAccountContact.get(accountId).Is_Primary__c && (mapAccountContact.get(accountId).Decision__c =='Approve' || mapAccountContact.get(accountId).creditchecker__Average_Score__c > Decimal.valueOf(System.Label.CreditDecision))){
                    oAccount.Name = mapAccountContact.get(accountId).LastName + ',' + mapAccountContact.get(accountId).FirstName;
                }
                listAccount.add(oAccount);               
            }
        }

        if(listAccount != null && !listAccount.isEmpty()) {
            Database.update(listAccount,false) ;
        }
    }   
}
trigger CreditReportTrigger on creditchecker__Credit_Report__c (after update, before update){
    if(Trigger.isUpdate){
        if(Trigger.isBefore && Trigger.New != null && Trigger.New.size() > 0) {
           CreditReportTriggerHandler.onBeforeUpdate(Trigger.new);
        }
        if(Trigger.isAfter && Trigger.New != null && Trigger.New.size() > 0) {
            CreditReportTriggerHandler.onAfterUpdate(Trigger.new);
        }
    }
}
2
  • Which picklist field? May 22 '20 at 11:17
  • @FlorianHeer Decision__c May 22 '20 at 12:41
0

I was able to solve this by just removing the object which i was creating in the update method.

Thanks everyone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.