Script Include for reference qualifier

Nataliia_Lova
Tera Guru

Hi there, 

I need restrict options on Location Dropdown to patient addresses. Required conditions are:

  • Where “Type” = Health Location or Health Depot, OR

  • Where the linked Consumer on the location is the same as the Consumer on the Patient associated with a WOT

I wrote the Reference Qualifier in Dictionary Entry Override: 

cmn_location_type=health_location^ORcmn_location_type=health_depot^ORconsumer="+current.work_order.x_table_patient_name.consumer 

However, only the first condition works (Type = Health Location or Health Depot).
The condition with consumer="+current.work_order.x_table_patient_name.consumer does not seem to be working.

My manager suggested me to use this Script Include methods for the reason: 

getLocationAndTerritoryHospitalIDsForConsumerFromGr: function(gr) {
        var consumer_sysid = '';
        var table = gr.getTableName();
        if (table == 'sn_hcls_appointment') {
            consumer_sysid = gr.patient.consumer;
        }
        if (table == 'wm_task') {
            consumer_sysid = gr.work_order.u_appointment.patient.consumer;
        }
        return this.getLocationIDsForConsumer(consumer_sysid).join('^NQ');
    },

    getLocationIDsForConsumerFromGr: function(gr) {
        var consumer_sysid = '';
        var table = gr.getTableName();
        if (table == 'sn_hcls_appointment') {
            consumer_sysid = gr.patient.consumer;
        }
        if (table == 'wm_task') {
            consumer_sysid = gr.work_order.u_appointment.patient.consumer;
        }
        return this.getLocationIDsForConsumer(consumer_sysid)[0];
    },

    getLocationIDsForConsumer: function(consumer_sysid) {
        /* returns location sysids in array */
        var consumerSysID = '';
        if (consumer_sysid) {
            consumerSysID = consumer_sysid;
        } else {
            consumerSysID = this.getParameter('sysparm_consumer_sysid');
        } // end if else make client callable as well
        var locationIDs = [];
        var territoryIDs = [];
        var state = '';
        var tableName = 'cmn_location';
        var encQry = 'consumer.sys_id=' + consumerSysID;
        var gr = new GlideRecord(tableName);
        gr.addEncodedQuery(encQry);
        gr.query();
        var getRowCount = gr.getRowCount();
        while (gr.next()) {
            var locationSysID = gr.sys_id.toString();
            var locationName = gr.name;
            locationIDs.push(locationSysID);
            if (gr.u_territory) territoryIDs.push(gr.u_territory.toString());
            state = gr.state.toString();
            gs.info('locationName ' + locationName + '\n' + 'locationSysID ' + locationSysID);
        }
        var locationSYSIDs = 'sys_idIN' + locationIDs.toString();
        var territoryHospitalSYSIDs = 'cmn_location_typeINhealth_depot,health_location^ORDERBYconsumer';
        /*if (territoryIDs.length > 0) { // add a territory filter
            territoryHospitalSYSIDs += '^u_territoryIN' + territoryIDs.toString();
        } else { //else filter by state
            if (state) territoryHospitalSYSIDs += '^state=' + state;
        }*/
        return [locationSYSIDs, territoryHospitalSYSIDs];

    },

    getstringback: function(given) {
        var answer = given;

        return answer;
    }

My questions are:

  1. Should this Script Include be client-callable, server-callable?

  2. Since the Script Include returns an array, and a Reference Qualifier must be a string, how can I use these methods in a Reference Qualifier?

Thanks in advance!

5 REPLIES 5

John Gilmore
Giga Guru

You shouldn't have to use a Script Include (SI), your original method could have worked but it looks like there might be an issue in that you may have left out a " in front of 'consumer='. That said, use of "current"  can be inconsistent at best.

If that doesn't work then you can change the reference qualifier to an advanced (scripted) reference qualifier and then use the following script to produce the query string. This will get the value of consumer from the patient associated with the WOT based on your information and if it is valid return the query as you are attempting to write it. If it fails to find a value for patient it will return the query with just the location type filters.

 

(function () {
	var consumerID = '';
	var query = 'cmn_location_type=health_location^ORcmn_location_type=health_depot';
	if (current.work_order && current.work_order.x_table_patient_name) {
		var patientGR = current.work_order.x_table_patient_name.getRefRecord();
		if (patientGR.isValidRecord()) {
			consumerID = patientGR.consumer.toString();
		}
		
		if (consumerID) {
			query += '^ORconsumer=' + consumerID;
		}
	}
	
	return query;
});



Thank you for the suggestion, but I need to use the Script Include that was previously created. That is the reason my manager told me to use it — because this Script Include already exists.
When I use it and test it in the Background Script, it works as expected and returns the sys_ids that I need.
However, in the reference qualifier I still only see health_location and health_depot, but the consumer's locations are missing.
According to the task requirements, all these locations should be selectable in the dropdown.

Hi Nataliia_Lova,
in this line:

var locationSYSIDs = 'sys_idIN' + locationIDs.toString();

instead of 'sys_idIN' shouldn't be 'consumer.location.sys_idIN', like this?

var locationSYSIDs = 'consumer.location.sys_idIN' + locationIDs.toString();


And here

return this.getLocationIDsForConsumer(consumer_sysid).join('^NQ');

the '^NQ' is not an '^OR'?

return this.getLocationIDsForConsumer(consumer_sysid).join('^OR');



Did you try the update I first suggested, you have an unclosed quotation mark in your Reference Qualifier, I believe the correct version would be:

cmn_location_type=health_location^ORcmn_location_type=health_depot^OR"consumer="+current.work_order.x_table_patient_name.consumer