1

I have this apex code to call the Salesforce Function :

public String tokenExampleFunction() {
    functions.Function idTokenFunction = functions.Function.get('FunctionsOrg.tokenexample');
    String idTokenRequest = '{ "idToken" : "' + idToken + '" }';

    functions.FunctionInvocation invocation = idTokenFunction.invoke(idTokenRequest);
    String idtokenJSON = invocation.getResponse();

    return idtokenJSON;
}

Then in my Salesforce Function that is written in TypeScript :

 * Following parameters are pre-configured and provided to your function on execution:
 * @param event: representative of the data associated with the occurrence of an event,
 * and supporting metadata about the source of that occurrence.
 * @param context: represents the connection to the the execution environment and the Customer 360 instance that
 * the function is associated with.
 * @param logger: represents the logging functionality to log given messages at various levels
 */
export default async function (event,context,logger){
    // I thought this is right way to get the variable from Apex but looks like it isn't working
    const jsonPayload = JSON.parse(event.data) 
    const idToken = jsonPayload.idToken
    return idToken
}

When running the code, idToken = null.

Can someone help me out how to pass the String idTokenRequest from Apex to then Salesforce functions the right way?

1 Answer 1

1

First, I recommend you don't build your own JSON. The example docs do, but the preferred method would be:

String idTokenRequest = JSON.serialize(
  new Map<String, Object> {
    'idToken' => idToken
  }
);

This ensures that if idToken has characters that need escaping, this will happen automatically. Even if that might not ever be the case here, I'd still recommend it so you're in the habit of doing so.

Second, there's already a serialize/deserialize layer as the function is called from Apex. In Use JavaScript and TypeScript Functions, it says:

When your Javascript function runs, Salesforce Functions handles converting JSON data into a Javascript object.

For example, if your function accepts JSON input like:

{
  "fieldOne": "Hello World!",
  "fieldTwo": 23
}

Salesforce Functions converts it into a Javascript object:

const input = {
  fieldOne: "Hello World!",
  fieldTwo: 23
};

Output

You must ensure that Javascript functions return objects that can be serialized to JSON.

So, you don't need to serialize or deserialize explicitly in your TypeScript at all in the usual sense, because it has already been taken care of for you.

const jsonPayload = JSON.parse(event.data) 

Is working on a toString implementation of data, which somehow isn't crashing, as I'd expect with JSON.parse({idToken:"someValue"}), but whatever it is you get back on the next line:

const idToken = jsonPayload.idToken

Does not have an idToken property. TypeScript might be swallowing the parsing error, leading idToken to be a null/undefined value.

Edit: Just in case I'm not being clear enough, your final code could be written as:

export default async function (event,context,logger){
  return event.data.idToken;
}

And you'd see the expected result.

2
  • On the first part of the building JSON yourself, I agree with you but not sure why their official docs love to build JSON themselves as in (developer.salesforce.com/docs/platform/functions/guide/…)
    – compski
    Commented Apr 10, 2023 at 1:37
  • 1
    @compski The docs aren't usually prepared with best practices in mind, but rather to be as terse as reasonable. I agree it'd be nice to have best practices directly in the docs, though.
    – sfdcfox
    Commented Apr 10, 2023 at 6:48

You must log in to answer this question.

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