Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Notification Email Script LOOP

iamspencer
Giga Contributor

Hello Community,

I need your help. Saving the backstory i need to do what i feel should be simple but for whatever reason i am unable to use my knowledge to be successful.

I have a working email script that is working, but i have (12) copies of this code in the script and i know i can reduce this down to one occurance in which it can interate through an array and only have one piece of code.

*****

if (current.variables.ldap != 'Yes') {
template.print("<br/><br/>" + current.variables.ldap.getGlideObject().getQuestion().getLabel() + ": <b>" +
current.variables.ldap.getGlideObject().getQuestion().getValue() +
"</b><br/> Implemented at go-live?: <b>" +
current.variables.ldap_go_live.getGlideObject().getQuestion().getValue() +
"</b><br/> Additional details/Comments: <b>" +
current.variables.ldap_reason.getGlideObject().getQuestion().getValue())+"</b>";
} else {
template.print("<br/><br/>" + current.variables.ldap.getGlideObject().getQuestion().getLabel() + ": <b>" +
current.variables.ldap.getGlideObject().getQuestion().getValue() +
"</b><br/> Additional details/Comments: <b>" +
current.variables.ldap_reason.getGlideObject().getQuestion().getValue())+"</b>";
}

*****

 

What im trying to do is this...

 

var VARIABLE = ['ldap','fsso'];  //<<<will be all 12 names in the list<<<

for( var i = 0; i < myArray.length; i ++){
 var qna = myArray [i];
 if (current.variables.[VARIABLE] != 'Yes') {
 template.print("<br/><br/>" + current.variables.[VARIABLE].getGlideObject().getQuestion().getLabel() + ": <b>" +
 current.variables.[VARIABLE].getGlideObject().getQuestion().getValue() +
 "</b><br/> Implemented at go-live?: <b>" +
 current.variables.[VARIABLE]_go_live.getGlideObject().getQuestion().getValue() +
 "</b><br/> Additional details/Comments: <b>" +
 current.variables.[VARIABLE]_reason.getGlideObject().getQuestion().getValue())+"</b>";
 } else {
 template.print("<br/><br/>" + current.variables.[VARIABLE].getGlideObject().getQuestion().getLabel() + ": <b>" +
 current.variables.[VARIABLE].getGlideObject().getQuestion().getValue() +
 "</b><br/> Additional details/Comments: <b>" +
 current.variables.[VARIABLE]_reason.getGlideObject().getQuestion().getValue())+"</b>";
 }
 break;
 }

 

 

Anyone that can help me I would be greatly appreciative.

1 ACCEPTED SOLUTION

Spencer, 

     Sounds good; I think the issue is with resolving the dot walk and string combinations. I concatenated the string to hopefully solve this and attached a rough draft of the current script. First, we should ensure the variableName and variableVaule are correct before moving to this full script. 

 

(function runMailScript(current, template, email, email_action, event) {
	var list = 'ldap,fsso,data_at_rest,tls,endpoint,patch30,network,seperation,us_data,logging,risk_asmt,pentest';
	var array = list.split(",");
	for (var i = 0; i < array.length; i++) {
		var VariableName = current.variables + "." + array[i];
        gs.log('VariableName');
        var VariableValue = VariableName.getValue();
        gs.log('VariableValue')
		if (VariableValue != 'Yes') {
            template.print(VariableName + "." + getGlideObject().getQuestion().getLabel() + ": <b>" +
                VariableName + "." + getGlideObject().getQuestion().getValue() +
                "</b><br/> Implemented at go-live?: <b>" +
                VariableName + "_go_live." + getGlideObject().getQuestion().getValue() +
                "</b><br/> Additional details/Comments: <b>" +
                VariableName + "_reason." + getGlideObject().getQuestion().getValue() + "</b>");
        } else {
            template.print("OR HERE<br/><br/>" + VariableName + "."+ getGlideObject().getQuestion().getLabel() + ": <b>" +
                VariableName + "." + getGlideObject().getQuestion().getValue() +
                "</b><br/> Additional details/Comments: <b>" +
                VariableName + "_reason." + getGlideObject().getQuestion().getValue() + "</b>");
        }
    }
})(current, template, email, email_action, event);

View solution in original post

18 REPLIES 18

Spencer, 

     Did you type syslog.list in the Filter Navigator and hit enter? 

find_real_file.png

yea - that doesn't work for me. but I can just 'system logs' > 'all' and im in same place

 

seems like it looped, but in the logs im not seeing a value from the list passed. I see 12 instances if VariableName and VariableValue.  

 

I am going to run the last full code. (in case you haven't figured it out - im new to SNOW and things like this make me insane - smile)

 

03-29 12:20:31 PM
19m ago
Information VariableName *** Script Log Entry system  
 
 
03-29 12:20:31 PM
19m ago
Information VariableName *** Script Log Entry system  
 
 
03-29 12:20:31 PM
19m ago
Information VariableName *** Script Log Entry system  
 
 
03-29 12:20:31 PM
19m ago
Information VariableName *** Script Log Entry system  
 
 
03-29 12:20:31 PM
19m ago
Information VariableName *** Script Log Entry system  
 
 
03-29 12:20:31 PM
19m ago
Information VariableName *** Script Log Entry system  
 
 
03-29 12:20:31 PM
19m ago
Information VariableName *** Script Log Entry system  
 
 
03-29 12:20:31 PM
19m ago
Information VariableName *** Script Log Entry system  
 
 
03-29 12:20:31 PM
19m ago
Information VariableName *** Script Log Entry system  
 
 
03-29 12:20:31 PM
19m ago
Information VariableName *** Script Log Entry system  
 
 
03-29 12:20:31 PM
19m ago
Information VariableName *** Script Log Entry system  
 
 
03-29 12:20:31 PM
19m ago
Information VariableName *** Script Log Entry system

 

 

 
03-29 12:20:31 PM
21m ago
Information VariableValue *** Script Log Entry system  
 
 
03-29 12:20:31 PM
21m ago
Information VariableValue *** Script Log Entry system  
 
 
03-29 12:20:31 PM
21m ago
Information VariableValue *** Script Log Entry system  
 
 
03-29 12:20:31 PM
21m ago
Information VariableValue *** Script Log Entry system  
 
 
03-29 12:20:31 PM
21m ago
Information VariableValue *** Script Log Entry system  
 
 
03-29 12:20:31 PM
21m ago
Information VariableValue *** Script Log Entry system  
 
 
03-29 12:20:31 PM
21m ago
Information VariableValue *** Script Log Entry system  
 
 
03-29 12:20:31 PM
21m ago
Information VariableValue *** Script Log Entry system  
 
 
03-29 12:20:31 PM
21m ago
Information VariableValue *** Script Log Entry system  
 
 
03-29 12:20:31 PM
21m ago
Information VariableValue *** Script Log Entry system  
 
 
03-29 12:20:31 PM
21m ago
Information VariableValue *** Script Log Entry system  
 
 
03-29 12:20:31 PM
21m ago
Information VariableValue *** Script Log Entry system

ran new code and ..

 

03-29 12:53:34 PM
just now
Warning org.mozilla.javascript.EcmaError: "getGlideObject" is not defined.
Caused by error in Email Script: 'rp_mssa_email_loop' at line 49

46: var VariableValue = VariableName.getValue();
47: gs.log('VariableValue');
48: if (VariableValue != 'Yes') {
==> 49: template.print(VariableName + "." + getGlideObject().getQuestion().getLabel() + ": <b>" +
50: VariableName + "." + getGlideObject().getQuestion().getValue() +
51: "</b>
Implemented at go-live?: <b>" +
52: VariableName + "_go_live." + getGlideObject().getQuestion().getValue() +
Evaluator Log Entry system  
 
 
 
 
         

 

syslog.message=org.mozilla.javascript.EcmaError: "getGlideObject" is not defined.
Caused by error in Email Script: 'rp_mssa_email_loop' at line 49

46: var VariableValue = VariableName.getValue();
47: gs.log('VariableValue');
48: if (VariableValue != 'Yes') {
==> 49: template.print(VariableName + "." + getGlideObject().getQuestion().getLabel() + ": <b>" +
50: VariableName + "." + getGlideObject().getQuestion().getValue() +
51: "</b><br/> Implemented at go-live?: <b>" +
52: VariableName + "_go_live." + getGlideObject().getQuestion().getValue() +

 

so taking your lead... I stripped the script down further:

(function runMailScript(current, template, email, email_action, event) {
	var list = 'ldap,fsso,data_at_rest,tls,endpoint,patch30,network,seperation,us_data,logging,risk_asmt,pentest';
	var array = list.split(",");
	for (var i = 0; i < array.length; i++) {
		var VariableName = current.variables + "." + array[i];
		template.print("<br / >>>>>>" + VariableName + "....." + array[i]);
        var VariableValue = VariableName.getDisplayValue();
    }
})(current, template, email, email_action, event);

 

That produced this: so... what this tells me is that the 'current.variables' is not prepending to the value from the array but that the array is populating [i].

 

so... getting closer. going to move current.variables into the string.

 

more to come. i think we are close!!!

 

*****THERE SHOULD BE SOMETHING AFTER THIS LINE

>>>>>.ldap.....ldap
>>>>>.fsso.....fsso
>>>>>.data_at_rest.....data_at_rest
>>>>>.tls.....tls
>>>>>.endpoint.....endpoint
>>>>>.patch30.....patch30
>>>>>.network.....network
>>>>>.seperation.....seperation
>>>>>.us_data.....us_data
>>>>>.logging.....logging
>>>>>.risk_asmt.....risk_asmt
>>>>>.pentest.....pentest

Spencer; 

Try this too:

(function runMailScript(current, template, email, email_action, event) {
	var list = 'ldap,fsso,data_at_rest,tls,endpoint,patch30,network,seperation,us_data,logging,risk_asmt,pentest';
	var array = list.split(",");
	for (var i = 0; i < array.length; i++) {
		var VariableName = current.variables + "." + array[i];
		template.print("<br / >>>>>>" + VariableName + "....." + array[i]);
        var VariableValue = VariableName + "." + getDisplayValue();
        template.print("<br / >>>>>>" + VariableValue);
    }
})(current, template, email, email_action, event);