- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2022 10:44 AM
I found this script that copies any uploaded photo in the sys_user table to Live_profile table 'photo' column. I would like to do the reverse.
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var livetable=new GlideRecord('live_profile');
livetable.addQuery('document',current.sys_id);
livetable.query();
while(livetable.next())
{
livetable.photo=current.photo;
livetable.update();
}
})(current, previous);
Here is my current script performed on the Live_profile table.
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var photo=current.photo.getDisplayValue();
//gs.addInfoMessage(photo);
var gr=new GlideRecord('sys_user');
gr.addNullQuery('photo');
//gr.addQuery('photo', current.sys_user.photo);
gr.query();
if(gr.next())
{
// gs.addInfoMessage(gr.photo);
gr.photo.setValue(photo);
gr.update();
}
})(current, previous);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-04-2022 05:59 AM
Hiya. Run this script in Scripts - Background:
var profileUser = new GlideRecord('live_profile');
profileUser.addNotNullQuery('photo');
profileUser.addQuery('type','user');
profileUser.query();
while(profileUser.next()) {
var userUser = new GlideRecord('sys_user');
userUser.addQuery('sys_id',profileUser.document); // match the users
userUser.query();
if(userUser.next()){
userUser.setValue('photo',profileUser.photo);
userUser.update();
}
}
I've tested it in my dev instance and it works fine:
Some users appear to have photos but the photo field is empty. I'm not sure why this is but those photos won't get copied across.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-04-2022 05:05 AM
Updated and still didn't work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-04-2022 05:59 AM
Hiya. Run this script in Scripts - Background:
var profileUser = new GlideRecord('live_profile');
profileUser.addNotNullQuery('photo');
profileUser.addQuery('type','user');
profileUser.query();
while(profileUser.next()) {
var userUser = new GlideRecord('sys_user');
userUser.addQuery('sys_id',profileUser.document); // match the users
userUser.query();
if(userUser.next()){
userUser.setValue('photo',profileUser.photo);
userUser.update();
}
}
I've tested it in my dev instance and it works fine:
Some users appear to have photos but the photo field is empty. I'm not sure why this is but those photos won't get copied across.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-04-2022 06:10 AM
Isn't background scripts a one time thing? If so, I would need this to function anytime someone updates a Profile Pic on the Service Portal.
Thanks.
Jason
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-04-2022 06:16 AM
Actually, I added this as a business rule and it works. Thank you so much. The additional query to set the conditions on Live_profile is what made it work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-04-2022 07:04 AM
Ah, yes, I didn't read\understand the requirements. Well done for getting it working!
You could probably optimize the script a little bit like this:
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var userUser = new GlideRecord('sys_user');
userUser.addQuery('sys_id', current.document); // match the users
userUser.query();
if (userUser.next()) {
userUser.setValue('photo', current.photo);
userUser.update();
}
}
})(current, previous);
Because you're already on the live_profile table, you needn't run the first GlideRecord queries. Although, I'm of the opinion; if it works, leave it alone 😉