0

I have a JSON String Need to pass as request body with configurabale parameter as below.I have used a wrapper class for that called as EmailWrapper , but here assign value to wrapper.options.fieldOne ,throw Attempt to de-reference a null object but threatMapping.Field1__c has value. I think I have access wrapper in incorrect way. Can someone help me on this?

public with sharing class TestValid {

    @AuraEnabled(cacheable=true)
    public static void emailValidation(String email){
        EmailOptionAttributes emailOptions = new EmailOptionAttributes();
        EmailWrapper wrapper = new EmailWrapper();
        System.debug('email====-: ' +email);
        if(email != null){
            System.debug('email====-: ' +email);
            wrapper.emailValue = email;
            System.debug('email2====-: ' +email);
        }
        Email_validation__mdt[] emailValidation = [SELECT Field1__c,Field2__c,Field3__c,Field4__c,Field5__c FROM Email_validation__mdt];
        System.debug('emailValidation' +emailValidation);
        for (Email_validation__mdt threatMapping : emailValidation) {
            wrapper.options.fieldOne = threatMapping.Field1__c;
            wrapper.options.fieldTwo = threatMapping.Field2__c;
            wrapper.options.fieldThree =  threatMapping.Field3__c;
            wrapper.options.fieldFour = threatMapping.Field4__c;
            wrapper.options.fieldFive = threatMapping.Field5__c;
        }
        System.debug('emailOptions: ' +emailOptions);
        String jsonString = JSON.serialize(emailOptions);
        System.debug('jsonString: ' +jsonString);

    }

    public class EmailOptionAttributes {
        public  Boolean fieldOne;
        public  Boolean fieldTwo;
        public  Boolean fieldThree;
        public  Boolean fieldFour;
        public  Boolean fieldFive;
    }

    public class EmailWrapper{
        public String emailValue;
        public EmailOptionAttributes options;
    }
}
4
  • You need to assign wrapper.options = emailOptions before you try to set the option field values.
    – Phil W
    Commented Aug 29, 2023 at 7:25
  • BTW, only the last MDT's values will be kept with the way you wrote this code.
    – Phil W
    Commented Aug 29, 2023 at 7:27
  • Thank you for the response with correct answer. How can I get all @PhilW?
    – Buhuni
    Commented Aug 29, 2023 at 7:54
  • If you want all, you need to ensure that "options" is an array, then you need to make sure you instantiate new option entries as you go and add them to the array.
    – Phil W
    Commented Aug 29, 2023 at 8:34

1 Answer 1

1

You defined emailOptions at the beginning of your method (a new EmailOptionAttributes) but are not doing anything with it, as instead you're trying to access wrapper.options that is null at this point.

Instead, you can populate the properties of emailOptions and then assign this value to wrapper's options field.

Taking your emailValidation method into consideration, it would be somehow similar to:

@AuraEnabled(cacheable=true)
    public static void emailValidation(String email){
        EmailOptionAttributes emailOptions = new EmailOptionAttributes();
        EmailWrapper wrapper = new EmailWrapper();
        System.debug('email====-: ' +email);
        if(email != null){
            System.debug('email====-: ' +email);
            wrapper.emailValue = email;
            System.debug('email2====-: ' +email);
        }
        Email_validation__mdt[] emailValidation = [SELECT Field1__c,Field2__c,Field3__c,Field4__c,Field5__c FROM Email_validation__mdt];
        System.debug('emailValidation' +emailValidation);
        for (Email_validation__mdt threatMapping : emailValidation) {
            emailOptions.fieldOne = threatMapping.Field1__c;
            emailOptions.fieldTwo = threatMapping.Field2__c;
            emailOptions.fieldThree =  threatMapping.Field3__c;
            emailOptions.fieldFour = threatMapping.Field4__c;
            emailOptions.fieldFive = threatMapping.Field5__c;
        }
        wrapper.options = emailOptions;

        System.debug('emailOptions: ' +emailOptions);
        String jsonString = JSON.serialize(emailOptions);
        System.debug('jsonString: ' +jsonString);

    }

Best regards!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .