Iterate over a set of fields and copy content to a text box

Hello,

As soon as the "Ad CP" button is clicked, the content that is in the fields should be written to the "Contact Person" field. Subsequently, the fields are to be cleared.
How do I implement this?
Currently it only writes the names from the array into the box.


Here is my code:

function contactPerson(){
var fields = ['First_x0020_name' ,'Last_x0020_name', 'Academic_x0020_title', 'Function', 'Department', 'Language', 'CP_x003a__x0020_Telephone', 'CP_x003a__x0020_Mobile_x0020_pho', 'CP_x003a__x0020_E_x002d_Mail_x00'];
    if(fd.field('Add_x0020_CP').value == true){
        for(var i=0; i < fields.length; i++){
            
        	fd.field('Contact_x0020_person').value = fd.field('Contact_x0020_person').value + fields[i];
        }  
    }
}

I hope you can help me fixing my code.

Best regards
Corinna

Hello @Sternchen,

I updated your code, please try it out:

function contactPerson(){
var fields = ['First_x0020_name' ,'Last_x0020_name', 'Academic_x0020_title', 'Function', 'Department', 'Language', 'CP_x003a__x0020_Telephone', 'CP_x003a__x0020_Mobile_x0020_pho', 'CP_x003a__x0020_E_x002d_Mail_x00'];
    if(fd.field('Add_x0020_CP').value == true){
        for(var i=0; i < fields.length; i++){
            
        	fd.field('Contact_x0020_person').value = fd.field('Contact_x0020_person').value  + ' ' + fd.field(fields[i]).value;
        }  
    }
}

Now it looks like this:

The Language drop-down field is a lookup. Why does it now say object instead of the selected value?
Is it possible not to save undefined values like this and how do I delete the values after putting them into the textfield?
What do you recomand?

function contactPerson(){
var fields = ['First_x0020_name' ,'Last_x0020_name', 'Academic_x0020_title', 'Function', 'Department', 'Language', 'CP_x003a__x0020_Telephone', 'CP_x003a__x0020_Mobile_x0020_pho', 'CP_x003a__x0020_E_x002d_Mail_x00'];
    if(fd.field('Add_x0020_CP').value == true){
        for(var i=0; i < fields.length; i++){
            if ( fd.field(fields[i]).value == 'undefined'){
            
            }else{
        	    fd.field('Contact_x0020_person').value = fd.field('Contact_x0020_person').value  + ' ' + fd.field(fields[i]).value;
            }
        }  
    }
}

Hello @Sternchen,

The Lookup field value is stored as an object. You can add a condition to check that the value is present and check the field type, like so:

function contactPerson(){
var fields = ['First_x0020_name' ,'Last_x0020_name', 'Academic_x0020_title', 'Function', 'Department', 'Language', 'CP_x003a__x0020_Telephone', 'CP_x003a__x0020_Mobile_x0020_pho', 'CP_x003a__x0020_E_x002d_Mail_x00'];
    if(fd.field('Add_x0020_CP').value == true){
        for(var i=0; i < fields.length; i++){
            //the value is present
            if (fd.field(fields[i]).value){
                //check the type of the field value
                if(typeof fd.field(fields[i]).value == 'object') {
                    fd.field('Contact_x0020_person').value = fd.field('Contact_x0020_person').value  + ' ' + fd.field(fields[i]).value.Title;;
                }
                else{
            	    fd.field('Contact_x0020_person').value = fd.field('Contact_x0020_person').value  + ' ' + fd.field(fields[i]).value;
                }
            }
            //clear field value
            fd.field(fields[i]).clear()
        }  
    }
}

This workaround works.
Thank you :slight_smile:

1 Like