0

I am using below code with reference to this thread: Yes/No (check box) - who updated field on the custom list item

Basically when a user checks on a yes/no column, it shows/prints the user name who did that in a respective column. It works great and does the job. For example in the above thread, Updated By column shows 'app'.All i need to add is the time and date when user checks a yes/no column along with the user name. So it should look like: Updated by: app 8/15/2018 4:01PM Below is the code, can someone please help me add timestamp config to the below code.:

<script src="//code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
    var fieldValue=$("input[title='YesNoColumn']").is(":checked");
    var updatedby=$("input[title='Updated by']").val();
    $("input[title='Updated by']").attr("readonly","true").css('background-color','#F6F6F6');
    $("input[title='YesNoColumn']").change(function(){
        if($(this).is(":checked")!=fieldValue){
            $("input[title='Updated by']").val(getUserName());
        }else{
            $("input[title='Updated by']").val(updatedby);
        }
    });
});
function getUserName(){
    var username;
    var userid = _spPageContextInfo.userId;
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
    // execute AJAX request
    $.ajax({
        url: requestUri,
        type: "GET",
        async: false,
        headers: { "ACCEPT": "application/json;odata=verbose" },
        success: function (data) {
            username=data.d.Title;
        },
        error: function () {            
        }
    });
    return username;
}
</script>
1

Sample script for your reference.

enter image description here

<script src="//code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        var fieldValue = $("input[title='YesNoColumn']").is(":checked");
        var updatedby = $("input[title='Updated by']").val();
        $("input[title='Updated by']").attr("readonly", "true").css('background-color', '#F6F6F6');
        $("input[title='YesNoColumn']").change(function () {
            if ($(this).is(":checked") != fieldValue) {
                var currentDateTime =new Date();
                var fmtDateTime = currentDateTime.format('MMM/dd/yyyy hh:ss tt');
                $("input[title='Updated by']").val(getUserName()+' '+ fmtDateTime);
            } else {
                $("input[title='Updated by']").val(updatedby);
            }
        });
    });
    function getUserName() {
        var username;
        var userid = _spPageContextInfo.userId;
        var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
        // execute AJAX request
        $.ajax({
            url: requestUri,
            type: "GET",
            async: false,
            headers: { "ACCEPT": "application/json;odata=verbose" },
            success: function (data) {
                username = data.d.Title;
            },
            error: function () {
            }
        });
        return username;
    }
</script>
  • Thanks a lot for the help, excellent. I would recommend changing hh:ss to hh:mm to show hours:minutes not hours:seconds. – mdevm Aug 16 '18 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.