How to get the sys_id of the current form which is not yet submitted in portal?

Kannan Nadar
Tera Guru

Hi Team,

I am submitting a new record to a custom table on the portal, before submit I need to check if there are any attachments added to the form. I am using an onSubmit client script for this but when I try to query the sys_attachment table, I dont get any records. The g_form.getUniqueValue() returns -1.

Thanks,

Kannan

1 ACCEPTED SOLUTION

Hi,

the above onSubmit client script did you try setting Isolate Script as false on the client script?

or else you can have before insert BR on your table; query sys_attachment table with the current.table_sys_id as current sys id and if record not found then show message and stop form submission

Note: g_form.getUniqueValue() won't give the record sys_id as sys_id is not generated on new record; it only generates once you submit; so BR can have the current.sys_id

Also in scoped app you cannot do GlideRecord in client script; also you cannot perform synchronous GlideAjax in onSubmit script

BR Script: you can enhance it to determine if specific file name is also attached or not

(function executeRule(current, previous /*null when async*/) {

	// Add your code here

	var attachmentRec = new GlideRecord('sys_attachment');
	attachmentRec.addQuery('table_sys_id', current.sys_id);
	attachmentRec.addQuery('table_name', <yourTableName>));
        attachmentRec.query();
	if(!attachmentRec.next()){
		gs.addErrorMessage('Attachment required');
		current.setAbortAction(true);
	}


})(current, previous);

OR

BR Script;

if(!current.hasAttachments()){

gs.addErrorMessage('Attachment required');
current.setAbortAction(true);

}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

13 REPLIES 13

Try this code:

Before running this script, Make an Isolate script: False on client script / catalog client script.

function onSubmit() {
//Type appropriate comment here, and begin script below

var attachments = document.getElementById('header_attachment_list_label');
if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none' ) {
alert('Please add attachment document');
return false;
}

}

Thank you,
Abhishek Gardade

Hey Abhishek,

I got the below error even with the isolate script set to false:

TypeError: Cannot read property 'getElementById' of null

Please note that I am working on a scoped app. I even set the property "glide.script.block.client.globals" to false but still not working.

Megha Padale
Giga Guru

Hi Kannan,

 sys_id of -1 is the sys_id of a new record. Once the record is inserted, it will be given a new sys_id.

So before inserting record its sys_id is -1 only.

getUniqueValue() is the method to return sys_id of particular record.

As you want to check any attachments added to form, below link may help you to achieve  this,

https://community.servicenow.com/community?id=community_question&sys_id=f4215be9dbdcdbc01dcaf3231f96...

If my answer helped you in any way, mark answer as helpful and correct.

Thanks and regards,

Megha.

 

Hello Megha,

The link you provided talks about the server side, but in my scenario I am working on the client script of the form.

Thanks.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Is this about record producer on portal? if yes then you can select the box of Mandatory attachment for producer

Sample client script for onSubmit if you require custom form/alert message; ensure UI type is ALL;

function onSubmit(){

if (this.document.getElementsByClassName('get-attachment').length == 0) {
		g_form.addErrorMessage('Attachment is required during submission');
		return false;
	}

}

Regards
Ankur

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