1

I'm picking up salesforce marketing cloud and refreshing myself in AMPscript/SSJS. Could you give me some feedback on the following snippet? I don't have access to SFMC to test out, so I'm just reading the documentation and using the forums.

For context, I was given a javascript script snippet and told to reproduce in SFMC for an email send. I thought about doing it all in AMPscript but creating a functions to create a random string, and reproducing Math.Random() and charCodeAt in AMPscript isn't quite the same. I feel a combination of AMPscript and SSJS would work better? Most of the javascript I was given hasn't been changed, I just refactored. If you could let me know if this would work and would be an efficient way to do it.

<!-- Setting AMPscript variables -->
%%[
Var @url, @field_1
Set @url = ""
Set @field_1 = "Random String"
]%%

<!-- SSJS Block to do the math for link -->
<script type="text/javascript" runat="server">
Platform.Load("core", "1.1.1");

var myStr = ""
var gap = "~"
var encString = "";

// Function to get random string
function getRandStr(leng) {
  var str = "";
  for(var i = 0; i < leng; i++){
      str += parseInt(Math.Random()*5); 
  }
  return str;
}
// Get value from AMPscript variable
myStr = Platform.Variable.GetValue('@field_1');

// Create random url
for(j = 0; j < myStr.length; j++) {
    encString += getRandStr(5) + myStr.charCodeAt(j) + gap + getRandStr(3);
}
// Pass back to AMPscript variable
Platform.Variable.SetValue("@url", encString);

</script>

<!-- inline AMPscript to print url -->
<a href='%%=Concat("http://", V(@url))=%%' class="button">Click Me</a>
2
  • 1
    Can you use the GUID ampscript function for this purpose? developer.salesforce.com/docs/… Nov 30, 2019 at 7:02
  • @BradSapkota Yes I could use the GUID AMPscript that would be a more simple approach. But as I was given a snippet of javascript to reproduce the same URL string in SFMC, is what I've done above valid use of AMPscript and SSJS. or would this error out?
    – Kunal
    Dec 1, 2019 at 7:28

1 Answer 1

1

To answer your question, yes what you have above is mostly valid, but requires 2 little tweaks to work. I am not sure for the intention of this process as it seems somewhat convoluted, but if necessary, it can be implemented with the following changes:

  1. The first issue is that your current parseInt(Math.Random()*5) utilizes an uppercase Random which is returning a jint exception as the correct function is lowercase Math.random().

    The parseInt() is returning NaN, once this is corrected, as what is returned for str. I did not do an investigation into why, but instead have come up with alternatives (assuming you were intending to use this to make the result to be an integer without any decimalplaces). To get there, you can use:
    Math.floor(Math.random()*5) (rounds down)
    Math.ceil(Math.random()*5) (rounds up)
    (Math.random()*5).toFixed(0)(which will do essentially the same as Math.floor and just remove the decimal, but retain the integer.)
  2. The second tweak is because using the concat in the href will break the link when it is wrapped for link tracking. Basically what you need to do is just include the href inside the concat. like this:
    <a %%=Concat('href="http://',@url,'"')=%% class="button">

You must log in to answer this question.

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