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

for whatever reason - as soon as value is looked for it stops

 

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

>>>>>.ldap.....ldap
 
 
^^^it made it to LDAP but then when it went to get the value... BOOM!
 
i am going to press the release button on the long version of this script. (seen below) i need to get this out into the wild for now. I really want the clean efficient code working but at this point ive halted production to try and make this work.
 
working 'long' code is this:
 
(function runMailScript(current, template, email, email_action, event) {

  // LDAP
    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>");
    }

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

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

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

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


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

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

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

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

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

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

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



})(current, template, email, email_action, event);

Spencer, 

     For testing can you run this code and provide the output logs:

(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);

iamspencer
Giga Contributor

@DJohnson   I am breaking the reply out for testing of code below.  I need to get through usecase testing but wanted to let you know that when i place this code it stops at getting the VALUE.

RESULT in email

>>>>There should be something below here >>>>>

>>>>>.ldap.....ldap
 

 

CODE used

(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);

 

I have a total of 13 usecases to test - as soon as complete i will grab the log from the final UC

 

thank you 

iamspencer
Giga Contributor

@djohnson 

My leadership has pulled the plug on this but i know we were close so i will credit you with correct response though we need some tuning - i know it should work.

 

will try again in the future