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

To my earlier post I should have made it clear I need this to run against all users (



I did test with a valid email address on our system however that doesn't work so I modified the script to the below and that also doesn't work:-



var emailAddr = "";


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();


}



Appreciate your help.


Hi,


var emailAddr = "";   simply means assign an empty string to the var, thus   your script wont work.



And if country is a reference field, then you need to update it with its sys_id on the referred table..


If you want to run this against all users, then this is what more like what you need.



var usr = new GlideRecord('sys_user');


usr.query();



while (usr.next()) {


var emailAddr = usr.getValue('email');


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


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


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


        usr.country = count;


        usr.update();


}


Thanks for that Chuck however I get an error in the System Log as below:-



org.mozilla.javascript.EcmaError: Cannot convert null to an object.


Caused by error in <refname> at line 6



3:


4: while (usr.next()) {


5: var emailAddr = usr.getValue('email');


==> 6: var arr = emailAddr.split("@");


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


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


9: usr.country = count;


well, maybe thats because data type conversion/compatibility issue.



You can either change


var emailAddr = usr.email.toString(); or


var emailAddr = usr.getDisplayValue('email').toString();