cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Script to extract measure values and loop through them on initialization of page in SAC

MadhuLodugu48
Participant
0 Kudos
1,148

Can anyone show me how to code a custom script in SAC optimized design story (on initialization of page) to load the measure values in a table into a variable and then loop through the measure values to check for a condition for each of the values.

I want to create a conditional alert but not able to add code successfully to extract measure values like described above. I have only one measure in the table.

 

Regards,

 

Accepted Solutions (0)

Answers (2)

Answers (2)

GiriRaaj
Product and Topic Expert
Product and Topic Expert

Hi ,

consider the below example and sample code:

Example :
Your table is named Table_1.
Your measure is in column index 0 (since there's only one measure).
Your condition is, for example, checking if any value exceeds 1000.


Ref. code :

let ds = Table_1.getDataSource();


ds.getData({
includeDimensions: false,
includeMeasures: true
}).then(function(data) {

let measureValues = [];

for (let i = 0; i < data.length; i++) {
let measureValue = data[i][0]; // First column contains the measure value
measureValues.push(measureValue);
}

 

let alertTriggered = false;
for (let i = 0; i < measureValues.length; i++) {
if (measureValues[i] > 1000) { // Your condition
alertTriggered = true;
break; // Exit loop on first match
}
}


if (alertTriggered) {
Application.showMessage(ApplicationMessageType.Error, "Warning: Some values exceed the limit!");
}
});

 

Hope this helps,

Thank you!

Giri

SAP - PSCC 

 

MadhuLodugu48
Participant
0 Kudos
Hi @GiriRaaj, Thank you!. Can I use the script as is by just changing the relevant widget names & logic to those from my story/use case?. Is this the only way to use Alerts in SAC?, if so when is the alert triggered - on running the story?
JBARLOW
Active Contributor
0 Kudos
Stupid question time from me.... But is that actually valid sac script syntax? It looks like the code that chatgpt writes... I.e. The let function :let ds = Table_1.getDataSource(); shouldn't that be var ds=etc
JBARLOW
Active Contributor
0 Kudos

Hi there,

As a simple example, the code below will loop through all measure values in the table,
As soon as a value is greater than 282 - the script will launch an application message or a text box etc.
I've created 2 buttons one for each...

var arr = Table_3.getDataSource().getResultSet();

for (var i=0;i<arr.length;i++)
	{   var string = arr[i][Alias.MeasureDimension].rawValue;	    
                    var number = ConvertUtils.stringToNumber(string);		     
if(number>282)   {		Text_1.setVisible(true);	break	   }}	
var arr = Table_3.getDataSource().getResultSet();

for (var i=0;i<arr.length;i++)
	{   var string = arr[i][Alias.MeasureDimension].rawValue;	    
                    var number = ConvertUtils.stringToNumber(string);		     
if(number>282)   {		Application.showMessage(ApplicationMessageType.Info,"test");	break	   }}	

Measurevaluecheck-ezgif.com-video-to-gif-converter.gif

MadhuLodugu48
Participant
0 Kudos
Hi @JBARLOW, the second script is more relevant but would like to know if the same can be customized with code to trigger an email to intended recipients when the condition is satisfied (i.e., number > 282)?
MadhuLodugu48
Participant
0 Kudos

Hi @JBARLOW, the script you provided worked to pop a text message on fulfillment of the condition coded. I am trying to do the same to trigger an email having problem with it as the email is getting delivered even if the condition in the story is not fulfilled.

Is the requirement to send email notifications to intended recipients on a scheduled  basis whenever the particular condition is met (for instance, revenue > 120000) feasible in SAC?. Please let me know if you are aware of it or tried this earlier?