<button> onclick function that will redirect to a specific record

dfsdf
Kilo Expert

Hi,

I have a button and an onclick function defined there. When I click this button I want to redirect to the specific url in the same window. Unfortunately, I get this error even if the function is defined in client script:

Uncaught ReferenceError: redirectToDraft is not defined onclick https://mwtab.service-now.com/kp?id=form_kb&table=kb_knowledge&sys_id=17fffdc3db89ecd0391061bbd3961927:1

. Do you know how to solve this? Thanks in advance.

HTML:

<div class="row center"><button onclick="redirectToDraft()" class="btn btn-warning">Draft
                    <!--<a href="/kp?id=form&table=kb_knowledge&sys_id={{data.articleSysId}}" class="link_style">Draft</a>-->
                    </button></div>

Client Script:

function redirectToDraft(){
		var url = "/kp?id=form&table=kb_knowledge&sys_id=" + $scope.data.articleSysId;
		window.location.href = url;
	}

Server side to get articleSysId

//var articleSysId;
			var article = new GlideRecord('kb_knowledge');
			article.get(input.article);
			
			gs.info('asd' + input.article);
			
			var newRecord = new KBVersioning().checkout(article);
			
			var gr = new GlideRecord('kb_knowledge');
			gr.addQuery('number', article.number + '');
			gr.orderByDesc('version');
			gr.setLimit(1);
			gr.query();
			if(gr.next()) {
				data.articleSysId = gr.sys_id +'';
			}
			gs.info("sys id" + data.articleSysId);
		}
1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

can you try to use ng-click instead of onclick ?

 

eg:

 

<div class="row center"><button ng-click="redirectToDraft()" class="btn btn-warning">Draft
                    <!--<a href="/kp?id=form&table=kb_knowledge&sys_id={{data.articleSysId}}" class="link_style">Draft</a>-->
                    </button></div>

View solution in original post

3 REPLIES 3

Harsh Vardhan
Giga Patron

can you try to use ng-click instead of onclick ?

 

eg:

 

<div class="row center"><button ng-click="redirectToDraft()" class="btn btn-warning">Draft
                    <!--<a href="/kp?id=form&table=kb_knowledge&sys_id={{data.articleSysId}}" class="link_style">Draft</a>-->
                    </button></div>

Harsh Vardhan
Giga Patron

Tested on my personal instance. 

 

HTML

 

<div class="buttondiv">

  <input id="my_button" class="button" type="button" value="create new Project" ng-click="c.myFunction()">

</div>

 

Client script:

 

function ($uibModal, $scope, spUtil) {
	var c = this;

	c.myFunction = function() {
		var url = "/sp?id=form&table=kb_knowledge&sys_id=" + $scope.data.articleSysId;
		window.location.href = url;

	};

}

 

Server side script. for testing i have hardcoded one article sys_id. also in url i have added sp portal for testing. 

 

(function() {
  /* populate the 'data' object */
  /* e.g., data.table = $sp.getValue('table'); */
data.articleSysId='c85cd2519f77230088aebde8132e70c2';  // this is for testing
})();

 

 

 

 

Harsh Vardhan
Giga Patron

Let me know if you need any further help here. 

 

If my answer helped you, kindly mark the answer as correct and close this thread.