Copy fields to other fields

Hello,

I hope you can help me find a better solution for my code.
I have two arrays with fields in them.
In the first one there are only Plumsail fields and in the second one there are only Sharepoint fields.
I want to save the content of the fields from the Plumsail array list to the other array list. But only if the field aren´t empty. Otherwise the corresponding field which is empty should be skipped.
Here is my code so far:

var Text = ['Text1','Text2','Text3','Text4','Text5','Text6','Text7','Text8','Text9','Text10'];
function changedata(){
var newdata = ['Name_x0020_change', 'Street_x0020_change', 'House_x0020_number_x0020_change', 'City_x0020_change', 'Postal_x0020_code_x0020_change', 'Postal_x0020_code_x0020__x0028_P0', 'PO_x0020_box_x0020_change', 'Tax_x0020_number_x0020_change', 'Ust_x002d_Id_x002e__x0020_Nr_x000', 'E_x002d_mail_x0020_change'];
for(var i=0; i < newdata.length; i++){
if(fd.field(Text[i]).value != null ){
fd.field(newdata[i]).value= fd.field(Text[i]).value;
}
}

}
for(var i=0; i < Text.length; i++){
    if(fd.field(Text[i]).value != null ){
        fd.field(Text[i]).$on('change',changedata);
    }
}

Hello @Sternchen,

You can try out this code:

var Text = ['Text1', 'Text2', 'Text3', 'Text4', 'Text5', 'Text6', 'Text7', 'Text8', 'Text9', 'Text10'];
var newdata = ['Name_x0020_change', 'Street_x0020_change', 'House_x0020_number_x0020_change', 'City_x0020_change', 'Postal_x0020_code_x0020_change', 'Postal_x0020_code_x0020__x0028_P0', 'PO_x0020_box_x0020_change', 'Tax_x0020_number_x0020_change', 'Ust_x002d_Id_x002e__x0020_Nr_x000', 'E_x002d_mail_x0020_change'];

for (var i = 0; i < Text.length; i++) {
    fd.field(Text[i]).$on('change', changedata);
}

function changedata() {
    for (var i = 0; i < Text.length; i++) {
        if (fd.field(Text[i]).value) {
            fd.field(newdata[i]).value = fd.field(Text[i]).value;
        }
    }
}

But you can also save data from common field to SharePoint fields within the spBeforeSave() event like this:

fd.spBeforeSave() {
fd.field('SPField').value = fd.field('CommonField').value
});

This worked thanks :slight_smile:

1 Like