0

I need to pass data from LWC to Apexclass, But in apexlass i am getting it as null, But in LWC console i am getting values. Please help me where i did the wrong.

LWC:

import { LightningElement, api, wire, track } from 'lwc';

import { getRecord, getObjectInfo, getPicklistValues, getFieldValue, updateRecord } from 'lightning/uiRecordApi';
import getallMetadata from '@salesforce/apex/RegionController.getallMetadata';
import getmdata from '@salesforce/apex/RegionController.getRegion';
import createUpdateMetadata from '@salesforce/apex/RegionController.createUpdateMetadata'; // to save record.

import picklistval from '@salesforce/apex/RegionController.getallMetadatapiclist';


export default class Custmetadata extends LightningElement {
    @track myMap = [];
    @track meatadataLabel;

    handleClick(event) {
        console.log('Inside Button click ' + event.detail.name);
        this.editmode = true;
        this.displaydetails = false;
        const label = event.target.label;

        if (label === 'Update') { }
        else if (label === 'Save') {
        let i;
        let fieldMap;
        let checkboxes = this.template.querySelectorAll('[data-id="checkbox"]')
        for (i = 0; i < checkboxes.length; i++) {
            this.fieldMap = [...checkboxes].reduce((p, v) => (p[v.name] = p.checked, p), {});
        }
        console.log('myMap data === ' + this.fieldMap);
        console.log('myMap data === ' + JSON.stringify(this.fieldMap));
        createUpdateMetadata({ fullName: this.metadataname, label: this.value, fieldWithValuesMap: this.fieldMap }) //
            .then(result => {
                var options = [];
                if (result) {
                    console.log(result);
                }

            })
            .catch(error => {
                console.log(error);
            });
    }
}

Apex class:

    @AuraEnabled(cacheable=true)
    public static void createUpdateMetadata(String fullName, String label, Map<String, Object> fieldWithValuesMap){ // 
        Metadata.CustomMetadata customMetadata =  new Metadata.CustomMetadata();
        system.debug('fieldWithValuesMap --'+fieldWithValuesMap);
        customMetadata.fullName = fullName;
        customMetadata.label = label;
        
        for(String key : fieldWithValuesMap.keySet()){
            Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
            customField.field = key;
            customField.value = fieldWithValuesMap.get(key); 
            customMetadata.values.add(customField);
        }
        
        Metadata.DeployContainer mdContainer = new Metadata.DeployContainer();
        mdContainer.addMetadata(customMetadata);
   
    }

Output in LWC console:

Prepared map result --- >[{"key":"test","value":true},{"key":"test2","value":true},{"key":"trest3","value":false},{"key":"test4","value":true},{"key":"test233","value":false}]
 this.myMap --- [object Object],[object Object],[object Object],[object Object],[object Object]

enter image description here Thank you

1 Answer 1

0

You made the Map into an Array, but it should be an Object.

let checkboxes = this.template.querySelectorAll('[data-id="checkbox"]');
let fieldMap = [...checkboxes].reduce((p,v) => (p[v.name] = p.checked, p), {});
8
  • Hi @sfdcfox, Here it is updating, but values are not passing the selected values in the map. Here is the updated code. All values are getting as null. What is the issue ? let i; let fieldMap; let checkboxes = this.template.querySelectorAll('[data-id="checkbox"]') this.fieldMap = [...checkboxes].reduce((p, v) => (p[v.name] = p.checked, p), {}); console.log('myMap data === ' + this.fieldMap); console.log('myMap data === ' + JSON.stringify(this.fieldMap));
    – anji reddy
    Commented Jan 31, 2021 at 16:51
  • HI @sfdcfox, This is what am getting as output, myMap data === [object Object] myMap data === {}, Data is not mapping properly. What is the mistake i did it here. Thank you
    – anji reddy
    Commented Jan 31, 2021 at 18:00
  • @anjireddy Don't know, you haven't edited your question to include the latest version of your code.
    – sfdcfox
    Commented Jan 31, 2021 at 18:10
  • here i need to pass selected and unselected checkbox values as ( string, object) format to apex class variable. Please correct me if i did anything wrong. I updated the code, Please have a look. Thank you.
    – anji reddy
    Commented Jan 31, 2021 at 18:15
  • @anjireddy you used my name, fieldMap, but then gave the parameter as this.myMap. Make sure the variable names match. Also, your first parameter appears to be misspelled (meatadataname instead of whatever it should be; it doesn't appear to be declared or assigned anywhere).
    – sfdcfox
    Commented Jan 31, 2021 at 18:18

You must log in to answer this question.

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