0

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));    
        }   
    })  
}
  • 1
    try it as value.Title0 ? value.Title0 : "" and check. You need to do it for other fields as well ! – Gautam Sheth Sep 17 '18 at 6:21
2

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.

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.