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

Hi Ankur,

No, this is not a record producer. This is a form on the portal. Also I am in a scoped app, document is not accessible in my client script. I even tried adding the system property "glide.script.block.client.globals" but that didn't help as well.

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

Hi Ankur,

It seems like there was no other option other than using a business rule.

Thanks,
Kannan

Swapnil Soni1
Giga Guru

Hi,

You can open any record , and get the sys_id from header menu options ( as below )

find_real_file.png

 

Or you can do like-

function onLoad() {
var incSysid = g_form.getUniqueValue();
alert(incSysid);
}

And for more info please refer this link
https://www.servicenowelite.com/blog/2014/5/19/finding-the-sysid-of-a-record

Please mark correct or helpful if this works.
Thanks
Swapnil