Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Need help for one of the server script

Are Kaveri
Tera Contributor

Hi,

 

 

I have a below requirement 

 

There is one custom table in which 2 fields are primary keys.

Item

Order Number

 

we are receiving data from another system.

for that we have been validating from my Scripted Rest resource as below

ar isExistingRecord = (function(order_no) {
		var grTERD = new GlideRecord('my_custom_table');
		grTERD.addQuery('order_no', order_no);
		grTERD.query();
		if (grTERD.next()) {
			return true;
		} else {
			return false;
		}

 

 

I am not checking Item  and order number, I am only queriying on order number.

 

 

Can someone help me how to query order number and Item in a query.

 

 

2 ACCEPTED SOLUTIONS

SanjivMeher
Mega Patron
Mega Patron

Depending on the field name, you can add another addQuery which will look for a combination of both

ar isExistingRecord = (function(order_no,item) {
		var grTERD = new GlideRecord('my_custom_table');
		grTERD.addQuery('order_no', order_no);
                grTERD.addQuery('item', item);
		grTERD.query();
		if (grTERD.next()) {
			return true;
		} else {
			return false;
		}

Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

Aniket Chavan
Tera Sage
Tera Sage

Hello @Are Kaveri ,

Please give a try to the script below and let me know how it works for you.

var isExistingRecord = function(item, order_no) {
    var grTERD = new GlideRecord('my_custom_table');
    grTERD.addQuery('item', item); // Add query condition for the 'Item' field
    grTERD.addQuery('order_no', order_no); // Add query condition for the 'Order Number' field
    grTERD.query();

    if (grTERD.next()) {
        return true;
    } else {
        return false;
    }
};


Let me know your views on this and Mark
Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket

View solution in original post

4 REPLIES 4

SanjivMeher
Mega Patron
Mega Patron

Depending on the field name, you can add another addQuery which will look for a combination of both

ar isExistingRecord = (function(order_no,item) {
		var grTERD = new GlideRecord('my_custom_table');
		grTERD.addQuery('order_no', order_no);
                grTERD.addQuery('item', item);
		grTERD.query();
		if (grTERD.next()) {
			return true;
		} else {
			return false;
		}

Please mark this response as correct or helpful if it assisted you with your question.

AshishKM
Kilo Patron
Kilo Patron

Hi @Are Kaveri ,

You can add item column in the addQuery() method and pass the value in function(order_no, item) 

grTERD.addQuery('item', item);

-Thanks,

AshishKMishra


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Aniket Chavan
Tera Sage
Tera Sage

Hello @Are Kaveri ,

Please give a try to the script below and let me know how it works for you.

var isExistingRecord = function(item, order_no) {
    var grTERD = new GlideRecord('my_custom_table');
    grTERD.addQuery('item', item); // Add query condition for the 'Item' field
    grTERD.addQuery('order_no', order_no); // Add query condition for the 'Order Number' field
    grTERD.query();

    if (grTERD.next()) {
        return true;
    } else {
        return false;
    }
};


Let me know your views on this and Mark
Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket

Anurag Tripathi
Mega Patron
Mega Patron

if it is a custom table mostly the fierld will start with u_

	grTERD.addQuery('u_order_no', order_no);
-Anurag