How can I add an approval button in a form

Juliette1
Tera Contributor

Hi everyone, 

I need to add a button in a form.

the user see the record he can approve or reject.

I am new to servicenow and I am not really sure of how to do it.

 

Thanks for your help

Juliette

1 ACCEPTED SOLUTION

Hi,

got it

Try this code in the UI action of Approve on your custom Table; It should be server side

updateRecord();

function updateRecord(){

var rec = new GlideRecord('sysapproval_approver');

rec.addQuery('sysapproval', current.sys_id).addOrCondition('document_id', current.sys_id);

rec.query();

if(rec.next()){

rec.state = 'approved';

rec.update();

}

}

Regards
Ankur

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

View solution in original post

8 REPLIES 8

Hi,

got it

Try this code in the UI action of Approve on your custom Table; It should be server side

updateRecord();

function updateRecord(){

var rec = new GlideRecord('sysapproval_approver');

rec.addQuery('sysapproval', current.sys_id).addOrCondition('document_id', current.sys_id);

rec.query();

if(rec.next()){

rec.state = 'approved';

rec.update();

}

}

Regards
Ankur

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

Juliette1
Tera Contributor

Hi, sorry for my late answer... 

So I finally wrote this code (client side + server side) but still get an error: 

Uncaught TypeError: Cannot read property 'setAttribute' of null

I don't really know what's wrong with the code.... 

It definitely calls the client side function (I have the pop up question) but stop with the server side function and send me to the UI action form....

Thank you for your help

 

 

Onclick call the demoClientServerInv()

 

function demoClientServerInv()
{
	var answer=confirm("Are you sure you want to approve this invoice?");
	if(answer == true){
		gsftSubmit(null, g_form.getFormElement(), 'sys_demoinvoice');
	}
}
if (typeof window == 'undefined'){
	updateRecord();
}

function updateRecord(){

	//rec.addQuery('sysapproval', current.sys_id).addOrCondition('document_id', current.sys_id);
	rec.addQuery('document_id', current.getUniqueValue());

	rec.query();

	if(rec.next()){

		rec.state = 'approved';

		rec.update();
	}
	action.setRedirectURL(current);
}

 

Juliette1
Tera Contributor

Thank you for your Help!!!

 

My code finally gets to work thanks to you Ankur

And I leaned a lot

@Juliette 

Glad to help.

Regards
Ankur

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