3

I am very new at this and the answer is probably very simple, but please help.

I get the following returned from a query, this is a jsonParse of the result

{"totalSize":1,
"done":true,
"records":
    [{"attributes": 
    {
        "type":"Account",
        "url":"/services/data/v21.0/sobjects/Account/001C0000012Z8Y5IAK"
    },
    "Id":"001C0000012Z8Y5IAK"
    }] 
}

How do I extract the Id at the end into a variable when totalSize = 1?

1 Answer 1

1

objectName.records[0].Id will do the trick. Increment records index appropriately.

Sample -

var sforceResponse = {"totalSize":1,"done":true,"records":[{"attributes":{"type":"Account","url":"/services/data/v21.0/sobjects/Account/001C0000012Z8Y5IAK"},"Id":"001C0000012Z8Y5IAK"}]}

sforceResponse.records[0].Id //this variable contains the Id. 
Sign up to request clarification or add additional context in comments.

5 Comments

I get "TypeError: Cannot read property "Id" from undefined." Here is my code
var fields = "Id"; var soql = "SELECT "+fields+" FROM Account WHERE Site_ID__c = '"+ siteID +"'LIMIT 100"; var queryUrl = instanceUrl + "/services/data/v21.0/query?q="+encodeURIComponent(soql); var response = UrlFetchApp.fetch(queryUrl, { method : "GET", headers : { "Authorization" : "OAuth "+accessToken } }); var queryResult = Utilities.jsonParse(response.getContentText()); if (queryResult["totalSize"]=1) { var acctId = queryResult.records[0].Id; } else { var acctId = "NotFound"; }
First thing I noticed is that you want to change this line - queryResult["totalSize"]=1 to be queryResult["totalSize"]===1. The code as is will always be true as its just an assignment and the 2nd version will actually do the comparision. Does that help?
Thank you very much. I assumed I had a syntax error and never thought about this as a data error. Problem solved.
Great. Glad to hear. Mark this as accepted so that the next person knows what fixes the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.