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.

How to populate MailTo Body with request variables

Nathan Okh
Mega Sage

I have a requirement to modify the MailTo script to change the Subject and Body. I used the following to modify the mail script:

 

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {
	var subjectTxt = encodeURI('Re:' + current.number + ' - Accept Access Attestation');
	var bodyTxt = encodeURI('Please provide a business justification for your Access Attestation:   ');
	template.print('<a title="click here" href="mailto:YOURINSTANCEEMAIL@service-now.com?SUBJECT=' + subjectTxt + '&amp;body='+ bodyTxt+ bodyTxt+'">Click here</a>');

	
	}

 

 

However, the subject does not put the correct requested item number, and I have not been able to get the options variables to populate in the body.

I dont mind printing the variables individually rather than as a loop etc. simple is great at this point.

NathanOkh_0-1714413317984.png

 




4 REPLIES 4

Saloni Suthar
Giga Sage
Giga Sage

To set the subject correctly, you need to use email.setSubject:
email.setSubject(subjecttxt);


If my response helped you, please click on "Accept as solution" and mark it as helpful.
- Saloni

James Chun
Kilo Patron

Hi @Nathan Okh,

 

Can you confirm if your notification is created on the [sc_req_item] table? 

I have copied your code and the subject looks fine to me.

 

In regards to getting all the variables, try the following script.

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
    var subjectTxt = encodeURI('Re:' + current.number + ' - Accept Access Attestation');
    var bodyTxt = 'Please provide a business justification for your Access Attestation:   \n';

    var variables = current.variables.getElements();
    var vquest;

    for (var key = 0; key < variables.length; key++) {
        vquest = variables[key].getQuestion();
        bodyTxt += vquest.getLabel() + " : " + vquest.getDisplayValue() + "\n";
    }

    template.print('<a title="click here" href="mailto:YOURINSTANCEEMAIL@service-now.com?SUBJECT=' + subjectTxt + '&amp;body=' + encodeURI(bodyTxt) + '">Click here</a>');


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

 

JamesChun_0-1714424051253.png

 

Cheers

 

 

KhushbooR
Tera Contributor

Hello @James Chun @Nathan Okh 

How to call the email script on email template?

Thank you

Khushboo

I am pretty in the same way.

Navigate to System Notification > Email > Notification Email Scripts. to Create a email script

Navigate to > Email Client Templates, and add it there. Ex. ${mail_script:your_script_name}