Data Table Issue with current user

Hello,

I have some code to automatically populate the user name of the current user in a Data Table field called "Verified By". See attached code. What has started happening is when a new record is added on top of an existing record, the previous "Verified By" username is replaced with the person adding the new record.

Please see video for an example and focus on the "Verified By" column. Why is this happening?

image

Hello @ParAvion,

Please use the code from the documentation to populate only a new records in the DataTable control.

Find the code here.

@IliaLazarevskii the issue described above (with video) is happening again. The "Verified By" username column value is replaced with the next person's name that's adding the new record to the data table. The names that populate the Verified By column need to be "locked" and intact, and shouldn't change. Help!

Hello @ParAvion,

Please export the form and share it with me. I'll review the code and the control's settings.

Hi @Margo please check your PM...thank you.

@ParAvion,

Please use this code to populate only new lines with user name:

var dt = fd.control('DataTable1');
var username = _spPageContextInfo.userDisplayName;
dt.widget.bind('beforeEdit', function(e) {
    var model = e.model;
    if (model.isNew()) {
        model.set('Column1', username);
    }
});

Also, I suggest updating your code so there is only one spRendered() function and all teh code is inside it. No need to warp each action in it.

@Margo thank you for taking a look at this.

How do I also use the code above to incorporate auto populating today's date into the date column as new records are added? That functionality used to be native but it's not anymore.

Also getting "Unmatched '{'" error with your code:

image

Thank you.

@ParAvion,

The editor shows that the code is incorrect. Most likely, a closing curly brace is missing. Read the error's tooltip to find the error.

And for the date field the code is similar:

var dt = fd.control('DataTable1');
var username = _spPageContextInfo.userDisplayName;
dt.widget.bind('beforeEdit', function(e) {
    var model = e.model;
    if (model.isNew()) {
        model.set('Column1', username);
        //set date column
       model.set('DateColumn', new Date());
    }
});

@Margo I got it to work but now the issue is that when the new record is created in the table, the dropdown in the 'Assessment' column cannot be accessed unless I refresh the browser. Is this a bug?

EDIT: I did a mistake, refresh is working with List or Library - sorry
Hello @ParAvion
what if you add a line of code like:

fd.control("dataTable").refresh()

Regards
Stepan

@StepanS how would I incorporate that into the existing code? I'm new at this...thank you.

@ParAvion ,
Take @Margo code, for example this:

var dt = fd.control('DataTable1');
var username = _spPageContextInfo.userDisplayName;
dt.widget.bind('beforeEdit', function(e) {
    var model = e.model;
    if (model.isNew()) {
        model.set('Column1', username);
        //set date column
       model.set('DateColumn', new Date());
    }
// HERE DO THE REFRESH
dt.refresh()
});

@StepanS thank you for this, unfortunately still having the same issue as noted in the video.

@ParAvion can you share your entire script and what you have in the designer? Assessment is a dropdown field with predefined values in the "Options" or do you load the data from other source?

Thanks :slight_smile:

@StepanS Assessment is a dropdown with predefined values in the Options.

image

@ParAvion,

Please update the code like so:

var dt = fd.control('DataTable1');
var username = _spPageContextInfo.userDisplayName;
var date = new Date().toISOString();
dt.widget.bind('beforeEdit', function(e) {
    var model = e.model;
    if (model.isNew()) {
        model.set('Approver', username);
        //set date column
       model.set('Date', date);
    }
});
1 Like

@Margo that seems to have fixed the issue...thank you!

1 Like