How to get variables from a record producer to send an automated email

Jeffrey Siegel
Mega Sage

I am very new to the Service Now administration.

im trying to send an email to one of my vendors when certain values are selected on my record producer form.

then i want to take other values from my record producer and send it in a formatted email to the vendor.  without them getting all the information.  i would say 90% of my variables in my record producer go into the description field in the resulting ticket, but the vendor I have doesn't need all of that, which is what is making this endeavor harder for me.  

is anyone able to help me get started on how to do this,  ive seen a few questions like this on the forums, but they are way ahead of where i am at.  I pick up on these things quickly, so any guidance getting started would be highly appreciated. 

 

1 ACCEPTED SOLUTION

Jeffrey Siegel
Mega Sage

I was able to get this to work with help from this post: https://community.servicenow.com/community?id=community_question&sys_id=fcff3be9db66fbc02be0a851ca961904

Sample of the code that worked for me:

 

In Record Producer:

var EmailData = {"Date" : DateField, "Position" : PositionInfoField, "ReportedBy" : ReportedByField, "Description" : DescriptionField, "Subj1" : Subject1Field, "Subj2" : Subject2Field};

var parser = new JSON();
var notificationContent = parser.encode(EmailData);

gs.log('JSON object:' + notificationContent);
gs.eventQueue('EmailEvent',current , notificationContent , '');

in Email Script:

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

          // Add your code here

var jsonObject=event.parm1;
	
	// Parsing JSON object:
	
	var obj = JSON.parse(jsonObject);
	
	
	template.print(obj.Date+"<br>");
	template.print(obj.Position+"<br>");
	template.print(obj.ReportedBy+"<br>");
	template.print(obj.Description+"<br>");

	// Set Subject in resulting email
	email.subject =obj.Subj1+" - "+ obj.Subj2;
	

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

View solution in original post

25 REPLIES 25

AnirudhKumar
Mega Sage
Mega Sage

Correct me if I'm wrong, the problem here is parsing all that data in the description, correct?

I think you could send a notification from the Script field on the record producer itself.

 

Here are the ingredients you need for this broth.

producer.fieldName(to get the data that you want to send in your notification)

gs.eventQueue(for triggering the notification) 

So this is what your Script field on record producer looks like:

var data1 = producer.fieldName1;
var data2 = producer.fieldName2;

var notificationContent = {};
notificationContent.detail1 = data1;
notificationContent.detail2 = data2;

gs.eventQueue(current, 'yourEventName' , notificationContent , '');

 

You email Script should look like:

var magicData = event.parm1;

var detail1 = magicData.detail1;
var detail2 = magicData.detail2;

template.print(detail1);
template.print(detail2);

this is the syntax im using.  its not giving me any results, just undefined for each template.print i setup.

ok, then the JSON must have become string along the way. Let's convert it back to JSON.

var magicData = event.parm1.toString();
var magicDataJSON = JSON.parse(magicData);

var detail1 = magicDataJSON.detail1;
var detail2 = magicDataJSON.detail2;

template.print(detail1);
template.print(detail2);