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

I started concentrating on this specifically because I need it for my job. I found christopher occhicone information for my project. This is the most important stage, in my opinion, because locating the right materials is essential to the project's future success.

jjones4773
Tera Guru

Just in case anyone needs a full recap on this.

Run on table: Live_profile

When to run:  After,  Insert and Update "checked".

Filter Conditions: Photo changes

Advanced script with script below.

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

    // Add your code here
    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();
    }
}

})(current, previous);