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

Sai Kumar B
Mega Sage
Mega Sage

@jjones4773 

Try the below

(function executeRule(current, previous /*null when async*/) {

    // Add your code here
var gr=new GlideRecord('sys_user');
gr.addNullQuery('photo');
gr.addQuery('sys_id', current.getValue('document'));
gr.query();
if(gr.next())
{
gr.setValue('photo', current.getValue('photo'));
gr.update();
}
    
})(current, previous);

Sorry. That didn't work.

 

Ok. Kept trying and it seemed like it did it once, but then hasn't since.

I believe it should be a while loop, not an if:

 

(function executeRule(current, previous /*null when async*/) {

    // Add your code here
var gr=new GlideRecord('sys_user');
gr.addNullQuery('photo');
gr.addQuery('sys_id', current.getValue('document'));
gr.query();
while (gr.next())
{
gr.setValue('photo', current.getValue('photo'));
gr.update();
}
    
})(current, previous);