Need to make OOTB attachment icon mandatory based on variable value.

bishalsharm
ServiceNow Employee
ServiceNow Employee

Hi Team,

 

I am struggling to make the OOTB attachment icon mandatory based on variable value. I have tried almost everything available here but nothing is working for me. Right now my current onSubmit() script looks like below:

 

 

function onSubmit() {
    var status = g_form.getValue('trainings_completed');
    if (status == 'Yes') {
        var cart_id = g_form.getValue('sysparm_item_guid');
        alert("cart ID is " + cart_id);
        var gr = new GlideRecord("sys_attachment");
        gr.addQuery("table_name", "sc_cart_item");
        gr.addQuery("table_sys_id", cart_id);
        gr.query();
        if (!gr.next()) {
            alert("You must add an attachment before submitting this request.");
            return false;
        }
    }
}

 

cart_id is showing empty during execution

 

 

unfortunately this is not working. Please help me to get this work or suggest any other alternate way to achieve this.

 

Thanks

Bishal

1 ACCEPTED SOLUTION

Hi @bishalsharm 

Is the BR in Global scope?

Try below code

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

	var con = new GlideRecord('u_contract');
	con.addEncodedQuery('u_parent_ritm='+current.getUniqueValue());
	con.query();
	if(con.next())
	{
		var attach = new GlideSysAttachment();
		var result = attach.copy('sc_req_item', current.getUniqueValue(),'u_contract', contract.sys_id);
	}
})(current, previous);

Check the syntax of GlideSysattachment copy - GlideSysAttachment - copy


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

View solution in original post

8 REPLIES 8

Voona Rohila
Kilo Patron
Kilo Patron

Hi @bishalsharm 

GlideRecord should not be used in client side scripting. It is server side API and if you want server side info then use GlideAjax.

 

In this Particular scenario, You don't need Glide Ajax too you can try the solution from links provided by Viraj.

 

Similar use case- https://www.servicenow.com/community/developer-blog/verify-mandatory-attachments-count-on-catalog-it...

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Hi @Voona Rohila,

 

Option 2 from below community post worked for me

https://www.servicenow.com/community/developer-articles/possible-ways-for-making-an-attachment-manda...

 

but can u take a look on the additional query i posted here. any idea ?

 

Thanks

Bishal

 

 

Hi @bishalsharm 

Is the BR in Global scope?

Try below code

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

	var con = new GlideRecord('u_contract');
	con.addEncodedQuery('u_parent_ritm='+current.getUniqueValue());
	con.query();
	if(con.next())
	{
		var attach = new GlideSysAttachment();
		var result = attach.copy('sc_req_item', current.getUniqueValue(),'u_contract', contract.sys_id);
	}
})(current, previous);

Check the syntax of GlideSysattachment copy - GlideSysAttachment - copy


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Hi @Voona Rohila ,

 

This code is working thank you so much for your dedicated response 🙂

 

Thanks

Bishal