- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2017 06:30 AM
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
Solved! Go to Solution.
- Labels:
-
Instance Configuration
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-21-2017 02:21 AM
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@');

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2017 06:36 AM
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.
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:
Developer: GlideRecord
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2017 06:44 AM
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();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2017 06:48 AM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2017 07:15 AM
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.