0

I'm attempting to use the AJAX Toolkit to access the current Account ID of the logged-in user from within a community site. (The particular community is managed by Nimble AMS, and is referred to as "Community Hub." It appears to be a fairly straightforward force.com community site.) The reason I need to access the Account ID is that I need to pass the current user's account ID to an embedded widget that lives in one of our Community Hub pages (a Wufoo form, to be specific).

At the advice of my implementation team, I am using the AJAX Toolkit. As a proof of concept, I'm calling the following code in the $(document).ready() handler (paraphrasing a bit here):

function getAccountId() {
    sforce.connection.sessionId = window.ApiUtils.getSessionId();
    sforce.connection.serverUrl = '/communityhub/services/Soap/u/46.0';

    try{
        var userInfo = sforce.connection.getUserInfo();
        console.log(userInfo);
    }
    catch(error) {
        console.log(error);   
    }
}

However, I'm getting an error message in the console indicating that the current user (I'm logged in as a regular Community Hub user) doesn't have permission to access the API:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sf="urn:fault.partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <soapenv:Fault>
            <faultcode>sf:INVALID_SESSION_ID</faultcode>
            <faultstring>INVALID_SESSION_ID: This session is not valid for use with the API</faultstring>
            <detail>
                <sf:UnexpectedErrorFault xsi:type="sf:UnexpectedErrorFault">
                    <sf:exceptionCode>INVALID_SESSION_ID</sf:exceptionCode>
                    <sf:exceptionMessage>This session is not valid for use with the API</sf:exceptionMessage>
                </sf:UnexpectedErrorFault>
            </detail>
        </soapenv:Fault>
    </soapenv:Body>
</soapenv:Envelope>

I have tried enabling the 'API Access' permission in the Profile for "Community Hub User," which is the profile attached to the user that I'm logged in as, but the error message continues.

Diagram of the System Permissions settings for the Community Hub Login User

Diagram showing profile assigned to current user

So here's the question. How can I overcome that error? What else, besides enabling API Access, do I need to do to allow this user to make an API call through the AJAX Toolkit to retrieve the current Account ID?

1
  • One further note -- I cannot 'embed' this value into the page because it is being rendered in a the custom label of a 'Card' with type Plain Text 2 and the Render HTML card layout. This content delivery mechanism apparently does not support the macros that are sometimes used to embed values into VisualForce pages.
    – Ben Birney
    Commented Jun 13, 2019 at 20:36

1 Answer 1

1

Here is the answer I found after numerous conversations with a generous assortment of consultants.

Community users cannot access the AJAX Toolkit, period. (Though the linked discussion is elderly, it is apparently still a legitimate limitation of Communities.) That hacky UserContext hack that you see elsewhere on the web doesn't work consistently, if at all, and isn't guaranteed to work in the future. You need to write the value into the page source using a merge field.

In the case of Nimble AMS specifically, this can be accomplished by setting the Object and Data Source of the 'Card' object, of type Plain Text 2 and with layout Display HTML, and then rendering the merge field in the card's description label. I selected Account as the Object, CurrentAccount as the Data Source, and Display HTML as the card layout. In the Description Label, I add the following:

<script>
var currentAccountId = '{!Account.Id}';
</script>

That results in the current account ID being rendered to the card's source in the HTML output. From there it can be interrogated by Javascript and used as needed.

Not sure if this procedure works generally for Community sites, but hopefully this helps someone find the answer.

You must log in to answer this question.

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