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
Salesforce Training 167Salesforce Training 167 

Please help me with this test class

Please help me with the following test method. I have an apex method for which I need to write a test method. I tried but I keep getting error:System.TypeException: Invalid conversion from runtime type List<ANY> to Map<String,ANY>

Here is my Apex method:
public static boolean isAutoApprovalRuleSatisfied (Map<String,Object> productRecFieldMap) {
        Map<String,Object> productFieldValueMap = (Map<String,Object>) JSON.deserializeUntyped(JSON.serialize(productRecFieldMap.get('Productl')));
        Boolean isAutoApprovalRuleSatisfied = false;
        List <SBT_QuoteAutoApprovalRule__mdt> approvalRules = SBT_QuoteApprovalUtility.getQuoteAutoApprovalRules();
        for (SBT_QuoteAutoApprovalRule__mdt autoApprovalRule : approvalRules) {
            if(autoApprovalRule.SBT_BasedOn__c == 'QuoteLineItem' && autoApprovalRule.SBT_Discount_Field__c != null) {
                if (autoApprovalRule.SBT_Addtional_Condition_Field__c != null  && autoApprovalRule.SBT_Addtional_Condition_Field__c.contains('Product2')) {
                    if (autoApprovalRule.SBT_Operator__c == 'Equals') {
                        String productFieldName = autoApprovalRule.SBT_Addtional_Condition_Field__c.split('Product2.')[1];
                        if ((String)productFieldValueMap.get(productFieldName) == autoApprovalRule.SBT_FieldValue__c){
                            if ((Decimal)productRecFieldMap.get('Discount') >= autoApprovalRule.SBT_Discount_Lower_Range__c && (Decimal)productRecFieldMap.get('Discount') <= autoApprovalRule.SBT_Discount_Upper_Range__c) {
                                isAutoApprovalRuleSatisfied = true;
                                break;
                            }
                        }
                    } else if (autoApprovalRule.SBT_Operator__c == 'Not Equals') {
                        if ((String)productRecFieldMap.get(autoApprovalRule.SBT_Addtional_Condition_Field__c) != autoApprovalRule.SBT_FieldValue__c){
                            if ((Decimal)productRecFieldMap.get('Discount') >= autoApprovalRule.SBT_Discount_Lower_Range__c && (Decimal)productRecFieldMap.get('Discount') <= autoApprovalRule.SBT_Discount_Upper_Range__c) {
                                isAutoApprovalRuleSatisfied = true;
                                break;
                            }
                        }
                    } else if (autoApprovalRule.SBT_Operator__c == 'Contains') {
                        String fieldValue = (String)productRecFieldMap.get(autoApprovalRule.SBT_Addtional_Condition_Field__c);
                        if (fieldValue.indexOf(autoApprovalRule.SBT_FieldValue__c) > -1){
                            if ((Decimal)productRecFieldMap.get('Discount') >= autoApprovalRule.SBT_Discount_Lower_Range__c && (Decimal)productRecFieldMap.get('Discount') <= autoApprovalRule.SBT_Discount_Upper_Range__c) {
                                isAutoApprovalRuleSatisfied = true;
                                break;
                            }
                        }
                    }                   
                } else {
                    if ((Decimal)productRecFieldMap.get('Discount') >= autoApprovalRule.SBT_Discount_Lower_Range__c && (Decimal)productRecFieldMap.get('Discount') <= autoApprovalRule.SBT_Discount_Upper_Range__c) {
                        isAutoApprovalRuleSatisfied = true;
                        break;
                    }
                }
            }
        }
        return isAutoApprovalRuleSatisfied;
    }
And here is the test class I have written
@isTest
	static void testIsAutoApprovalRuleSatisfiec(){
        Pricebook2 pricebookRec2 = [SELECT Id from Pricebook2 LIMIT 1];
        List<QuoteCreationApex.ProductListWrapper> getProducListRecords2= QuoteCreationApex.getProductList(pricebookRec2.Id);
        String productRecString1 = JSON.serialize(getProducListRecords2);
        Map<String,Object> productRecFieldMap1 =  (Map<String,Object>) JSON.deserializeUntyped(productRecString1);
        Test.startTest();
        boolean isRuleSatisfied1 = QuoteCreationApex.isAutoApprovalRuleSatisfied(productRecFieldMap1);
        System.assertEquals(expected, actual);
        system.debug('isRuleSatisfied---'+isRuleSatisfied1);
        Test.stopTest();
    }

Please help someone :(

 
mukesh guptamukesh gupta
Hi,

Can you please share exact line number

Regards
Salesforce Training 167Salesforce Training 167
@Mukesh Gupta - throwing me an error in this line: 
Map<String,Object> productRecFieldMap1 = (Map<String,Object>) JSON.deserializeUntyped(productRecString1);
mukesh guptamukesh gupta
Hi,

Please follow below url

https://newbedev.com/invalid-conversion-from-runtime-type-list-any-to-map-string-any

https://developer.salesforce.com/forums/?id=9062I000000IOUnQAO 

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh