-1

Hi Iam getting output in Javascript client side.How can I separate components and components1 value in the image.

//Please check one more updated code

    public class ActionController {
    @AuraEnabled
    public static string pageLoading(string id) {

        List < Action_Criteria__c > RecordsInserted = new List < Action_Criteria__c > ();
        List < WrapperCriteria > wList = new List < WrapperCriteria > ();
        Criteria_Master__c cmas = new Criteria_Master__c();
        cmas=[select id, Name, Action_Criteria__c, Company_Master__c from Criteria_Master__c where Id =:id];
        RecordsInserted = [select id, Action_Criteria_API_Name__c, Action_Criteria_Field_Name__c, Criteria_Master__c, Object_API_Name__c, Operator__c, Object_Name__c, Special_Operator__c, Special_operator_slash_value__c, Special_operator_value__c from Action_Criteria__c where Criteria_Master__c = : id order by Sequence_No__c];
        system.debug('record Inserted values:::' + RecordsInserted);
        Integer counter=1;
        for (Action_Criteria__c ac: RecordsInserted) {
            WrapperCriteria wrp = new WrapperCriteria();
            wrp.record = ac;
            wrp.ObjectApiNAme = ac.Object_API_Name__c;
            wrp.FieldName = ac.Action_Criteria_API_Name__c;
            wrp.index =counter;
            counter=counter+1;
            wrp.CriteriaMasId = ac.Criteria_Master__c;
            wrp.Operator = ac.Operator__c;
            wrp.SpecialOperator = ac.Special_Operator__c;
            wrp.specialOperatorSlashValue = ac.Special_operator_slash_value__c;
            wrp.specialOperatorValue = ac.Special_operator_value__c;
            wList.add(wrp);
        }

        return JSON.serialize(wList);
    }
    @AuraEnabled
    public static string ObjectNames()
    {

        List<SelectOption> ObjNames= new List<SelectOption>();
        ObjNames.add(new SelectOption('','--None--'));
        ObjNames.add(new selectoption('Pay_Element_Master__c','Pay Element'));
        ObjNames.add(new selectoption('CTC_Master__c','CTC Master'));
        ObjNames.add(new selectoption('Section_Master__c','Section Master'));
        return JSON.serialize(ObjNames);
    }

    @AuraEnabled
    public static WrapperCriteria getComponent(List < WrapperCriteria >warpList,string id) {
        String objectName;
        Criteria_Master__c cmas = new Criteria_Master__c();
        //List < WrapperCriteria > warpList = (List < WrapperCriteria > )System.JSON.deserialize(wrapval, List<WrapperCriteria >.class);

        system.debug('::::::wrList value::::' + warpList);
        system.debug('id value ::::::' + id);

            WrapperCriteria ac=new WrapperCriteria ();
        List < SelectOption > ObjNames = new List < SelectOption > ();
        ObjNames.add(new SelectOption('', '--None--'));
        ObjNames.add(new selectoption('Pay_Element_Master__c', 'Pay Element'));
        ObjNames.add(new selectoption('CTC_Master__c', 'CTC Master'));

        Map <String,String> ctcMasAPINames = new Map < String, String > ();
        cmas = [select id, Name, Action_Criteria__c, Company_Master__c from Criteria_Master__c where Id=:id];

        if (warpList.size() > 0) {
            for (WrapperCriteria wr: warpList) {
                List <SelectOption > options = new List <SelectOption > ();
                if (wr.ObjectApiNAme == 'Pay_Element_Master__c') {
                    for (SelectOption so: ObjNames) {
                        if (so.Value == wr.ObjectApiNAme) {
                            objectName = so.Label;
                            system.debug('****objectname *****' + objectName);
                        }
                    }
                    wr.SelObjectName = objectName;     //aassigned to wrapper


                    for (Pay_Element_Master__c pem: [select id, name, Company_Master__c from Pay_Element_Master__c where Company_Master__c = : cmas.Company_Master__c]) {
                        options.add(new SelectOption(pem.id, pem.name));
                        //dc.componentMap.put(pem.id,pem.name);
                        ctcMasAPINames.put(pem.id, pem.name);
                        System.debug('--------Pay element MAster MAP VAlues-----' + ctcMasAPINames);
                    }
                    ac.Components = options;
                    system.debug('ac.Components value:::::::'+ac.Components);
                } 
                else if (wr.ObjectApiNAme == 'CTC_Master__c') {
                    for (SelectOption so: ObjNames) {
                        if (so.Value == wr.ObjectApiNAme) {
                            objectName = so.Label;
                            system.debug('-----------objectname --------------' + objectName);
                        }
                    }
                    wr.SelObjectName = objectName;
                    Map < String, Schema.SObjectType > globalDescription = Schema.getGlobalDescribe();
                    Schema.sObjectType objType = globalDescription.get('CTC_Master__c');
                    Schema.DescribeSObjectResult ob = objType.getDescribe();

                    if (ob == Null) {
                        AuraHandledException e = new AuraHandledException('Invalid sObject Type');
                        e.setMessage('Invalid sObject Type');
                        system.debug(e);
                        throw e;

                    }
                    Map < String, Schema.SObjectField > mapFieldList = ob.fields.getMap();

                    for (Schema.SObjectField field: mapFieldList.values()) {
                        Schema.DescribeFieldResult fieldResult = field.getDescribe();
                        if (fieldResult.isAccessible() && (fieldResult.getType() == Schema.DisplayType.Integer || fieldResult.getType() == Schema.DisplayType.Double || fieldResult.getType() == Schema.DisplayType.Currency)) {
                            options.add(new SelectOption(fieldResult.getName(), fieldResult.getLabel()));
                            //dc.componentMap1.put(fieldResult.getName(),fieldResult.getLabel());
                            ctcMasAPINames.put(fieldResult.getName(),fieldResult.getLabel());   
                        }
                    }
                    ac.Components1= options;
                    system.debug('ac.Components1 value:::::::'+ac.Components1);
                }
            }
        }

        return ac;
    }


    @AuraEnabled
    public static string addRow(string wrapval,integer counterval ){
        system.debug('counter value::::'+counterval);
        List < WrapperCriteria > warpList = (List<WrapperCriteria >)JSON.deserialize(wrapval,List<WrapperCriteria >.class);

        WrapperCriteria objInnerClass = new WrapperCriteria();
        Action_Criteria__c newAc= new Action_Criteria__c();        
        objInnerClass.record = newAc;
        objInnerClass.index = counterval;
        warpList.add(objInnerClass);    
        system.debug('------warpList---->'+warpList); 
        return JSON.serialize(warpList);
    }
    @AuraEnabled
    public static void saveCall(string wrvalue,string id){
        List<Action_Criteria__c> RecordsToBeInserted=new List<Action_Criteria__c>();
        Map <String,String> ctcMasAPINames = new Map < String, String > ();
        system.debug('save functionality is calling::::');
        List < WrapperCriteria > warpList = (List < WrapperCriteria >) JSON.deserialize(wrvalue, List<WrapperCriteria >.class);
        system.debug('::::::::::::warpList::::::::::::::::'+warpList);
        system.debug('id value:::::::::::'+id);
        List<Criteria_Master__c> updatedCrimasters=new List<Criteria_Master__c>();
        Criteria_Master__c cmas = new Criteria_Master__c();

        cmas=[select id, Name, Action_Criteria__c, Company_Master__c from Criteria_Master__c where Id =:id];
        ID cid=cmas.id;
        system.debug('cid value ::::::'+cid);

        if(warpList!=null && !warpList.isEmpty())
        {
            for(WrapperCriteria wf: warpList)
            {
                system.debug('wrapper calling::::::::::::::::::::');
               // if(!string.isBlank(wf.SelObjectName) && !string.isBlank(wf.FieldName))
                //{    
                    Action_Criteria__c ac= wf.record;
                    ac.Criteria_Master__c =cid ;
                    ac.Object_Name__c=wf.SelObjectName;
                    if(wf.Operator!='None')
                    {
                        ac.Operator__c=wf.Operator;
                    }
                    ac.Object_API_Name__c=wf.ObjectApiNAme;
                    ac.Action_Criteria_API_Name__c=wf.FieldName;
                    ac.Special_operator_slash_value__c = wf.specialOperatorSlashValue;
                    ac.Special_operator_value__c = wf.specialOperatorValue;
                    // System.debug('-----fc.object api--------'+ctcMasAPINames.ContainsKey(wf.FieldName));
                    if(ctcMasAPINames.ContainsKey(wf.FieldName)) 
                    {
                        if(ac.Object_API_Name__c=='Pay_Element_Master__c')
                        {
                            ac.Action_Criteria_Field_Name__c= ctcMasAPINames.get(wf.FieldName);
                        }
                        if(ac.Object_API_Name__c=='CTC_Master__c')
                        {
                            ac.Action_Criteria_Field_Name__c= ctcMasAPINames.get(wf.FieldName);
                            System.debug('-------------'+ctcMasAPINames.get(wf.FieldName));
                        }

                    }
                    system.debug('==fc==>'+ac);
                    RecordsToBeInserted.add(ac);

                //}
            }

            system.debug('==RecordsToBeInserted==>'+RecordsToBeInserted.size());
            upsert RecordsToBeInserted;
            for(Criteria_Master__c cm:[select id,Name,Filter_Criteria__c from Criteria_Master__c where Id=:id])
            {
                string ActionCriteria;
                if(!RecordsToBeInserted.isEmpty())
                {
                    for(Action_Criteria__c ac: [select id, Object_API_Name__c,Action_Criteria_API_Name__c, Sequence_No__c, Operator__c, Criteria_Master__c from Action_Criteria__c where Criteria_Master__c=:id order by  Sequence_No__c asc])
                    {
                        if(string.isBlank(ActionCriteria))
                        {
                            ActionCriteria =  string.valueof(ac.Action_Criteria_API_Name__c);
                        }
                        else
                        {
                            ActionCriteria =  ActionCriteria + string.valueof(ac.Action_Criteria_API_Name__c);
                        }
                        if(ac.Operator__c=='+ Addition')
                        {
                            ActionCriteria = ActionCriteria +'+';    
                        }
                    }
                    if(!string.isBlank(ActionCriteria))
                    {
                        cm.Action_Criteria_Logic__c =  ActionCriteria;
                    }
                    cm.Action_Criteria__c =True;
                    updatedCrimasters.add(cm);
                }
            }
            update updatedCrimasters;
            system.debug('update criteria master::::::::::'+updatedCrimasters);
        }
    }
    @AuraEnabled
    public static string removeRow(string wrvalue,integer rowval){
        system.debug('::::: rowToRemove val  :::::'+rowval);

        List < WrapperCriteria > warpList = (List < WrapperCriteria > ) JSON.deserialize(wrvalue, List<WrapperCriteria >.class);
        system.debug('::::::wrList value::::' + warpList);
        rowval = Integer.valueOf(rowval);
        if(!string.isBlank(string.valueof(rowval)))
        {
            Action_Criteria__c acdelete = warpList[rowval].record;
            list<Action_Criteria__c> listAc = new list<Action_Criteria__c>();

            if(!string.isblank(acdelete.id))
            {
                Criteria_Master__c criteriaMaster = [select id,Name,Action_Criteria__c,(select id, Criteria_Master__c from Action_Criteria__r where id!=:acdelete.id ) from Criteria_Master__c where Id=:acdelete.Criteria_Master__c];
                listAc = [select id, Criteria_Master__c from Action_Criteria__c where id!=:acdelete.id and Criteria_Master__c=:acdelete.Criteria_Master__c];
                if(criteriaMaster.Action_Criteria__r.isEmpty())
                {
                    criteriaMaster.Action_Criteria__c = false;
                    update criteriaMaster;
                }
                delete acdelete;
            }
        }
        warpList.remove(rowval);
        system.debug('wraper List  *******'+warpList);
        return JSON.serialize(warpList);
    } 
}

Javascript code:

    ({
    doInit: function(component, event, helper) {
        helper.pageLoad(component);
        helper.componentVal(component);

    },
    doSomething: function(component, event, helper) {
        helper.Method2(component);
    },
    RemoveClick: function(component, event, helper) {
        var selCont = event.getParam("selectedJob");
        var conts = component.get("v.wrplst");
        var contsval = JSON.stringify(conts);

        var Selndex = conts.indexOf(selCont);

        var DeleteLen = conts.length;
        //alert('deleteLen value::::' + DeleteLen);
        var removeAction=component.get("c.removeRow");
        removeAction.setParams({
            "wrvalue":contsval,
            "rowval":Selndex 
        });
        removeAction.setCallback(this, function(g) {
            var state = g.getState();
            if (state == 'SUCCESS') {
                alert('success value');
                component.set("v.wrplst",JSON.parse(g.getReturnValue()));
            }
            else if(state==='error'){
                alert('error value');
            }
        });
        $A.enqueueAction(removeAction);
    },
    AddRow: function(component, event, helper) {

        var comp1 = component.get("v.wrplst");
        var compJson1 = JSON.stringify(comp1);
        var mapRecords = new Array();
        for (i = 0; i < comp1.length; i++) {
            mapRecords.push(comp1[i]);
        }
        var len = mapRecords.length;

        var newlen = len + 1;

        var addAction = component.get("c.addRow");
        addAction.setParams({
            "wrapval": compJson1,
            "counterval": newlen,
        });
        addAction.setCallback(this, function(res) {
            var state = res.getState();
            if (state == 'SUCCESS') {
                component.set("v.wrplst", JSON.parse(res.getReturnValue()));
            } else if (state == 'ERROR') {
                alert('error');
            } else if (state == 'INCOMPLETE') {
                alert('incompltete');
            }
        });
        $A.enqueueAction(addAction);
    },
    saveRow: function(component, event, helper) {
        var Idx = component.get("v.theId");
        alert('Id value ::::' + Idx);
        var com = component.get("v.wrplst");
        var comJson = JSON.stringify(com);

        var saveAction = component.get("c.saveCall");
        saveAction.setParams({
            "wrvalue": comJson,
            "id": Idx,
        });
        saveAction.setCallback(this, function(re) {
            var state = re.getState();
            if (state === 'SUCCESS') {

            }
        });
        $A.enqueueAction(saveAction);
    }
});

helper

    ({
    pageLoad: function(component, event) {
        var action = component.get("c.pageLoading");
        var self = this;
        var Idx = component.get("v.theId");
        action.setParams({
            "id": Idx
        });
        action.setCallback(this, function(g) {
            var state = g.getState();
            if (state == 'SUCCESS') {

                component.set("v.wrplst",JSON.parse(g.getReturnValue()));
                self.Method2(component);

            }
        });
        $A.enqueueAction(action);

    },

    Method2: function(component, event, helper) {

        var comp = component.get("v.wrplst");
        var compJson = JSON.stringify(comp);
        var Id1 = component.get("v.theId");
        var compAction = component.get("c.getComponent");
        compAction.setParams({
            "wrapval": compJson,
            "id": Id1,
        });
        compAction.setCallback(this, function(re) {
            var state = re.getState();
            if (state ==='SUCCESS') {
               var da=re.getReturnValue();
              console.log('da value======'+da); //Here we are getting [object,object]
              console.log('da value======'+JSON.stringify(da));//if we check the value means here we will get null
               }
            else if(state==='error'){
                alert('error value');
            }
                else if(state==='INCOMPLETE'){
                    alert('incomplete values:::');

                }
        });
        $A.enqueueAction(compAction);
    },
    componentVal:function(component,event,helper){

        var action=component.get("c.ObjectNames");
        action.setCallback(this,function(re){
            var state=re.getState();
            if(state==='SUCCESS'){

                component.set("v.options",JSON.parse(re.getReturnValue()));
            }
            else if(state==='Error'){
                alert('error');
            }
        });
        $A.enqueueAction(action);
    }
})
  • from where this value you got? from controller ? – Safiya PV Mar 31 '17 at 7:35
  • Can you share your controller code ? or the part where you console.logged this object dump – StormBr34ker Mar 31 '17 at 8:31
  • 1
    Best if you add a bit more to your question - you have ended up with escaped JSON. Generally in Lightning Components you can leave it to the framework to serialize and deserialize and just use JavaScript dot notation to access child objects and fields. – Keith C Mar 31 '17 at 8:31
  • from the controller I got...I have added controller code please check it...Requirement:I want to display component 1 and components value in lightning select – SFDC FAN Mar 31 '17 at 8:43
  • did you parsed the return value? you need to parse using JSON.parse(your value from controller) in javascript. – Safiya PV Mar 31 '17 at 8:50
2

Your Apex controller is returning a String that the framework is then escaping so that the value can be safely used as one String in an overall JSON structure - that is not what you want.

As I suggested in the answer to System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set, return the WrapperCriteria object and allow the framework to do the serialisation and deserialisation and that will eliminate the spurious encoding. Then at the JavaScript side you will have a normal JavaScript nested object that you can use dot notation on and iterate over arrays etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.