0

I am trying to do the following:

  1. Called an HTTP Get method from Wired Apex
  2. The callout will return an array of objects
  3. Return response.getBody the wired method.
  4. Parse the response and display in a Lighting Data table.

The callout works fine and returns the values as expected. But I do not seem to be able to access the values of the response. In my console.log it appears that the wired method is running twice. The second time through in the console the value of the parsed response shows as [object object]. When I call Object.keys I get a single value of 'data'. And Object.values show one very long JSON String. I assume that I am failing somehome parse this response into its appropriate array of objects. Any help on how to do this would be appreciated.

Here is the response from the Apex continuation method. I was able to parse the data using Imperative Apex. But when I tried to used wired everything failed.

 @AuraEnabled(Cacheable = true)
    public static Object processLoginResponse(List<String> labels, Object state){        
        HttpResponse response = Continuation.getResponse(labels[0]);        
        return response.getBody();
    }

Here is the JS for this LWC

export default class IdLoginHistory extends LightningElement {
    @track returnedData = [];
    @track error;
    @track loginData;
    @api showHistory;
    @api federationId;
    @track dataReady;
    @track recordsToDisplay =[];
    @track columns = columns;

    closeHistory(){
        this.dispatchEvent(new CustomEvent('closetable'));
    }

    @wire(getLoginHistory, {federationId: '$federationId'})
    wiredHistory(data, error){
        if (data){
            const stringified = JSON.stringify(data);
            const parsedData = JSON.parse(stringified);
            console.log('type of ' + typeof  parsedData);
            console.log('index type of = ' + parsedData[0]);
            console.log('value of parsed data ' + parsedData);
            console.log('object keys ' + Object.keys(parsedData));
            console.log('object values = ' + Object.values(parsedData));           
        }
        if (error){
            this.data = undefined;
            this.error = error;
        }
  }
}

This is a sample of the JSON from PostMan

[
    {
        "actor": {
            "id": "00urdbalriusuaL5g0h7",
            "type": "User",
            "alternateId": "[email protected]",
            "displayName": "Brooks Johnson",
            "detailEntry": null
        },
        "client": {
            "userAgent": {
                "rawUserAgent": "SFDC-Async-Callout",
                "os": "Unknown",
                "browser": "UNKNOWN"
            },
            "zone": "null",
            "device": "Unknown",
            "id": null,
            "ipAddress": "136.147.62.8",
            "geographicalContext": {
                "city": null,
                "state": null,
                "country": "United States",
                "postalCode": null,
                "geolocation": {
                    "lat": 37.751,
                    "lon": -97.822
                }
            }
        },
        "authenticationContext": {
            "authenticationProvider": null,
            "credentialProvider": null,
            "credentialType": null,
            "issuer": null,
            "interface": null,
            "authenticationStep": 0,
            "externalSessionId": "trsVBXNk1krSSSPtoz6WBymZA"
        },
        "displayMessage": "User reset password for Okta (by admin)",
        "eventType": "user.account.reset_password",
        "outcome": {
            "result": "SUCCESS",
            "reason": null
        },
        "published": "2020-05-26T13:41:10.024Z",
        "securityContext": {
            "asNumber": 14340,
            "asOrg": "salesforce.com  inc.",
            "isp": "salesforce.com  inc.",
            "domain": "salesforce.com",
            "isProxy": false
        },
        "severity": "INFO",
        "debugContext": {
            "debugData": {
                "requestId": "Xs0cdaY3os7Dp0RrBi5ZQAAAB-Q",
                "requestUri": "/api/v1/users/00urdbalriusuaL5g0h7/lifecycle/reset_password",
                "threatSuspected": "false",
                "url": "/api/v1/users/00urdbalriusuaL5g0h7/lifecycle/reset_password?sendEmail=false"
            }
        },
        "legacyEventType": "core.user.config.user_status.password_reset",
        "transaction": {
            "type": "WEB",
            "id": "Xs0cdaY3os7Dp0RrBi5ZQAAAB-Q",
            "detail": {}
        },
        "uuid": "8ed048d9-9f56-11ea-8513-3942826c9987",
        "version": "0",
        "request": {
            "ipChain": [
                {
                    "ip": "136.147.62.8",
                    "geographicalContext": {
                        "city": null,
                        "state": null,
                        "country": "United States",
                        "postalCode": null,
                        "geolocation": {
                            "lat": 37.751,
                            "lon": -97.822
                        }
                    },
                    "version": "V4",
                    "source": null
                }
            ]
        },       

EDIT it does work from Imperative Apex but I have to parse twice

logInHistory() {       
        userHistory({federationId: this.federationId})
            .then(result => {
                this.showLoginHistory = true;
                const parsedResult = JSON.parse(result);               
                const parsedII = JSON.parse(parsedResult);               
                this.historyContinuation = parsedII;                
                this.showSpinner = false;
            })
            .catch(error => {
                this.historyContinuation = error;
                this.showSpinner = false;
                alert("There was an error while fetching the patient's log in history");
            })
    }

2 Answers 2

2

You serialized it once, then deserialized it once. This has the net effect of doing nothing at all. Also, your wired method parameter is incorrect, so you're actually serializing the wrong object. Here's the recommended changes:

public static String processLoginResponse(List<String> labels, Object state){        

And

wiredHistory({data, error}){
  if(data) {
    const parsedData = JSON.parse(data);
    ...
1
  • @sfdx sorry that was the callback for the continuation class. Not the method that the wired Apex actually is called. But it is the method that provides the return value. Commented May 31, 2020 at 17:33
1

You can use Json2Apex to create Apex class for your objects and JSON class deserialize method.

1
  • I tried that earlier and was getting errors when trying to deserialize the wrapper class. Also because this is getting displayed in a lightning data table I would like to just parse the JSON in the browser. Commented May 31, 2020 at 16:21

You must log in to answer this question.

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