0

I have been trying to pass the JS object from component to apex. it says Issues are I am not getting Listsubsystem variable in apex.

     var listOfsubsystemObj = [];
            var jsondata = JSON.stringify(component.get("v.gettotalsubproducts"));
            var jsonParsedData = JSON.parse(jsondata);

            for (var data in jsonParsedData) {
                var singleObj = {};
                singleObj['subsystemid'] = data;
                singleObj['totalcogs'] = jsonParsedData[data].totalCogs;
                singleObj['totalRevenue'] = JSON.stringify(jsonParsedData[data].totalRevenue);
                listOfsubsystemObj.push(singleObj);
            }
 totalPrice = Math.round(totalPrice);
            var action = component.get("c.submitForApproval");
            //PRJ59909-5 Start Added another parameter.
            action.setParams({
                prdId: prdId,
                sellPrice: totalPrice,
                margin: totalMargin,
                listsubsystem: (listOfsubsystemObj),
                ignoreAudits: ignoreAudits
            });
            action.setCallback(this, function(response) {
                var state = response.getState();
                if (state === "SUCCESS") {
                    window.location = "/c/PRDAppConversion.app?prdId=" +
                        component.get("v.prdId") +
                        "&returnURL=" +
                        encodeURIComponent(component.get("v.prdId"));
                } else {
                    var sObj = component.get('v.prdSObj');
}

and in apex my method is

public static Boolean submitForApproval(Id prdId, list<String> listsubsystem, Decimal sellPrice, Decimal margin, boolean ignoreAudits) {

Here is the error Value provided is invalid for action parameter 'listsubsystem' of type 'LIST'

2
  • Can you please share the relevant code of JS from which you are calling the apex method? Commented Aug 8, 2019 at 9:32
  • updated Krishnan
    – Arvind
    Commented Aug 8, 2019 at 9:35

1 Answer 1

3

This is because listsubsystem in parameters is of type List<String> but what you are passing is of type List<Object>. So, you can try changing it to List<Object> listsubsystem.

Also, singleObj['subsystemid'] = data; - this should be I think singleObj['subsystemid'] = jsonParsedData[data].Id;

And better code will be using map:

    let target = component.get("v.gettotalsubproducts");

    let listOfsubsystemObj = Object.keys(target).map(function (itemId) {
        return {
            subsystemid: itemId,
            totalcogs: String(target[itemId].totalCogs),
            totalRevenue: String(target[itemId].totalRevenue)
        };
    });

---- added based on comments -----

and you can consume in apex:

public static List<Object> destructureSystemObjArray(List<Object> listOfsubsystemObj){
    for(Object obj : listOfsubsystemObj) {
        System.debug('obj => '+obj);
        Map<Object,Object> objMap = (Map<Object,Object>)obj;
        System.debug('objMap => '+objMap);
        System.debug('subsystemid => '+objMap.get('subsystemid'));
        System.debug('totalcogs => '+objMap.get('totalcogs'));
        System.debug('totalRevenue => '+objMap.get('totalRevenue'));
    }
    return null;
}

If you need to save in database, you convert to different data types. For example, String.valueOf(objMap.get('totalRevenue'))

0

You must log in to answer this question.

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