0

I need a help with this javascript above;

In my context, i have a Data Extension (named: CountsDE2)that is populate via integration. I lookup a report that I can accompany the growth rate by daily this Data Extension; In my searchs the better method is create a Data Extension and populate this via query and after use a javascript to have my growth rate.

The Query it's normally, but the javascript not working. enter image description here

<script runat="server">

var dataRows = Platform.Function.LookupOrderedRows('CountsDE2', 'Counts','Hour DESC',['Secret'],[1]);


if (dataRows && dataRows.length > 0) {

var today = dataRows[0]["Hour"]; //Sets the most recent date
var num1 = dataRows[0]["Count"]; //Sets the most recent count entry
var num2 = dataRows[1]["Count"]; //Sets the previous count entry
}

var growthRaw = (num2 / num1) * 100;
var growth = (growthRaw).toFixed(2) + '%';

var rows = Platform.Function.UpdateData("CountsDE2",["Hour"], [today],["Growth"],[growth]);

</script>
  • can you give some insight into how it is 'not working'. – Gortonington Dec 13 '18 at 14:25
  • did i just do the exact same edit like you @Gortonington? :D – Johannes Schapdick Dec 13 '18 at 14:25
  • 2
    Fastest edits in the west spins guns – Gortonington Dec 13 '18 at 14:28
  • Hello @Gortonington, Happens a errored that Automation Studio does not get me the details, but i changed the declaration of Server-Side JavaScript Block to " <script runat=server language="JavaScript" executioncontexttype="post">" and now does not appears error, but the script simply does nothing. – Joao Marano Dec 13 '18 at 17:29
  • the error is likely happening since there is only 1 entry in your DE. The script requires at least 2 records. – Gortonington Dec 13 '18 at 18:59
1

I found the issue, which is actually inaccurate data in the SFMC SSJS LookupOrderedRows() documentation. The second parameter should be the number of rows you want to look up, not 'the field you want to retrieve' like the documentation says.

See below for corrected SSJS (I have tested it and it produced expected results)

<script runat="server">

var dataRows = Platform.Function.LookupOrderedRows('CountsDE2', 2,'Hour DESC',['Secret'],[1]);


if (dataRows && dataRows.length > 0) {

var today = dataRows[0]["Hour"]; //Sets the most recent date
var num1 = dataRows[0]["Counts"]; //Sets the most recent count entry
var num2 = dataRows[1]["Counts"]; //Sets the previous count entry
}

var growthRaw = (num2 / num1) * 100;
var growth = (growthRaw).toFixed(2) + '%';

var rows = Platform.Function.UpdateData("CountsDE2",["Hour"], [today],["Growth"],[growth]);

</script>
  • Hello, @Gortinington! You are awesome, simply fantastic! – Joao Marano Dec 20 '18 at 13:12

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.