Attach Knowledge Client script SOW

Brian Lancaster
Tera Sage

I have the below client script to update the URL of the article so that it provides the service portal view of the article since most user cannot see it in the standard UI. It does not work in Service Operations Workspace. Is there some other place I would need to use this code so it works in SOW.

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    if (newValue.includes('kb_view.do')) {
        g_form.getReference('caller_id', company);
    }
}

function company(caller){
	if (caller.company == '357bc3411b39d8503d4477761a4bcbaa'){
		
		var comment = g_form.getValue('comments');

        // retrieve KB link between [code] blocks
        var commentLink = comment.substring(
            comment.lastIndexOf('[code]') + 6,
            comment.lastIndexOf('[/code]')
        );

        // retrieve KB number from URL
        var kbNumberSub = commentLink.match(' >(.*) : ')[1];
        var kbNumber = trim(kbNumberSub);
		
		//call script include to verify if self-service kb
        var ga = new GlideAjax('CheckKnowledgeBase');
        ga.addParam('sysparm_name', 'isSelfService');
        ga.addParam('sysparm_number', kbNumber);
        ga.getXML(isSelfServiceParse);
	}
	else{
		alert('This user is not an employee and does not have access to view KAs');
		g_form.clearValue('comments');
	}
}

function isSelfServiceParse(response) { //Verify responce is true and set comments if correct.
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if (answer == "true") {
        var comment = g_form.getValue('comments');
        g_form.setValue("comments", comment.replace("kb_view.do?sys_kb_id", "prism?id=kb_article&sys_id"));
    } else {
        var userGA = new GlideAjax('getUserData');
        userGA.addParam('sysparm_name', 'checkFulfiller');
        userGA.addParam('sysparm_user_sys', g_form.getValue('caller_id'));
        userGA.getXML(checkFulfillerParse);
    }
}

function checkFulfillerParse(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if (answer == "false") {
        g_form.clearValue('comments');
        alert("The KA you tried to attach is for fulfillers only. The caller cannot view this article.");
    }
}

 

1 ACCEPTED SOLUTION

@Brian Lancaster  I just tried to use onChange client script on comment field, if i mention any url it should replace to different url and that has updated the correct result. 

 

Untitled.gif

 

Have you tried to reproduce it on your PDI ? your updated script looks fine to me. 

 

View solution in original post

14 REPLIES 14

@Brian Lancaster  Looks like some other dom script creating this error.

 

Can you try to comment the trim() method line to check the result of ajax ?

 

Also try to disable this script to check if you are getting this console error ? on native UI view you are not seeing the same console error ? 

@Brian Lancaster  trim() should be used like this way. 

 

syntax :

String.prototype.trim()

 

 

eg: 

alert (' KB Number ' + kbNumberSub.trim());

 

@Harsh Vardhan 

So there are absolutely no error when running this in the regular UI. Once I changed the trim code to var kbNumber = kbNumberSub.trim(); this section of code started working in SOW.  However the code for the function was doing anything. So I made another adjustment to the code in the GlideAjext response function. It now looks like this.

function isSelfServiceParse(response) { //Verify responce is true and set comments if correct.
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if (answer == "true") {
        var comment = g_form.getValue('comments');
	var newComment = comment.replace("kb_view.do?sys_kb_id", "prism?id=kb_article&sys_id"); //this is the new code
        g_form.setValue("comments", newComment); //this has been setup to use the new variable.
    } else {
        var userGA = new GlideAjax('getUserData');
        userGA.addParam('sysparm_name', 'checkFulfiller');
        userGA.addParam('sysparm_user_sys', g_form.getValue('caller_id'));
        userGA.getXML(checkFulfillerParse);
    }
}

Now I can see the comments actually being updated (screenshot below) but for some reason it still posts the comments with the kb_view.do URL instead of the portal URL.

BrianLancaster_0-1738359780037.png

 

@Brian Lancaster 

why not return the portal URL from the script include function itself?

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Brian Lancaster  As far as i know, if you are passing any parameter inside trim() which is not correct. at least vanilla JS trim() does not allow it , i have seen on lodash trim we can but that has different naming convention. at least on workspace it is giving an error which is good. 

 

add alert to check the newComment variable value value ? is this onChange client script on comment field ? if not then try to do validation on script include and use getJournalEntry() to access the comment data . 

I just checked on PDI and it is replacing it on activity as well as on comment box. 

 

Thanks,

Harsh