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.

Trying to use an encoded query with REST API request

lonesoac01
Giga Guru

Hello all,

I am trying to pass two parameters to a REST API that I have created.  The parameters are:

queryParam.opened_at_start_date and queryParam.opened_at_end_date.  The values are formatted and passed as:

opened_at_start_date=01-01-2022&opened_at_end_date=03-01-2022

query = 'opened_atBETWEENjavascript:gs.dateGenerate(' + "'" + queryParam.opened_at_start_date + "'" + ',' + "'" + '00:00:00' + "'" + ')@javascript:gs.dateGenerate(' + "'" + queryParam.opened_at_end_date + "'" + ',' + "'" + '23:59:59' + "'" + ')';

 

The query keeps coming up with 0 records and I do not know why.

2 REPLIES 2

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

TRy this

query = "opened_atBETWEENjavascript:gs.dateGenerate('"+ queryParam.opened_at_start_date + "00:00:00')@javascript:gs.dateGenerate('"+queryParam.opened_at_end_date + "'23:59:59')";
-Anurag

That did not work.  I am going to post my full code to see if I am missing anything anywhere else.

 

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var queryParam = request.queryParams;
    var gr_problem_response_to_client = {};
    var query = '';
    // Checking if the parameter of prb_number is present.
    // If present, then continue on.  If not, show error code and return from script.
    if (queryParam.opened_at_start_date && queryParam.opened_at_end_date) {
		query = "opened_atBETWEENjavascript:gs.dateGenerate('"+ queryParam.opened_at_start_date + "00:00:00')@javascript:gs.dateGenerate('"+queryParam.opened_at_end_date + "'23:59:59')";
    } else {
        gr_problem_response_to_client.error = 'You are either missing opened_at_start_date opened_at_end_date values.';
        response.setBody(gr_problem_response_to_client);
        return;
    }

    var grProblem = new GlideRecord('problem');
    grProblem.addEncodedQuery(query);
    grProblem.query();
    while (grProblem.next()) {
        gr_problem_response_to_client.sys_id = grProblem.sys_id.toString();
        gr_problem_response_to_client.u_affectedcompany = grProblem.u_affectedcompany;
        gr_problem_response_to_client.cmdb_ci = grProblem.cmdb_ci.getDisplayValue();
        gr_problem_response_to_client.u_category = grProblem.u_category;
        gr_problem_response_to_client.subcategory = grProblem.subcategory;
        gr_problem_response_to_client.u_impacted = grProblem.u_impacted;
        gr_problem_response_to_client.urgency = grProblem.urgency;
        gr_problem_response_to_client.assignment_group = grProblem.assignment_group.getDisplayValue();
        gr_problem_response_to_client.assigned_to = grProblem.assigned_to.getDisplayValue();
        gr_problem_response_to_client.u_reoccurrence = grProblem.u_reoccurence;
        gr_problem_response_to_client.u_problem_statement = grProblem.u_problem_statement;
        gr_problem_response_to_client.description = grProblem.description;
        gr_problem_response_to_client.first_reported_by_task = grProblem.first_reported_by_task;
        gr_problem_response_to_client.business_service = grProblem.business_service;
        gr_problem_response_to_client.u_expected_resolution_date = grProblem.u_expected_resolution_date;
        gr_problem_response_to_client.u_problem_manager = grProblem.u_problem_manager;
        gr_problem_response_to_client.number = grProblem.number;
        gr_problem_response_to_client.opened_at = grProblem.opened_at;
        gr_problem_response_to_client.opened_by = grProblem.opened_by.getDisplayValue();
        gr_problem_response_to_client.state = grProblem.state.getDisplayValue();
        gr_problem_response_to_client.approval = grProblem.approval;
        gr_problem_response_to_client.u_approval_type = grProblem.u_approval_type;
        gr_problem_response_to_client.priority = grProblem.priority;
        gr_problem_response_to_client.u_auto_created = grProblem.u_auto_created;
        gr_problem_response_to_client.u_hpi = grProblem.u_hpi;
        gr_problem_response_to_client.u_major_problem = grProblem.u_major_problem;
        gr_problem_response_to_client.u_oa = grProblem.u_oa;
        gr_problem_response_to_client.u_production_risk = grProblem.u_production_risk;
        gr_problem_response_to_client.knowledge = grProblem.knowledge;
        gr_problem_response_to_client.u_sensitive_non_reportable_security_events = grProblem.u_sensitive_non_reportable_security_events;
        gr_problem_response_to_client.work_notes = grProblem.work_notes;
        gr_problem_response_to_client.u_root_cause_category = grProblem.u_root_cause_category;
        gr_problem_response_to_client.u_problem_caused_by_change = grProblem.u_problem_caused_by_change;
        gr_problem_response_to_client.u_problem_initiated_by_failed_change = grProblem.u_problem_initiated_by_failed_change;
        gr_problem_response_to_client.rfc = grProblem.rfc;
        gr_problem_response_to_client.u_cpir_task = grProblem.u_cpir_task;
        gr_problem_response_to_client.sys_updated_on = grProblem.sys_updated_on;
    }

    if (gr_problem_response_to_client.sys_id) {
        response.setBody(gr_problem_response_to_client);
    } else {
        gr_problem_response_to_client.error = 'No Records Found by searching for: ' + query;
        response.setBody(gr_problem_response_to_client);
    }
	
})(request, response);