1

I have this code and I want to store the data of auth API into a data extension. I am able to successfully generate the result of API but not able to store that data in a data extension.

You can see my code below:

<script runat="server">
Platform.Load("Core", "1")
    var str = Variable.GetValue("@JSON");
    var obj = Platform.Function.ParseJSON(str);
    var token = obj.access_token;
    var auth = 'Bearer ' + token;
    var req = new Script.Util.HttpRequest("https://api.socialstudio.radian6.com/v3/users/me");
        req.emptyContentHandling = 0;
        req.retries = 2;
        req.continueOnError = true;
        req.contentType = "application/json"
        req.setHeader("Authorization", auth);
        req.method = "GET";
    var resp = req.send();
    var resultJSON = Platform.Function.ParseJSON(String(resp.content));
    var item = resultJSON.data[0];
    Write(item['id']);
    var row = Platform.Function.UpsertDE("Auth_API", ["id"], [item['id']], ["username"], [item['username']]);
</script>

The UpsertDE function is not working and is showing this error while loading the cloud page: 500 - Internal server error.

I have checked the Attributes and the Data Extension name and they are the same.

4
  • have you tried to debug this in emailstudio to get better error messages? Do you have non nullable fields in the DE?
    – EazyE
    Jul 6 at 21:23
  • Give putting a try... catch around your code a go and take a look at the nature of the error.
    – Macca
    Jul 7 at 0:17
  • Can you share an example output of item? to make sure that the JSON structure matches? Have you validated that the outputs are as expected? Writing both id and username values? Have you put these into a var and pushed into the function rather than have them raw inside the array? Have you run it without the upsert function to validate it is 100% that function? Jul 12 at 19:27
  • Also - long story short on the Answers below around UpsertDE and UpsertData - the recommendation would be to use UpsertData on a CP or LP as it is optimized in this environment and as the functions are 100% identical, it is not a huge lift to implement. But neither of these would cause a 500 error - so not likely to be the root cause of the issue. Jul 12 at 19:38
2

The UpsertDE method can be used in sendable contexts, such as email messages.

UpsertData should be used in non-sendable contexts, such as landing pages.


UpsertData:

"Updates or inserts information into a data extension as indicated by the array containing the listed column name and value pairs. This method adds the information if the data extension does not contain an applicable value or updates the existing applicable value to the new value. You can include multiple column name and value parts in a single call. You can use this method in non-sendable contexts, such as landing pages. This call returns the number of rows modified."

https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-programmatic-content.meta/mc-programmatic-content/ssjs_platformDataExtensionUpsertData.htm


UpsertDE:

Updates or inserts information into a data extension as indicated by the array containing the listed column name and value pairs. This method adds the information if the data extension does not contain an applicable value or updates the existing applicable value to the new value. You can include multiple column name and value parts in a single call. You can use this method in sendable contexts, such as email messages. https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-programmatic-content.meta/mc-programmatic-content/ssjs_platformDataExtensionUpsertDE.htm

11
  • I answered this question with the same suggestion and it is not the case as @LukasLunow corrected me because you can use both SSJS functions on the landing page. Try it out yourself.
    – Niko
    Jul 10 at 15:58
  • @Niko I just tested both. UpsertData worked and UpsertDE did not. I have not seen any evidence that UpsertDE works on cloudpages. Can you share a link to that?
    – jake314159
    Jul 12 at 15:50
  • Hello @jake314159, I said that since I have tested as well and even re-tested now and it worked again with UpsertDE in CloudPage. I do not think it is documented (at least I did not find anything). Here is a short video showing the results from my end - loom.com/share/72b304c128cb47ce824cb192cf8def26 :) I do wonder why our results differ though...
    – Niko
    Jul 12 at 16:22
  • @Niko, from your video it looks like the UpsertDE is running when you first preview/publish the page, not when you visit the page itself. I'd recommend outputting the result of the UpsertDE/UpsertData on the page (not just any possible errors with try/catch) so you can see the result of the function call on the page. The preview of cloudpages will process the code differently than the actual page. You can prevent your code from running in the Preview w/this AMPscript: %%[if indexof(requestparameter('pageurl'),'cloudPages/preview') < 1 then]%% (could also be written in SSJS)
    – jake314159
    Jul 12 at 16:48
  • Nope, it actually adds a record when I am visiting the page, I have checked this point already. Also, it is mentioned here that the Preview feature of the CloudPage does not execute the code anymore as it was before.
    – Niko
    Jul 12 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.