0

in callBack function of runApex(), it reruns JSON...I want to get value from the callback function so that I could perform my logic but I am unable to do so because not having the exact value but a JSON string.

sforce.opencti.runApex({
                        apexClass : 'myController',
                        methodName : 'getStatus',
                        methodParams : 'providerClass=' + providerClass,
                        callback : function(result) {
                            if (result.success) {
                                var retVal = result.returnValue;
                                console.log('This is return value >>' , result.returnValue);

                                if( result.returnValue.status == " ringing")
                                {
                                   console.log('This is taking the retun value as ringing');
                                    //set the label to Dialing and dont start the time!
                                }

                            } else {
                                throw new Error('Unable to make a call. Contact your admin.');
                            }
                        }
                    });
5
  • So is there some problem calling JSON.parse? Is this code Javascript?
    – Adrian Larson
    Jan 29, 2019 at 22:00
  • if( result.returnValue.status == " ringing") here I want to get the return value which could be (status) ringing but how can I convert the JSON into JavaScript code and then only get the status however JSON is returning many things...
    – sami ullah
    Jan 29, 2019 at 22:12
  • 1
    can you edit the question and add details about the output from this: console.log('This is return value >>' , result.returnValue); What does the payload look like currently?
    – Mark Pond
    Jan 29, 2019 at 22:14
  • it is giving me this value: {runApex: "{↵ "status" : "ringing"↵}"}
    – sami ullah
    Jan 29, 2019 at 22:17
  • and if I use onsole.log('This is return value >>' + result.returnValue); then it is returning This is return value >>[object Object]
    – sami ullah
    Jan 29, 2019 at 22:18

1 Answer 1

1

One detail in your question may be critical, there's an extra space in the string you're comparing:

if( result.returnValue.status == " ringing")

Per the documentation about runApex() in the CTI toolkit.

The output along with your comment is going to be like so:

{
    "success": true,
    "returnValue": {
        "runApex": "{\"status\":\"ringing\"}"
    },
    "errors": null
}

Making your javascript to handle the results look like this:

sforce.opencti.runApex({
    apexClass : 'myController',
    methodName : 'getStatus',
    methodParams : 'providerClass=' + providerClass,
    callback : function(result) {
        if (result.success) {
            var retVal = result.returnValue;

            var returnJSON = JSON.parse(retVal); // parse the string into an object
            console.log(returnJSON);

            if ( returnJSON.status == "ringing")
            {
               console.log('This is taking the retun value as ringing');
                //set the label to Dialing and dont start the time!
            }

        } else {
            throw new Error('Unable to make a call. Contact your admin.');
        }
    }
});

You must log in to answer this question.

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