Create notification (email) off of the approval table

Sandy7
Tera Expert

Hi,

I am trying to create an email that gets sent out when a knowledge article is created. This should be built off the sys approval table and go to the approver.

I have the email being fired off using these conditions:

when to send: 

Table = Approval (sys approver_approval)

When source table = Kb_knowledge

Who will receive: approver

 

My problem is, I am trying to customize the subject & body text to pull in the Knowledge Article #

Here is what the approval record looks like:

I am trying a simple script in the subject field:

Docment ID = the name of the "Approving" field (to the right).. i've so tried "number", because that is the # of KB article.

When the email is triggered it only containts "has been submitted for approval" in the subject, it's not passing the KB#.

Can anyone help??
Thanks - the script i'm using is below

${sysapproval.document_id} ${sysapproval} has been submitted for approval

 

find_real_file.png

 

1 ACCEPTED SOLUTION

graham_c
Tera Guru

Hey,

No worries 🙂

Everyone does it differently to be honest. You could have separate notifications to handle the different Publish/Retire scenarios (and build that into the conditions of the notifications) with their own specific wording, or you keep the one notification and pull through the scenario via mail script.

For example I've taken the mail script I provided you earlier and added the knowledge state line into it, so that it can pull through where it is in the knowledge article journey.

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {
	
	//Get the Knowledge Article object
	var article = current.getValue('document_id');
	var grKnowledgeArticle = new GlideRecord("kb_knowledge");
	grKnowledgeArticle.addQuery("sys_id", article);
	grKnowledgeArticle.query();
	if (grKnowledgeArticle.next()) {
		//Set the Subject of the Email to include the KB number
		email.setSubject(grKnowledgeArticle.getValue('number') + ' has been submitted for approval');
		
		//Push details into the body of the notification
		template.print('Knowledge Number: ' + grKnowledgeArticle.getValue('number') + '<br/>');
		template.print('Knowledge Category: ' + grKnowledgeArticle.getDisplayValue('kb_category') + '<br/>');
		template.print('Knowledge State: ' + grKnowledgeArticle.getDisplayValue('workflow_state'));

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

Here is my knowledge article, so it is KB0010001, in the category of "Announcements" and in the workflow state of "Review".

find_real_file.png

This then pulls through to my notification in the body.

find_real_file.png

You can apply logic into your mail script if you wish to display dynamic content based on the data you are pulling in from the knowledge article record.

Example below checks for the workflow state and displays additional text to help explain what this means to the approver.

if(grKnowledgeArticle.getDisplayValue('workflow_state') == 'Review'){
    template.print(' (This approval is for the knowledge article to be published)');
}
else if(grKnowledgeArticle.getDisplayValue('workflow_state') == 'Pending retirement'){
    template.print(' (This approval is for the knowledge article to be retired)');
}

Article is in a state of review

find_real_file.png

Article is in a state of pending retirement

find_real_file.png

Something to consider in your decision making on how you go about developing the solution (i.e. 2 notifications vs 1) is to consider the overhead of maintaining that design going forwards.

For example if you were to go with the 1 notification approach, there is a risk that any future changes you make impacts both scenarios of publish and retire - which doubles test effort for the change - where two notifications allows you that greater control to update one without impacting the other (removing the need to test both scenarios).

Personally I prefer to break things down into modular components. A mail script to get the data from the record and publishes that into the notification. A notification is made for each scenario, with customised text hardcoded into each but the mail script is called in each one. This allows us to easily make a change the mail script if anything impacts the knowledge data (e.g. need to add a new field into each approval email), of if we need to update the wording in the retirement email we can do that without worry of breaking anything. 

TL;DR (Too Long; Didn't Read ^_^ - I have a habit of writing long replies)

There are a few different ways to do this and none of them are 'wrong', but you need to keep in mind how you want to maintain it going forwards too. I think you have the right approach by building a separate notification for each published state so you just need to build this into your conditions of the notifications so you only fire it once, and it'll have the right content.

Thanks

 

 

View solution in original post

9 REPLIES 9

graham_c
Tera Guru

Hi Sandy,

 

I think the issue is that the "Approval For" field (sysapproval) is looking at the Task table and knowledge articles are not extended from task therefore you can't pull through the information that you require.

One solution would be to create a mail script, which you can then pull through the document id's sys_id to do a GlideRecord to the knowledge article table (kb_knowledge), where you can then retrieve the number of the article and return that back into the notification directly.

Mail script documentation can be found here

Thanks

If this has been helpful or has answered your question please mark accordingly.

graham_c
Tera Guru

I had some time to kill 🙂

I created a new mail script called "kb_number" with the following script

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {
	
	// Add your code here
	var article = current.getValue('document_id');
	var grKnowledgeArticle = new GlideRecord("kb_knowledge");
	grKnowledgeArticle.addQuery("sys_id", article);
	grKnowledgeArticle.query();
	if (grKnowledgeArticle.next()) {
		template.print(grKnowledgeArticle.getValue('number'));
		email.setSubject(grKnowledgeArticle.getValue('number') + ' has been submitted for approval');
	}
})(current, template, email, email_action, event);

The script takes the value of the document_id field, then runs a GlideRecord query against the knowledge article table looking for the record listed in the document_id field. If found it then prints into the notification the number of the knowledge article. It also overwrites the subject to the "NUMBER has been submitted for approval".

I then created the notification as you have, but in the subject I left this blank and in the body I called the mail script.

find_real_file.png

This then creates a notification with the knowledge article number in the body and the required subject line.

find_real_file.png

Hope this helps

Thanks

Wow, thank you! This was a huge help!

I really appreciate you sending this my way!!!!!

 

Sandy7
Tera Expert

One more thing, I was also wanting to pull the Category  from the knowledge article into the body as well as the links to approve / reject the approval request. Any idea how this would be done? thanks again!