The CreatorCon Call for Content is officially open! Get started here.

Not able to Print Servers in Notification.

Shidhi
Tera Contributor

Hi,

 

I'm trying to print Servers or SPM Test Names in Notification using an Email Script. But I was not able to print the required info.

 

1. The Notification is on the sc_task table.

2. Servers or SPM Test Names are List collector variables referring to different tables.

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

          // Add your code here

	/*

	Tried in this way it didn't work
	
	if(current.server_names)
	template.print(current.server_names);
	else if(current.spm_test_name)
	template.print(current.spm_test_name);
	
	*/


var task = new GlideRecord('sc_task');
task.addQuery('number', current.number);
task.query();
while(task.next()){	

	if (task.server_names)// checking if empty
	template.print(current.server_names);
	else if (task.spm_test_name)//checking if empty
	template.print(current.spm_test_name);

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

It is printing empty not sure why.

 

Please help me with this.

 

Thank you!

 

1 ACCEPTED SOLUTION

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Hi @Shidhi - I'm don't recognize those fields on the sc_task table, but you should confirm that they're valid fields (as @Allen Andreas suggests, they might be variables).

 

You can do that check in a background script:

 

(function() {
    var tableName = 'sc_task';
    var fieldNames = ['server_names', 'spm_test_name'];
    var invalidFields = [];
    
    for (var i = 0; i < fieldNames.length; i++) {
        var gr = new GlideRecord(tableName);
        var fieldName = fieldNames[i];
        
        if (!gr.isValidField(fieldName)) {
            invalidFields.push(fieldName);
        }
    }
    
    if (invalidFields.length > 0) {
        gs.info('Invalid fields on ' + tableName + ': ' + invalidFields.join(', '));
    } else {
        gs.info('All specified fields are valid on ' + tableName);
    }
})();

 

Or, just as easily, in your mail script:

 

template.print('<p>server_names is valid: ' + current.isValidField('server_names')+ '</p>');
template.print('<p>spm_test_name is valid: ' + current.isValidField('spm_test_name')+ '</p>');

 

 

Also, whatever the case, I would recommend using getDisplayValue for email notifications:

 

if (current.server_names) {
    template.print(current.getDisplayValue('server_names'));
} else if (current.spm_test_name) {
    template.print(current.getDisplayValue('spm_test_name'));
}

 

View solution in original post

3 REPLIES 3

Allen Andreas
Administrator
Administrator

Hi,

If these are variables, then you'd want to use current.variables.field_name.

However, you may have to do the GlideRecord within the mail script on the related request item record instead.

Additionally, the list collector will contain a comma separated list of sys_ids, so you may need to loop through those and query the respective referenced table the list points to to grab any specific information from each entry.


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi @Shidhi 

I'm glad you got your issue resolved. If you don't mind, can you let us know what you ended up doing?

I'm curious to know if what you've done differs from what I mentioned above.

It seems like you may have done exactly what I mentioned, granted I didn't go and give you literal code to use (I like to try and encourage people to take an attempt themselves with the new information I provide before I go and write several lines of code).

 

If my reply was in line with what you've done, please consider marking it as well as Accept Solution since you can mark more than one reply with that selection.

 

Especially if the fields in question were variables...thanks!

 

Take care! 😀


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Hi @Shidhi - I'm don't recognize those fields on the sc_task table, but you should confirm that they're valid fields (as @Allen Andreas suggests, they might be variables).

 

You can do that check in a background script:

 

(function() {
    var tableName = 'sc_task';
    var fieldNames = ['server_names', 'spm_test_name'];
    var invalidFields = [];
    
    for (var i = 0; i < fieldNames.length; i++) {
        var gr = new GlideRecord(tableName);
        var fieldName = fieldNames[i];
        
        if (!gr.isValidField(fieldName)) {
            invalidFields.push(fieldName);
        }
    }
    
    if (invalidFields.length > 0) {
        gs.info('Invalid fields on ' + tableName + ': ' + invalidFields.join(', '));
    } else {
        gs.info('All specified fields are valid on ' + tableName);
    }
})();

 

Or, just as easily, in your mail script:

 

template.print('<p>server_names is valid: ' + current.isValidField('server_names')+ '</p>');
template.print('<p>spm_test_name is valid: ' + current.isValidField('spm_test_name')+ '</p>');

 

 

Also, whatever the case, I would recommend using getDisplayValue for email notifications:

 

if (current.server_names) {
    template.print(current.getDisplayValue('server_names'));
} else if (current.spm_test_name) {
    template.print(current.getDisplayValue('spm_test_name'));
}