How to show error message when no data is found through Get method

jobin1
Tera Expert

Hi All

 

I am getting a specific domain active P1 and p2 tickets when the end user querying a particular url through GET rest API

now the problem is like when there is no data I mean no p1 and p2 ticket for that domain it should show an message like "No data found"

I tried the last "else" condition in the below script but its not helping  and we are not using any initial params in url(direct hit and shows the result)

 

Can some one help me to achieve this(showing "No data" when there is no active p1 and p1 tickets)

 

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var arr = [];
var result = {};
var compsid;

var company = new GlideRecord('core_company');

company.addQuery('name', 'IN', 'Internal Tools');
company.query();
if (company.next()) {
compsid = company.sys_id;
gs.log("testingjobinsys "+ compsid);
var gr = new GlideRecord("sc_req_item");
gr.addEncodedQuery('active=true^priority=1^ORpriority=2^company=' + compsid);
gr.query();
while (gr.next()) {
//gs.log("testingjobin6 "+ gr.number);

var gr2 = new GlideRecord("sc_req_item");
gr2.addQuery('number', gr.number);
gr2.query();
if (gr2.next()) {
arr.push({
"Number": gr2.number,
"Short Description": gr2.short_description,
"Description": gr2.description,
"Assignment Group": gr2.assignment_group.name,
"Assigned to": gr2.assigned_to.name,
"Category": gr2.u_category.getDisplayValue(),
"Type": gr2.u_sub_category.getDisplayValue(),
"Item": gr2.u_subcategory2.getDisplayValue(),
"Requested For": gr2.request.requested_for.name,
"State": gr2.state.getDisplayValue(),
"Urgency": gr2.urgency.getDisplayValue(),
"Created On": gr2.sys_created_on,
"Created By": gr2.sys_created_by,
"Last Updated By": gr2.sys_updated_by,
"Last Updated On": gr2.sys_updated_on,
"Resolved Date": gr2.u_resolved_date,
"Resolution Category": gr2.u_resolution_category.getDisplayValue(),
"Close Notes": gr2.close_notes,
"Opened": gr2.opened_at,
"Opened by": gr2.opened_by.name,
"Priority": gr2.priority.getDisplayValue(),
"Reassignment count": gr2.reassignment_count,
"Request": gr2.request.number,
"Requestor": gr2.u_requestor.name,
"Company": gr2.company.name,
"Active": gr2.active.getDisplayValue(),
"Sys_id": gr2.sys_id,
"Reported date": gr2.u_reported_date.getDisplayValue()
});

}
result.Data = arr;

}}

else
{
result.Result = "No Data Found";

}

response.setBody(result);
}


)(request, response);

find_real_file.png

 

 

 

@Ankur Bawiskar Any idea?

1 ACCEPTED SOLUTION

Hello jobin,

Can you please try this code?

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
        var arr = [];
        var result = {};
        var compsid;
		var no_data = true;

        var company = new GlideRecord('core_company');

        company.addQuery('name', 'IN', 'Internal Tools');
        company.query();
        if (company.next()) {
            compsid = company.sys_id;
            gs.log("testingjobinsys " + compsid);
            var gr = new GlideRecord("sc_req_item");
            gr.addEncodedQuery('active=true^priority=1^ORpriority=2^company=' + compsid);
            gr.query();
            while (gr.next()) {
                //gs.log("testingjobin6 "+ gr.number);

                var gr2 = new GlideRecord("sc_req_item");
                gr2.addQuery('number', gr.number);
                gr2.query();
                if (gr2.next()) {
					no_data = false;
                    arr.push({
                        "Number": gr2.number,
                        "Short Description": gr2.short_description,
                        "Description": gr2.description,
                        "Assignment Group": gr2.assignment_group.name,
                        "Assigned to": gr2.assigned_to.name,
                        "Category": gr2.u_category.getDisplayValue(),
                        "Type": gr2.u_sub_category.getDisplayValue(),
                        "Item": gr2.u_subcategory2.getDisplayValue(),
                        "Requested For": gr2.request.requested_for.name,
                        "State": gr2.state.getDisplayValue(),
                        "Urgency": gr2.urgency.getDisplayValue(),
                        "Created On": gr2.sys_created_on,
                        "Created By": gr2.sys_created_by,
                        "Last Updated By": gr2.sys_updated_by,
                        "Last Updated On": gr2.sys_updated_on,
                        "Resolved Date": gr2.u_resolved_date,
                        "Resolution Category": gr2.u_resolution_category.getDisplayValue(),
                        "Close Notes": gr2.close_notes,
                        "Opened": gr2.opened_at,
                        "Opened by": gr2.opened_by.name,
                        "Priority": gr2.priority.getDisplayValue(),
                        "Reassignment count": gr2.reassignment_count,
                        "Request": gr2.request.number,
                        "Requestor": gr2.u_requestor.name,
                        "Company": gr2.company.name,
                        "Active": gr2.active.getDisplayValue(),
                        "Sys_id": gr2.sys_id,
                        "Reported date": gr2.u_reported_date.getDisplayValue()
                    });

                }
                result.Data = arr;

            }
			if(no_data)
				result.Result = "No Data Found";
        } else {
            result.Result = "No Data Found";

        }

        response.setBody(result);
    }


)(request, response);

 

I added a variable on top called "no_data" that is set, by default, as true.
If the variable is true, the result.Result will be set to "No da found".

If your code is pushing some values into the variable arr, then the "no_data" variable is set to false and the result.Result is not set.

Please mark my answer as correct and helpful if it is relevant for you!

Thanks,

Filipe Cruz

View solution in original post

4 REPLIES 4

Filipe Cruz
Kilo Sage
Kilo Sage

Hello jobin,

 

The statement "result.Result = "No Data Found";" is executed in an else statement that is executed when there is no company query does not retrieve data.
Neverthess, if the company retrieves data but there are no P1 or P2, there is no statement to set the result.Result = "No Data Found";

One possibility is to check if the result.data has no value, set the result.Result to "No data found":

if(!result.data)
   result.Result = "No Data Found";

 

Here is your full code with this change:

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
        var arr = [];
        var result = {};
        var compsid;

        var company = new GlideRecord('core_company');

        company.addQuery('name', 'IN', 'Internal Tools');
        company.query();
        if (company.next()) {
            compsid = company.sys_id;
            gs.log("testingjobinsys " + compsid);
            var gr = new GlideRecord("sc_req_item");
            gr.addEncodedQuery('active=true^priority=1^ORpriority=2^company=' + compsid);
            gr.query();
            while (gr.next()) {
                //gs.log("testingjobin6 "+ gr.number);

                var gr2 = new GlideRecord("sc_req_item");
                gr2.addQuery('number', gr.number);
                gr2.query();
                if (gr2.next()) {
                    arr.push({
                        "Number": gr2.number,
                        "Short Description": gr2.short_description,
                        "Description": gr2.description,
                        "Assignment Group": gr2.assignment_group.name,
                        "Assigned to": gr2.assigned_to.name,
                        "Category": gr2.u_category.getDisplayValue(),
                        "Type": gr2.u_sub_category.getDisplayValue(),
                        "Item": gr2.u_subcategory2.getDisplayValue(),
                        "Requested For": gr2.request.requested_for.name,
                        "State": gr2.state.getDisplayValue(),
                        "Urgency": gr2.urgency.getDisplayValue(),
                        "Created On": gr2.sys_created_on,
                        "Created By": gr2.sys_created_by,
                        "Last Updated By": gr2.sys_updated_by,
                        "Last Updated On": gr2.sys_updated_on,
                        "Resolved Date": gr2.u_resolved_date,
                        "Resolution Category": gr2.u_resolution_category.getDisplayValue(),
                        "Close Notes": gr2.close_notes,
                        "Opened": gr2.opened_at,
                        "Opened by": gr2.opened_by.name,
                        "Priority": gr2.priority.getDisplayValue(),
                        "Reassignment count": gr2.reassignment_count,
                        "Request": gr2.request.number,
                        "Requestor": gr2.u_requestor.name,
                        "Company": gr2.company.name,
                        "Active": gr2.active.getDisplayValue(),
                        "Sys_id": gr2.sys_id,
                        "Reported date": gr2.u_reported_date.getDisplayValue()
                    });

                }
                result.Data = arr;

            }
			if(!result.data)
				result.Result = "No Data Found";
        } else {
            result.Result = "No Data Found";

        }

        response.setBody(result);
    }


)(request, response);

 

Please mark my answer as correct and helpful if this solves your issue or if you consider it relevant.

Best Regards,

Filipe Cruz

@Filipe Cruz 

You're code is working when there is no data but now that message is also coming with the data as well.

 

find_real_file.png

 

 

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var arr = [];
var result = {};
var compsid;

var company = new GlideRecord('core_company');

company.addQuery('name', 'IN', 'Internal Tools');
//company.addQuery('name', 'IN', 'Qantas');
//company.addQuery('name', 'IN', 'Accor');
company.query();
if (company.next()) {
compsid = company.sys_id;
gs.log("testingjobinsys "+ compsid);
var gr = new GlideRecord("sc_req_item");
gr.addEncodedQuery('active=true^priority=1^ORpriority=2^company=' + compsid);
gr.query();
while (gr.next()) {
//gs.log("testingjobin6 "+ gr.number);

var gr2 = new GlideRecord("sc_req_item");
gr2.addQuery('number', gr.number);
gr2.query();
if (gr2.next()) {
arr.push({
"Number": gr2.number,
"Short Description": gr2.short_description,
"Description": gr2.description,
"Assignment Group": gr2.assignment_group.name,
"Assigned to": gr2.assigned_to.name,
"Category": gr2.u_category.getDisplayValue(),
"Type": gr2.u_sub_category.getDisplayValue(),
"Item": gr2.u_subcategory2.getDisplayValue(),
"Requested For": gr2.request.requested_for.name,
"State": gr2.state.getDisplayValue(),
"Urgency": gr2.urgency.getDisplayValue(),
"Created On": gr2.sys_created_on,
"Created By": gr2.sys_created_by,
"Last Updated By": gr2.sys_updated_by,
"Last Updated On": gr2.sys_updated_on,
"Resolved Date": gr2.u_resolved_date,
"Resolution Category": gr2.u_resolution_category.getDisplayValue(),
"Close Notes": gr2.close_notes,
"Opened": gr2.opened_at,
"Opened by": gr2.opened_by.name,
"Priority": gr2.priority.getDisplayValue(),
"Reassignment count": gr2.reassignment_count,
"Request": gr2.request.number,
"Requestor": gr2.u_requestor.name,
"Company": gr2.company.name,
"Active": gr2.active.getDisplayValue(),
"Sys_id": gr2.sys_id,
"Reported date": gr2.u_reported_date.getDisplayValue()
});

}
result.Data = arr;

}}
if(!result.data){
result.Result = "No Data Found";
//result.Message = "No records found for the given qualification, please check and try again.";
}

else {
result.Result = "No Data Found";

}


response.setBody(result);
}


)(request, response);

Hello jobin,

Can you please try this code?

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
        var arr = [];
        var result = {};
        var compsid;
		var no_data = true;

        var company = new GlideRecord('core_company');

        company.addQuery('name', 'IN', 'Internal Tools');
        company.query();
        if (company.next()) {
            compsid = company.sys_id;
            gs.log("testingjobinsys " + compsid);
            var gr = new GlideRecord("sc_req_item");
            gr.addEncodedQuery('active=true^priority=1^ORpriority=2^company=' + compsid);
            gr.query();
            while (gr.next()) {
                //gs.log("testingjobin6 "+ gr.number);

                var gr2 = new GlideRecord("sc_req_item");
                gr2.addQuery('number', gr.number);
                gr2.query();
                if (gr2.next()) {
					no_data = false;
                    arr.push({
                        "Number": gr2.number,
                        "Short Description": gr2.short_description,
                        "Description": gr2.description,
                        "Assignment Group": gr2.assignment_group.name,
                        "Assigned to": gr2.assigned_to.name,
                        "Category": gr2.u_category.getDisplayValue(),
                        "Type": gr2.u_sub_category.getDisplayValue(),
                        "Item": gr2.u_subcategory2.getDisplayValue(),
                        "Requested For": gr2.request.requested_for.name,
                        "State": gr2.state.getDisplayValue(),
                        "Urgency": gr2.urgency.getDisplayValue(),
                        "Created On": gr2.sys_created_on,
                        "Created By": gr2.sys_created_by,
                        "Last Updated By": gr2.sys_updated_by,
                        "Last Updated On": gr2.sys_updated_on,
                        "Resolved Date": gr2.u_resolved_date,
                        "Resolution Category": gr2.u_resolution_category.getDisplayValue(),
                        "Close Notes": gr2.close_notes,
                        "Opened": gr2.opened_at,
                        "Opened by": gr2.opened_by.name,
                        "Priority": gr2.priority.getDisplayValue(),
                        "Reassignment count": gr2.reassignment_count,
                        "Request": gr2.request.number,
                        "Requestor": gr2.u_requestor.name,
                        "Company": gr2.company.name,
                        "Active": gr2.active.getDisplayValue(),
                        "Sys_id": gr2.sys_id,
                        "Reported date": gr2.u_reported_date.getDisplayValue()
                    });

                }
                result.Data = arr;

            }
			if(no_data)
				result.Result = "No Data Found";
        } else {
            result.Result = "No Data Found";

        }

        response.setBody(result);
    }


)(request, response);

 

I added a variable on top called "no_data" that is set, by default, as true.
If the variable is true, the result.Result will be set to "No da found".

If your code is pushing some values into the variable arr, then the "no_data" variable is set to false and the result.Result is not set.

Please mark my answer as correct and helpful if it is relevant for you!

Thanks,

Filipe Cruz

thanks @Filipe Cruz  its working fine.