JoernBerkefeld / SFMC-Cookbook

How to survive as a developer for Salesforce Marketing Cloud
https://joernberkefeld.github.io/SFMC-Cookbook/
MIT License
125 stars 33 forks source link

"It seems there is no way to get all parameters or the query string itself." - A solution to this problem #3

Closed okane103 closed 1 year ago

okane103 commented 1 year ago

Hey Joern, thank you for the fantastic resource on SFMC development. I was reading through your website and had a thought when I read the portion here:

"It seems there is no way to get all parameters or the query string itself."

You can actually get the query string by using the 'PAGEURL' parameter, and parsing out the query string portion:

<script runat="server">
  Platform.Load("Core", "1");

  var pageUrl = Platform.Request.GetQueryStringParameter("PAGEURL");
  var queryString = pageUrl.split("?").reverse()[0];
</script>

Just thought it was worth noting. Thanks again for the time you have put into this resource, it has been a huge help to me over the years.

JoernBerkefeld commented 1 year ago

Hi @okane103 love the idea! I was wondering how in the world you found out about that one and googled your solution. Over at ampscript.xyz Ivan Razine found another option to get the current URL that seems more straightforward:

var pageUrl = Platform.Request.RequestURL;

To turn that into a complete example:

Platform.Load("core", "1.1.1");

var queryParams = {};
var pageUrl = Platform.Request.RequestURL;
var helper = pageUrl.split("?");

if(helper.length == 2) {
    var queryArr = helper[1].split('&');
    for(var i=0; i<queryArr.length; i++) {
        var keyVal = queryArr[i].split('=');
        queryParams[keyVal[0]] = keyVal[1];
    }
}

// queryParams should now be an object holding the query parameters with parameter name as keys

haven't had time to confirm this but once I did, both options will certainly make it into the guide :)

Quick question: What did you intend to do with the reverse()? If there are no query parameters the array pageUrl.split("?") would only hold one element and hence reverse shouldn't help to avoid that?

Thanks again for pointing this out! 👍 That's exactly why I kept this blog on Github to encourage collaboration like this <3

okane103 commented 1 year ago

Hi Joern,

I had just been recently using the 'PAGEURL' parameter in AMPscript and figured I'd test if that same value could be pulled in SSJS as if it were a query param, and it seemed to work. I agree using Platform.Request.RequestURL is a bit cleaner.

I tend to use reverse()[0] when I want a quick one-line way to get the last element of an array. You are absolutely correct that it would not account for there being no query string present, and isn't actually a full solution I would want to use for parsing out the query parameters. The example you have is a much more complete solution, I like it!

JoernBerkefeld commented 1 year ago

finally updated the page - the cache for github.io should refresh it shortly