The CreatorCon Call for Content is officially open! Get started here.

Check for Attachments if a field is blank.

turnea
Mega Expert

Hi everyone,

I've seen a lot of script for checking attachments, but I can't seem to track one down that is conditional on whether or not a catalog variable is blank.   I have this portion already:

function onSubmit()

{

    var cat_id = gel('sysparm_item_guid').value;

    var gr = new GlideRecord("sys_attachment");

    gr.addQuery("table_name", "sc_cart_item");

    gr.addQuery("table_sys_id", cat_id);

    gr.query();

    if (!gr.next()) {

          alert("Attach manager and/or site manager's approval email for cart movement");

          return false;

    }

                     

}

I know that I need to add something to this that checks one variable and if it returns "null" to make it mandatory for attachments.   Any help would be appreciated.

Thank you,

Amandah

1 ACCEPTED SOLUTION

acretion
Tera Expert

Amandah,



If i'm understanding your question, you'll probably just need to add an additional if condition at the top of the script, something like...(i haven't tested this, fair warning)



function onSubmit()


{


          if (g_form.getValue('FIELD_NAME') == '')   { //replace with whatever your catalog variable name is


                var cat_id = gel('sysparm_item_guid').value;


                var gr = new GlideRecord("sys_attachment");


                gr.addQuery("table_name", "sc_cart_item");


                gr.addQuery("table_sys_id", cat_id);


                gr.query();


                if (!gr.next()) {


                      alert("Attach manager and/or site manager's approval email for cart movement");


                      return false;


                }


      }


}


View solution in original post

5 REPLIES 5

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Amandah,




Please check section 8.16 for more info.


http://wiki.servicenow.com/index.php?title=GlideRecord#hasAttachments


Hi Pradeep,



Thanks for the link, but I'm already aware of how to check the record for attachments.   What I need is to find out how to add a condition to it to only alert to add an attachment if one of the catalog variables is blank.   That code doesn't look to do any of that.



Thank you,


Amandah


acretion
Tera Expert

Amandah,



If i'm understanding your question, you'll probably just need to add an additional if condition at the top of the script, something like...(i haven't tested this, fair warning)



function onSubmit()


{


          if (g_form.getValue('FIELD_NAME') == '')   { //replace with whatever your catalog variable name is


                var cat_id = gel('sysparm_item_guid').value;


                var gr = new GlideRecord("sys_attachment");


                gr.addQuery("table_name", "sc_cart_item");


                gr.addQuery("table_sys_id", cat_id);


                gr.query();


                if (!gr.next()) {


                      alert("Attach manager and/or site manager's approval email for cart movement");


                      return false;


                }


      }


}


Thank you!   This is what I was looking for.   I'll try it out.