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

Viraj Hudlikar
Giga Sage

Hello @bishalsharm 

 

You can check below threads:

Possible Ways for Making an Attachment Mandatory :... - ServiceNow Community

How to make attachment mandatory with Service Catalog Item on the Service Portal - Support and Troub...

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Hi @Viraj Hudlikar ,

 

Thanks it worked well, i guess we should dig deep down more in community too haha.

One more quick question, I have created a business rule to copy the attachment from RITM to the native record and it is working but i am getting one warning or error u can say when request is generating. below is the statement.

 

 Error Illegal access to method copy(string,string,string) in class com.glide.ui.SysAttachment 

 

Business Rule:

After Insert/Update

Table : RITM

 

(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('u_contract', contract.getUniqueValue(), current.getUniqueValue());
	}
})(current, previous);

 

Hello @bishalsharm 

 

Your syntax is wrong it should be 

copy(String sourceTable, String sourceID, String targetTable, String targetID)

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

@bishalsharm also do check in your code contract variable is not defined so it might not work.