Trying to copy the Live_profile 'photo' to the sys_user column 'photo'

jjones4773
Tera Guru

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

1 ACCEPTED SOLUTION

Wayne Richmond
Tera Guru

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:

find_real_file.png

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.

View solution in original post

11 REPLIES 11

Updated and still didn't work.

 

Wayne Richmond
Tera Guru

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:

find_real_file.png

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.

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

 

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.

 

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 😉