Question

How to change null value to empty string in Sharepoint custom script? Here is my code.

$(document).ready(function() { 
    getUserListItems()
});


    function getUserListItems() {  
        var currentUser = _spPageContextInfo.userId;

    $.ajax({  

        async: true,  
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('BUGSList')/items?$Select=Title,Requester_x0020_Name,Department,Title0,Priority,Description,Comments,Issue_x0020_Status,Assigned_x0020_To,Author/ID&$filter=substringof('"+ currentUser +"',Author/ID)&$expand=Author/ID",  
        method: "GET",                                                                  // ?$select=Title,PeopleField/ID&$filter=substringof('"+currentUserId+"',PeopleField/ID)&$expand=PeopleField/ID";
        headers: {  
            "accept": "application/json;odata=verbose",  
            "content-type": "application/json;odata=verbose"    
        },  
        success: function(data) {  
            data = data.d.results;
            $.each(data, function(index, value) {  

    var html = "<tr><td>"+"<a href='#' onclick='View1(" + value.Id + ")' data-target='#viewreport' data-toggle='modal'>"+value.Title+
            "</td>+<td>"+value.Requester_x0020_Name+
            "</td>+<td>"+value.Department+
            "</td>+<td>"+value.Title0+
            "</td>+<td>"+value.Priority+
            "</td>+<td>"+value.Description+
            "</td>+<td>"+value.Comments+
            "</td>+<td>"+value.Issue_x0020_Status+
            "</td>+<td>"+value.Assigned_x0020_To+
            "</td>+<td>"+"<a href='#' class='edit' onclick='Edit("+ value.Id +")' data-target='#editEmployeeModal' data-toggle='modal'><i class='material-icons' data-toggle='tooltip' title='Edit'>&#xE254;</i></a>" +
            "<a href='#' class='delete' onclick='Delete(" + value.Id + ")' ><i class='material-icons' data-toggle='tooltip' title='Delete'>&#xE872;</i></a>"+
            "</td></tr>";


                $('#TABLE_ITEMStr').append(html); 
            });



           $('#TABLE_ITEMS').dataTable();
        },  
        error: function(error) {  
            console.log(JSON.stringify(error));    
        }   
    })  
}
Was it helpful?

Solution

You can use it as value.Department ? value.Department : "" which is a JS ternary operator.

Basically it acts like an if condition as below:

if(value.Department){
    value.Department = value.Department;
}else{
    value.Department = "";
}

You need to do this for other fields as well to check for undefined or null values and convert them to empty string.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top