The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How can I use a UI_page popup on KB articles to update details of the KB article?

jeremyduffy
Kilo Guru

I have a UI action that creates a button on KB articles. When pressed, it opens a UI page with this: 

<div style="display:flex; justify-content:center">
					<button id="validateButton" style="padding: 10px; background-color: #007bff; color: white; border: none;"
						onclick="validateArticle()">
						Complete Attestation
					</button>
				</div>

				<g:script>
					function validateArticle() {
						var ga = new GlideAjax('UpdateKnowledgeArticle'); // Calls the Script Include
						ga.addParam('sysparm_article_id', g_form.getValue('sys_id'));
						ga.getXMLAnswer(function(response) { // Handles server response
							g_form.addInfoMessage(response);
						});
					}
				</g:script>			

 

In my UI page process script, I have this: 

var UpdateKnowledgeArticle = Class.create();
UpdateKnowledgeArticle.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    validateArticle: function() {
        var articleSysId = g_request.getParameter('sysparm_article_id');  // Get the article ID
        var user = gs.getUserID();  // Get current user ID

        if (!articleSysId) {
            return "Error: No knowledge article selected.";
        }

        var knowledgeArticle = new GlideRecord('kb_knowledge');  // Reference the knowledge base table
        if (knowledgeArticle.get(articleSysId)) {
            knowledgeArticle.u_last_validated_date = new GlideDateTime().getValue(); // Set current date
            knowledgeArticle.u_last_validated_by = user; // Set current user

            knowledgeArticle.update();  // Save changes
            return "Knowledge article successfully updated!";
        } else {
            return "Error: Knowledge article not found.";
        }
    },
    
    type: 'UpdateKnowledgeArticle' // Ensure this matches the GlideAjax call
});

I used CoPilot to build most of this because I don't know the SNOW particulars, but I can see it's making an AJAX call to the backend (presumably the process script)? The problem is that I'm getting a post error whenever I try to push the button so the backend isn't even running:

js_includes_doctype.jsx           
           
POST https://becudev.service-now.com/xmlhttp.do 404 (Not Found)

 

I'm not too concerned about the code here and will take anything that works, but the bottom line is I need to be able to push this button and have it update the two fields tracking attestation status.

 

1 ACCEPTED SOLUTION

jeremyduffy
Kilo Guru

I needed to create a script include and pasted the UI Page process script code above into that. The name of the script include must match the var (I changed it to UpdateAttestation) exactly as well. Then you use an ajax call from client javascript to call it like so: 

 

		function validateArticle() {
			var articleSysId = "${kbRecord.sys_id}";
			var ga = new GlideAjax('UpdateAttestation'); // Calls the Script Include
			ga.addParam('sysparm_name', 'validateArticle'); // Specifies the function inside the Script Include
			ga.addParam('sysparm_article_id', articleSysId); // Sends the article sys_id

			var answer;
			ga.getXML(function(response) { // Handles server response
				console.log(response);
				var answer = response.responseXML.documentElement.getAttribute("answer");
				if (answer) {
					document.getElementById("close_msg").textContent = answer;
					document.getElementById("content_area").style.display = "none";
					document.getElementById("close_button_area").style.display = "block";
				} else {
					// s... bad, yo
				}
			});

 

The working script include is:

var UpdateAttestation = Class.create();
UpdateAttestation.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    validateArticle: function() {
        var articleSysId = this.getParameter('sysparm_article_id');  // Get the article ID
        var user = gs.getUserID();  // Get current user ID

        if (!articleSysId) {
            return "Error: No knowledge article selected.";
        }

        var knowledgeArticle = new GlideRecord('kb_knowledge');  // Reference the knowledge base table
        if (knowledgeArticle.get(articleSysId)) {
            knowledgeArticle.u_last_validated_date = new GlideDateTime().getValue(); // Set current date
            knowledgeArticle.u_last_validated_by = user; // Set current user

            knowledgeArticle.update();  // Save changes
            return 1;
        }
    },

    type: 'UpdateAttestation' // Ensure this matches the GlideAjax call
});

And again, the name of the script include is UpdateAttestation as well.

 

View solution in original post

1 REPLY 1

jeremyduffy
Kilo Guru

I needed to create a script include and pasted the UI Page process script code above into that. The name of the script include must match the var (I changed it to UpdateAttestation) exactly as well. Then you use an ajax call from client javascript to call it like so: 

 

		function validateArticle() {
			var articleSysId = "${kbRecord.sys_id}";
			var ga = new GlideAjax('UpdateAttestation'); // Calls the Script Include
			ga.addParam('sysparm_name', 'validateArticle'); // Specifies the function inside the Script Include
			ga.addParam('sysparm_article_id', articleSysId); // Sends the article sys_id

			var answer;
			ga.getXML(function(response) { // Handles server response
				console.log(response);
				var answer = response.responseXML.documentElement.getAttribute("answer");
				if (answer) {
					document.getElementById("close_msg").textContent = answer;
					document.getElementById("content_area").style.display = "none";
					document.getElementById("close_button_area").style.display = "block";
				} else {
					// s... bad, yo
				}
			});

 

The working script include is:

var UpdateAttestation = Class.create();
UpdateAttestation.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    validateArticle: function() {
        var articleSysId = this.getParameter('sysparm_article_id');  // Get the article ID
        var user = gs.getUserID();  // Get current user ID

        if (!articleSysId) {
            return "Error: No knowledge article selected.";
        }

        var knowledgeArticle = new GlideRecord('kb_knowledge');  // Reference the knowledge base table
        if (knowledgeArticle.get(articleSysId)) {
            knowledgeArticle.u_last_validated_date = new GlideDateTime().getValue(); // Set current date
            knowledgeArticle.u_last_validated_by = user; // Set current user

            knowledgeArticle.update();  // Save changes
            return 1;
        }
    },

    type: 'UpdateAttestation' // Ensure this matches the GlideAjax call
});

And again, the name of the script include is UpdateAttestation as well.