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

Ankur Bawiskar
Tera Patron
Tera Patron

@Brian Lancaster 

Did you try to debug by adding alerts what's the outcome?

Did you try to use getXMLAnswer() for ajax?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Tossed in some alerts. It appears to get to this code and then stopes working in SOW. Fulfiller Standard UI it works fine. I get the alert that says before retrieve kb number but the alert that should be the KB number I don't get.

 
alert('before retrieve kb number'); 
// retrieve KB number from URL
var kbNumberSub = commentLink.match(' >(.*) : ')[1];
var kbNumber = trim(kbNumberSub);
alert (kbNumber);

@Brian Lancaster  Try to validate match and trim method output. 

Also check browser console log  , see if any error coming due to the function used inside this client script. 

 

eg:

 

alert('before retrieve kb number ' +  commentLink.match(' >(.*) : ')[1] ); 
// retrieve KB number from URL
var kbNumberSub = commentLink.match(' >(.*) : ')[1];
alert (' KB Number ' + trim(kbNumberSub) );
var kbNumber = trim(kbNumberSub);

 

Thanks,

Harsh

I get the before retrieve KB number. But not the other alert you suggested. These appear in the console log right after the alert before retrieve KB Number.

BrianLancaster_0-1738347663754.png