Search string for a value and return that value to a field if it exists

Carter1
Giga Guru

I have 2 columns (both string fields): u_test & u_return_test. I have values in u_test that are like the following "abc > test123 > dce", "abc > test456 > dce" (there are about 12 different "testXXX" values) The middle part of these string values are the values I'm searching for. I want to write a script that searches for these values, and if they are found, then return them to u_return_test column for that same record. 

To complicate things, the "testXXX" value that I want to return is actually a value that can be found in a reference table (u_reference). Is there a way to dynamically search the string values of u_test for records that are contained in the u_reference table?

I was googling and saw the indexOf() method but wasn't quite sure how to leverage this for my requirements. I was also thinking perhaps there is a way to only return the value between "> " and " >"? I have tried experimenting and have not had any success with this.

I also thought perhaps subString could be leveraged but the string records in u_test all differ in length besides the "testxxx" values.

Any help would be greatly appreciated!

3 REPLIES 3

Joaquin Campos
Mega Sage

Hi Carter,

I hope this high level description could help you.

I'd store all possible test values in an array (you can do it with something like nameArray.push(value)) and then loop into that array checking if the array value is found in the different strings.

To check if string is found that's indeed done with indexOf(), which should return -1 if the string is not found or the position value if the string is found. So whenever it returns a value > -1, then it's found.

Hope it helps!

 

Joaquín

Brad Bowman
Kilo Patron
Kilo Patron

This will be fun.  You'll want an onChange catalog script when test changes, that will look something like this

function onChange(control, oldValue, newValue, isLoading) {
 if (isLoading || newValue == '') {
  return;
 }

 var first = newValue.indexOf('>');
 var last =  newValue.lastIndexOf('>');
 var tst = trim(newValue.substring(first + 1, last));
 alert('Test===' + tst + '==='); //confirm all that works as it should for various values
 var ga = new GlideAjax('ReturnTest') ; //name of Script include
 ga.addParam('sysparm_name','getTest'); //name of function in script include
 ga.addParam('sysparm_test',tst);
 ga.getXML(testcallback);
	
 function testcallback(response){		  
  var answer = response.responseXML.documentElement.getAttribute("answer");
  g_form.setValue('u_return_test', answer);
 }
}

This passes the text between the two ">" to a server script to do the querying.  Create a Script Include with the same name used in the GlideAjax line, ensuring that the Client callable box is checked.  This script will get you started, but you'll have to correct and fill in the gaps per my comments

var ReturnTest = Class.create();
ReturnTest.prototype = Object.extendsObject(AbstractAjaxProcessor, { 
 
 getTest:function(){
  var answer = '';
  var tst = this.getParameter('sysparm_test');
  var gr = new GlideRecord('u_reference'); 
  gr.addQuery('fieldname', tst); //replace with the name of the field that contains 'testXXX' 
  gr.query();
  if(gr.next()){ //change 'if' to 'while' if there could be more than one record on u_reference that matches
   answer = gr.fieldname; //replace with the name of the field on this record that you want to populate on u_return_test
   //is it just one field from u_reference that you want to populate on u_return_test, or is that value found somewhere else, or a combination of fields from u_reference, and elsewhere???
  }
  return answer;
  }, 

type: 'ReturnTest'
});

Hi Brad,

My thoughts exactly.. lol. I appreciate you taking the time to detail this out. 

Unfortunately i do not understand much of this but think I can wade my way through with your comments and google university. I do not have any time to test this at the moment but I will be working on this next week and will post back here with my results and mark your answer as correct if I can get it to work for me.

Thanks again for helping with this!