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

Replace GlideRecord query?

DrewW
Mega Sage

Does anyone know if there is a way to replace the current query for a GlideRecord?   There is a condition being added and I do not have access to the code that is adding the condition so what I was hoping to do is just do a search and replace on the encoded query and then set the query to what ever the new encoded query turns out to be.   The issue I have is GlideRecord.addEncodedQuery adds to whats there and I would like to replace whats there.

Anyone have any thoughts?

4 REPLIES 4

Patrick Schult2
Giga Guru

Maybe set your GlideRecord variable to null, then add your query again? Seems kinda strange that you don't have access to the code that's controlling the query in the first place, and you need to know the variable name in order to 'reset' the GlideRecord and start over from scratch.


var gr = new GlideRecord('incident');


gr.addQuery('short_description', 'stuff');


gs.log('Typeof gr: ' + typeof gr);


gs.log('Current query is: ' + gr.getEncodedQuery());



gr = undefined


gr = new GlideRecord('incident');


gr.addQuery('short_description', 'NEW stuff');


gs.log('After undefining gr, this is the query: ' + gr.getEncodedQuery());


gs.log('After undefining gr, this is our type: ' + typeof gr);



The output looks like this:


Typeof gr: object


Current query is: stuff=short_description


After undefining gr, this is the query: short_description=NEW stuff


After undefining gr, this is our type: undefined


I definitely do not want to set it to null because the user may be saving something and thats just going to be even more messy I think.



As for the code access with Fuji and the application scope it is more than possible to have code in the system that you cannot change.



My issue was that the system is adding "company=<users_company_sys_id>" and I would like it to be "company=<users_company_sys_id>^ORcompany=<global_company_sys_id>^ORcompany=".



So I was hopping I could just "edit" the query to "fix" it.   I did find that in the specific case I was looking at the field was dependent on the company field so I just removed the dependance but I would still like to know if anyone has a good way to edit and then replace the query the GlideRecord is doing.


peterh_smith
Tera Contributor

Try gr.initialize().  It modifies the referenced object so the caller will pick up the change.

Igor Kozlov
Tera Expert

You should be able to override a query with "^NQ"

//save query you have
var oldQuery = gr.getEncodedQuery();

//now make sure your old query will return no result
gr.addQuery('sys_id', '_none_');

//to manipulations up to you needs
var newQUery = modifyQuery(oldQuery);

gr.addEncodedQUery('^NQ' + newQUery);

function modifyQuery(query) {
//here you can break it with .split('^') or whatever
//will just drop part of query as example
return query.replace('prioriry=4' , '');
}

This will give you union of 1st query with no result and your modified query