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

Background Script or not

amacqueen
Mega Guru

I have seen several comments about not running background scripts in a production environment.

With that in mind can anyone give me a sample script to run that would parse an email address and from the results update a field in sys_user called country.

Example, say I have an email address joe@de.domain.com I need to extract the de, capitalise it and then populate the country field with that information.

Thanks

1 ACCEPTED SOLUTION

Allright reason is, since you are running the script on all the records, which also contains email with null value so the script fails after reaching it.



So in the gliderecord query you can include the line..



usr.addEncodedQuery('email!=NULL^emailLIKE@');


View solution in original post

12 REPLIES 12

Chuck Tomasi
Tera Patron

Hi Angus,



Running scripts from scripts background is dangerous because it is direct. Once you hit run, there's no turning back. What's done is done. That's the risk. If you want to develop, test, and promote, use Fix Scripts. They become part of your update set or install package.


https://docs.servicenow.com/bundle/istanbul-application-development/page/build/applications/concept/...



As for parsing, you might find my videos on Regular Expressions helpful. Once you get the "de", you'll need to do a lookup to match "de" with Germany. The rest is basic GlideRecord operations.



Reference:


TechNow Episode List


Developer: GlideRecord


Please don't use background script in Production environment. Use any schedule job for this purpose and also test the code in sub prob instances first.



In order to get the parsing use the below code:



var emailid = "joe@de.domain.com";


var arr = emailid.split("@");


var arr1   = arr[1].split(".");


Var coun = arr1[0].toUpperCase();




var gr   = new GlideRecord(sys_user);


gr.addQuery('sys_id','<sysId of the user record>');


gr.query();


if(gr.next())


{


gr.country = count;


gr.update();


}


A few corrections and optimizations...



var emailAddr = "joe@de.domain.com";


var arr = emailAddr.split("@");


var arr1   = arr[1].split(".");


var count = arr1[0].toUpperCase();



var usr   = new GlideRecord(sys_user);


if (usr.get('email', emailAddr)) {


        usr.country = count;


        usr.update();


}


I knew I could rely on you Chuck (if I had 1% of the knowledge you have, I'd be very happy)



Can you and/or souren007 explain why the variable var emailAddr = "joe@de.domain.com"; is defined with the email address please.