1

I have seen there are several posts on a similar topic with answers but I can't seem to use any of that information to assist my issue; unless I am not understanding something correctly.

I have an object that I am converting to a JSON string. I can see the multidimensional object in the browser console, but when I stringify the object and print out the new variable it simply states {}.

Here is my JavaScript snippet:

//consider up until this point the object has been created in the variable returnObject
console.log("returnObject", returnObject);
var returnString = JSON.stringify(returnObject);
console.log("returnString", returnString);

and here is a screenshot of the console terminal for these two console logs: console screenshot

Can anyone assist in why it is not converting into a proper string value?

EDIT: Here is the entire function that is being executed including the initialization of the object.Note that this is interacting with a Salesforce API:

var getTabIds = function getTabIds(result) {
        if (result.success) {
            //var returnObject = new Array();
            var returnObject = new Object();
            for (i = 0; i < result.ids.length; i++) {
                sforce.console.getSubtabIds(result.ids[i], function (subTabResult) {
                    if (subTabResult.success) {
                        for (i = 0; i < subTabResult.ids.length; i++) {
                            var subTabId = subTabResult.ids[i];
                            sforce.console.getPageInfo(subTabId, function (page) {
                                var subTabInfo = jQuery.parseJSON(page.pageInfo);
                                var id = subTabInfo.objectId.toString();
                                returnObject[id] = subTabInfo;
                                Object.defineProperty(returnObject, id, {
                                    enumerable: true
                                });
                                //returnObject.push(page.pageInfo);
                            });
                        }

                        console.log("returnObject", returnObject);
                        var returnString = JSON.stringify(returnObject);
                        console.log("returnString", returnString);
                        console.log("keys", Object.keys(returnObject));
                        console.log(Object.getOwnPropertyNames(returnObject), returnObject["001N0000016Shzx"])

                        $Lightning.use("c:LO_InteractionLogApp", function() {
                            $Lightning.createComponent("c:LO_InteractionLogComponent",
                                                       null,
                                                       "lightning",
                                                       function(cmp) {
                                                           var myExternalEvent;
                                                           myExternalEvent = $A.get("e.c:InteractionLogRelatedRecordsResponse");
                                                           myExternalEvent.setParams({
                                                               "response": returnString
                                                           });
                                                           myExternalEvent.fire();
                                                       });
                        });
                    }
                });
            }
        }
    };

In short in Salesforce console there are parent tabs and child tabs (its a workspace that allows you to open multiple records in one UI) and I am trying to get the record IDs of all records that are currently open via the integration toolkit. This is not so important but figured the context might help.

David Dawson
  • 249
  • 1
  • 4
  • 9
  • where is `returnObjject` defined? – Daniel A. White Feb 23 '17 at 00:43
  • @DanielA.White in the same function above the code snippet I have provided. You can see from the console log right before the stringify is executed that it has access to the variable – David Dawson Feb 23 '17 at 00:45
  • but how is it defined is it a constructor? – Daniel A. White Feb 23 '17 at 00:47
  • var returnObject = new Object(); @DanielA.White – David Dawson Feb 23 '17 at 00:49
  • JSON *is* a string. You have a JS object that you're trying to serialize to JSON. As canon stated, properties must be enumerable. If `Object.keys(returnObject)` produces an empty array, then that's likely the problem. –  Feb 23 '17 at 00:55
  • yes the above returns an empty array. How do I resolve the issue? – David Dawson Feb 23 '17 at 01:02
  • @canon You might be right, however we are simply lacking the details of how those properties are created. I guess closing as "not enough code to reproduce" might have been more appropriate in hindsight. Still I'd wager an upvote that it's an asynchrony issue, not an enumerability one :-) – Bergi Feb 23 '17 at 01:20
  • 1
    @Bergi: Since the properties are printed in dark purple (as oppose to light purple, e.g. like `__proto__`), I guess you win ;) Dark purple = enumerable, light purple = non-enumerable. – Felix Kling Feb 23 '17 at 01:25
  • @DavidDawson What does `console.log(Object.getOwnPropertyNames(returnObject), returnObject["001N0000016Shzx"])` yield? – Bergi Feb 23 '17 at 01:25
  • @FelixKling Ah, thanks for the confirmation :-) – Bergi Feb 23 '17 at 01:26
  • @Bergi it returns an empty array or "[] undefined". I can post my entire function but I am tying into another javascript API and working with their JSON payloads so I am not sure how much it will help? – David Dawson Feb 23 '17 at 01:28
  • 1
    @DavidDawson Thanks, that's just further confirmation the duplicate is appropriate. The console is showing you the current state of the object when you view it, not the one when it was logged. Your `getPageInfo` function is asynchronous, you'll need to wait for it. – Bergi Feb 23 '17 at 01:43

0 Answers0