Parse the String

maryc
Tera Contributor

Hi,

I have a String in LDAP format and I want to get only the name from it. How do I parse this String with JS ?

CN=Bill\,Hamilton,OU=UNITNAME,OU=Region,OU=Country,DC=subdomain,DC=domain,DC=com

I want to extract the name Bill Hamilton from this entire String

Thanks

1 ACCEPTED SOLUTION

justin_drysdale
Mega Guru

Hi Mary,



String parsing comes up so often in Service Now that I constructed a nifty scraping function.   It isn't the fastest, but it is the coolest!   I think it is easier to understand than using the split method.   Splitting, and then splitting again, and maybe even splitting again on top of that gets messy.



var ldap_path = 'CN=Bill\,Hamilton,OU=UNITNAME,OU=Region,OU=Country,DC=subdomain,DC=domain,DC=com';


var first_name = grabContent(ldap_path, 'CN=', '\,');// Bill


var last_name = grabContent(ldap_path, '\,', ',OU=');// Hamilton




gs.print("frist name: " + first_name + "\nlast_name: " + last_name);




function   grabContent(str, start, end) {  


    var startLen = start.length;  


    var s = str.indexOf(start);  


    var e = str.indexOf(end);  


    var scrape = str.substring(s+startLen, e);  


    return scrape;  


}



To use the grabContent function, pass in the string you want to parse as the first parameter, the start of the parse as the 2nd parameter, and end of the parse as the final parameter.     Please let me know if you have any questions.


View solution in original post

7 REPLIES 7

justin_drysdale
Mega Guru

Hi Mary,



String parsing comes up so often in Service Now that I constructed a nifty scraping function.   It isn't the fastest, but it is the coolest!   I think it is easier to understand than using the split method.   Splitting, and then splitting again, and maybe even splitting again on top of that gets messy.



var ldap_path = 'CN=Bill\,Hamilton,OU=UNITNAME,OU=Region,OU=Country,DC=subdomain,DC=domain,DC=com';


var first_name = grabContent(ldap_path, 'CN=', '\,');// Bill


var last_name = grabContent(ldap_path, '\,', ',OU=');// Hamilton




gs.print("frist name: " + first_name + "\nlast_name: " + last_name);




function   grabContent(str, start, end) {  


    var startLen = start.length;  


    var s = str.indexOf(start);  


    var e = str.indexOf(end);  


    var scrape = str.substring(s+startLen, e);  


    return scrape;  


}



To use the grabContent function, pass in the string you want to parse as the first parameter, the start of the parse as the 2nd parameter, and end of the parse as the final parameter.     Please let me know if you have any questions.


This is awesome, but what if I need the "com" from DC=com.  What would be the "end?"

 

maryc
Tera Contributor

Hi,



I have this script in the Transform Map and the query is always returning 0 records ...If I execute the same query in a background script , it works... Not sure whats going on.



I see this error in the log "setValue called for unknown field 'NULL' in table 'sys_user' during update"



(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {



    var last_name = grabContent(source.u_manager, 'CN=', '\,');


  var first_name = grabContent(source.u_manager, '\,', ',OU=');


  gs.log("first name: " + first_name + "\nlast_name: " + last_name);  



  var gr1 = new GlideRecord('sys_user');


gr1.addQuery('first_name', first_name);


  gr1.addQuery('last_name', last_name);


  gr1.query();


  gs.log("Row Count "+gr1.getRowCount());


  if (gr1.next()) {


            return gr1.sys_id;


  }else


  gs.log("No record");


 


 



})(source, map, log, target);








function   grabContent(str, start, end) {      


            var startLen = start.length;      


            var s = str.indexOf(start);      


            var e = str.indexOf(end);      


            var scrape = str.substring(s+startLen, e);      



            return scrape;      


}