function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Quddus Ololade 4Quddus Ololade 4 

Unable to get the deserialized object to lightning controller

Component:
<aura:component controller="LightningController" implements="flexipage:availableForAllPageTypes,force:hasRecordId" access="global"  >
<aura:attribute name="purposes" type="ApexDataContainer" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    data: {!v.purposes}
</aura:component>
Controller
({
        doInit : function(component, event, helper) {
            var getdata = component.get("c.JsonGetValue");
            getdata.setCallback(this, function(response){      
                var state = response.getState();
                if (state === 'SUCCESS'){
                    var result = response.getReturnValue();
                    console.log('1'+JSON.stringify(result));
                    console.log('2'+result['purposes']);
component.set("v.purposes",response.getReturnValue());
                }
            });
            $A.enqueueAction(getdata);      
        }
    })
Apex:
public class LightningController {

    @AuraEnabled
    public static ApexDataContainer JsonGetValue() {
        ApexDataContainer c1 = new ApexDataContainer();
    System.debug('hello');
        String json1=           '{'+
        ''+
        '    "purposes": ['+
        ''+
        '        {'+
        ''+
        '            "purpose": "service-improvement",'+
        ''+
        '            "legalGround": "ic",'+
        ''+
        '            "status": "not_answered",'+
        ''+
        '            "description": ['+
        ''+
        '                {'+
        ''+
        '                    "language": "sv-EN",'+
        ''+
        '                    "text": "Service data structure"'+
        ''+
        '                }'+
        ''+
        '            ],'+
        ''+
        '            "version": "1.0.0",'+
        ''+
        '            "dataIds": []'+
        ''+
        '        }'+
        '    ]'+
        ''+
        '}';       
        c1 = (ApexDataContainer)JSON.deserialize(json1,ApexDataContainer.class);

        system.debug('wrapper'+c1.purposes.size());
        return c1;
    }
}

In apex debug log, I see that the JSON object has been successfully parsed, while in a lightning controller, I don't get the value in response.getreturnvalue() for this apex method (JsonGetValue) also nothing in the console.log 

I saw in a couple of blogs that we can pass an object from APEX and refer it in lightning controller and component
Can someone help me understand why I am not getting the object in the lightning controller console log
Raj VakatiRaj Vakati
Working copy ... Use this code 

 
public class LightningController {
    
    @AuraEnabled
    public static ApexDataContainer JsonGetValue() {
        ApexDataContainer c1 = new ApexDataContainer();
        System.debug('hello');
        String json1=           '{'+
            ''+
            '    "purposes": ['+
            ''+
            '        {'+
            ''+
            '            "purpose": "service-improvement",'+
            ''+
            '            "legalGround": "ic",'+
            ''+
            '            "status": "not_answered",'+
            ''+
            '            "description": ['+
            ''+
            '                {'+
            ''+
            '                    "language": "sv-EN",'+
            ''+
            '                    "text": "Service data structure"'+
            ''+
            '                }'+
            ''+
            '            ],'+
            ''+
            '            "version": "1.0.0",'+
            ''+
            '            "dataIds": []'+
            ''+
            '        }'+
            '    ]'+
            ''+
            '}';       
        c1 = (ApexDataContainer)JSON.deserialize(json1,ApexDataContainer.class);
        System.debug('c1'+c1);
        return c1;
    }
    
   
}
 
public class ApexDataContainer{
    @AuraEnabled
    public cls_purposes[] purposes{get;set;}
    class cls_purposes {
        @AuraEnabled
        public String purpose{get;set;}	//service-improvement
        @AuraEnabled
        public String legalGround{get;set;}	//ic
        @AuraEnabled
        public String status{get;set;}	//not_answered
        @AuraEnabled
        public cls_description[] description{get;set;}
        @AuraEnabled
        public String version{get;set;}	//1.0.0
        @AuraEnabled
        public cls_dataIds[] dataIds{get;set;}
    }
    
    class cls_description {
        @AuraEnabled
        
        public String language{get;set;}//sv-EN
        @AuraEnabled
        
        public String text{get;set;}	//Service data structure
    }
    
    class cls_dataIds {
    }
    
}
 
<aura:component controller="LightningController" implements="flexipage:availableForAllPageTypes,force:hasRecordId" access="global"  >
    <aura:attribute name="purposes" type="ApexDataContainer" />
    
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    data: {!v.purposes}
</aura:component>
 
({
    doInit : function(component, event, helper) {
        var getdata = component.get("c.JsonGetValue");
        getdata.setCallback(this, function(response){      
            var state = response.getState();
            if (state === 'SUCCESS'){
                var result = response.getReturnValue();
                console.log(result);
                //               console.log('1'+JSON.stringify(result));
                console.log('2'+result['purposes']);
                component.set("v.purposes",JSON.stringify(result));
            }
        });
        $A.enqueueAction(getdata);      
    }
})

 
Quddus Ololade 4Quddus Ololade 4
Yes I just need the aura enabled in the variables to be able to access.

But I am getting an issue in parsing only when I send the request using REST API
 
Callout Class:

public class CallDevam2 {
        //Wrapper
        public class deserializeResponse
        {
            public String id;
            public String access_token;
        }
        public CallDevam2(){
            ApexDataContainer c1 = new ApexDataContainer();
            ApexDataContainer.Purposes p1 = new ApexDataContainer.Purposes();
            ApexDataContainer.Description d1 = new ApexDataContainer.Description();
            ApexDataContainer.DataIds dIds = new ApexDataContainer.DataIds();       
            List<ApexDataContainer.Purposes> addtolist = new List<ApexDataContainer.Purposes>();
            List<ApexDataContainer.Description> addtodesc = new List<ApexDataContainer.Description>();
            List<ApexDataContainer.DataIds> addtodIds = new List<ApexDataContainer.DataIds>();
            p1.legalGround = 'MyGround';
            p1.purpose = 'purpose';
            p1.status = 'status';
            p1.version = 'version';
            addtolist.add(p1);
            d1.language = 'language';
            d1.text = 'text';
            addtodesc.add(d1);
            p1.description = addtodesc;
            p1.dataIds = addtodIds;
            c1.purposes = addtolist;
            String jsonstr = JSON.serialize(c1);
            **system.debug('@@@'+jsonstr);**

            http http1 = new Http();
            HttpRequest req1 = new HttpRequest();
            req1.setEndpoint('https://test.salesforce.com/services/apexrest/restrequest/');
            req1.setHeader('Authorization','Bearer ' + token1); //The token which i had already recieved used here
            req1.setHeader('Content-Type','application/json');
            req1.setHeader('Accept','application/json');
            req1.setHeader('Content-length',string.valueOf(jsonstr.length()));
            req1.setMethod('POST');
            req1.setBody(jsonstr);
            HttpResponse response1 = http1.send(req1);
            system.debug('response1:'+response1.getBody());

        }
    }

I have seen in the debug log before making a call out that we have the valid json but once it hits the service I get the response as
00:41:07:354 USER_DEBUG [51]|DEBUG|response1:[{"message":"Unexpected parameter encountered during deserialization: purposes at [line:1, column:14]","errorCode":"JSON_PARSER_ERROR"}]

With the same json string if I do within classes, it gets successfully parsed, do we need to do anything extra in REST call out while sending Json request as string.
The exposed service class:

@RestResource(urlMapping='/restrequest/*')
global class RESTservice {
    @HttpPost
    global static String resultsDisplay(String c1){
        try{
        system.debug('1111'+c1);
        ApexDataContainer resp2 = (ApexDataContainer)JSON.deserialize(c1, ApexDataContainer.class);
        system.debug('2222'+resp2);
        return 'Connection established';
        }catch(exception e){
        system.debug('$$$$'+e.getMessage());
        return 'Connection Error';
        }
    }    
}

The ApexDataContainer class is exactly same in both the environments. The request gets failed in the method invoke.
Any ideas here