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

AbhishekGardade
Giga Sage

Hello Kannan,

Try checking with this method:  alert(g_form.getValue('sysparm_item_guid'));

What is the requirement?

Why are you not using this Mandatory Attachment Checkbox to make attachment mandatory. 

          image

Please check out my article on this

Possible Ways for Making an Attachment Mandatory : Service Portal/Native UI

Please mark as Correct Answer and Helpful, if applicable.
Thank You!
Abhishek Gardade
ServiceNow MVP 2020

Thank you,
Abhishek Gardade

Hello Abhishek,

I am not using a catalog item to submit this record, it is directly a new form.

Have you tried this method g_form.getValue('sysparm_item_guid')) ?

Thank you,
Abhishek Gardade

Yes, it comes up empty.