• Salesforce Training 167
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
Hey all,
I am having trouble understanding why this code is not bulkified? Can someone please help me understand why my senior said its not bulkified? Also, can someone please help me bulkify this trigger code? 
Its a simple trigger where I am sending an welcome email to the 'billing contacts' (role of the contact) when account is created. This billing contact is assoicated with this account
public static void sendWelcomeEmailforPartnerPlans(List<Account> accounts) {
        system.debug('inside Partner plan welcome email method');
        map<id,id> accBillContactId = new map<id,id>();
        OrgWideEmailAddress owa = [select Id, DisplayName, Address from OrgWideEmailAddress limit 1];
        List<String> corporateEmail = System.label.MBT_CorporateEmail.split(',');
        List<String> bccEmail = System.label.MBT_BccEmailForPartnerPlan.split(',');
        EmailTemplate welcomeEt = [SELECT Id, Subject, Body FROM EmailTemplate WHERE DeveloperName = 'MBT_WelcomeCallEmailAlert'];        
        List<String> emailList = new List<String>();
        List<Messaging.SingleEmailMessage> singleMailList = new List<Messaging.SingleEmailMessage>();
        List<Messaging.SendEmailResult> sendEmailResults = new List<Messaging.SendEmailResult>{};
		for(Account acc1:accounts){
            system.debug('accountid--'+acc1.Id);
            if(acc1.AcctSeed__Billing_Contact__c!=null){ 
                accBillContactId.put(acc1.Id,acc1.AcctSeed__Billing_Contact__c);
            }
        }
        List<Contact>  contacts = [SELECT id, Email from Contact where id in :accBillContactId.values() ];
        for (Contact con : contacts) {
            emailList.add(con.Email);
        }
        system.debug('email ids---'+emailList);
        for(Account acct : [SELECT id, Name, MBT_Plan_Name__r.Name, MBT_Plan_Name__c from Account where id in: accounts ]){
            system.debug(acct.MBT_Plan_Name__r.Name);
            if(acct.MBT_Plan_Name__r.Name=='Partner'){
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                email.setTargetObjectId(accBillContactId.get(acct.Id));
                email.setTemplateId(welcomeEt.Id);
                email.setOrgWideEmailAddressId(owa.id);
                email.setToAddresses(emailList);
                email.setCCAddresses(corporateEmail);
                email.setBccAddresses(bccEmail);
                singleMailList.add(email);
            }//end-if
        }//end-for
        if(singleMailList.size()>0){
            try{
                sendEmailResults= Messaging.sendEmail(singleMailList);
            }
            catch(System.EmailException ex){
                system.debug('email excepition-->'+ex.getMessage());
            }
            for(Messaging.SendEmailResult sendEmailResult: sendEmailResults) {
                if(sendEmailResult.isSuccess()){
                    system.debug('isSuccess welcome call---'+sendEmailResult.isSuccess());
                }else {
                    for(Messaging.Sendemailerror sendEmailError : sendEmailResult.getErrors()) {
                        system.debug('sendemail error--'+sendEmailError.Message);
                    }
                }
            }
        }
    }//end method

 
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 :(

 
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 :(