Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

TypeError: Cannot read property '0' of null when the lookup field clear

Posted on by Microsoft Employee

So I have a Vendor name Lookup field and whenever I select a value for that it will fill the expense category lookup. So my problem is when I clear the Vendor Name lookup, I'm getting this error "TypeError: Cannot read property '0' of null at setExpenseCategorybasedonVendor".

Already tried this clear the value of expense category

    lookup1.reset();
    value.reset();
    cse_expense_category_value.reset();
    cse_expense_category_value.clear();
    nullcse_expense_category_value.setValue("");
    cse_expense_category_value.setValue(null);
    document.getElementById("cse_expense_category").reset();
    document.getElementById("cse_expense_category").null();
    document.getElementById("cse_expense_category").clear();
    document.getElementById("cse_expense_category").setValue("");	    
    document.getElementById("cse_expense_category").setValue(null);	
    Xrm.Page.getElementById("cse_expense_category").reset();	
    Xrm.Page.getElementById("cse_expense_category").clear();	
    Xrm.Page.getElementById("cse_expense_category").null();	
    Xrm.Page.getElementById("cse_expense_category").setValue("");	
    Xrm.Page.getElementById("cse_expense_category").setValue(null);


My full code

function makeRequest(method, url) {
    return new Promise(function (resolve, reject) {
        var xhr = new XMLHttpRequest();
        xhr.open(method, url);
        xhr.setRequestHeader("OData-MaxVersion", "4.0");
        xhr.setRequestHeader("OData-Version", "4.0");
        xhr.setRequestHeader("Accept", "application/json");
        xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        xhr.onload = function () {
            if (this.status >= 200 && this.status < 300) {

                resolve(xhr.response);
            } else {
                reject({
                    status: this.status,
                    statusText: xhr.statusText
                });
            }
        };
        xhr.onerror = function () {
            reject({
                status: this.status,
                statusText: xhr.statusText
            });
        };
        xhr.send();
    });
    }

    function setExpenseCategorybasedonVendor() {
    var lookup1 = Xrm.Page.getAttribute("cse_vendor_name").getValue()[0].id;
    var clientUrl = Xrm.Page.context.getClientUrl();
    var query = clientUrl + "/api/data/v8.0/cse_vendormasters(" +           lookup1.slice(1, -1) + ")?$select=_cse_expense_category_value";
    makeRequest('GET', query)
    .then(function (res) {
    var res2 = JSON.parse(res);
    var guid = res2._cse_expense_category_value;
    var query2 = clientUrl + "/api/data/v8.0/cse_expensemasters(" + guid + ")?     $select=cse_name";
    makeRequest('GET', query2)
    .then(function (response) {
    var res3 = JSON.parse(response);
    var value = new Array();
    value[0] = new Object();
    value[0].id = guid;
    value[0].name = res3.cse_name;
    value[0].entityType = "cse_expensemasters";
    if (lookup1 != null){
    Xrm.Page.getAttribute("cse_expense_category").setValue(value);
    Xrm.Page.getControl("cse_expense_category").setFocus();
    Xrm.Page.getControl("cse_amount").setFocus();
    }
    else{
    Xrm.Page.getElementById("cse_expense_category").clear();	
    }
    })
    .catch(function (err) {
    console.error('there was an error!', err.statusText);
    });
    })
    .catch(function (err) {
    console.error(' there was an error!', err.statusText);
    });
    }


*This post is locked for comments

  • rthompson Profile Picture
    rthompson 1,530 on at
    RE: TypeError: Cannot read property '0' of null when the lookup field clear

    What was the answer?

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: TypeError: Cannot read property '0' of null when the lookup field clear

    I already know the answer, thanks guys

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: TypeError: Cannot read property '0' of null when the lookup field clear

    Hi Mohd,

    it doesn't work

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: TypeError: Cannot read property '0' of null when the lookup field clear

    Try this line.

    Xrm.Page.getAttribute("cse_expense_category").setValue();

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: TypeError: Cannot read property '0' of null when the lookup field clear

    Hi ,

    I already tried it the error "TypeError: Cannot read property '0' of null" was gone, but the field value is still there

  • Suggested answer
    Aiden Kaskela Profile Picture
    Aiden Kaskela 19,692 on at
    RE: TypeError: Cannot read property '0' of null when the lookup field clear

    Hi,

    Did you try setValue(null); ?

    Thanks,

     Aiden

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: TypeError: Cannot read property '0' of null when the lookup field clear

    Hi Mohd

    Thanks for your code the error was gone, but the value of expense category is this still there,

    I tried to insert it but nothing happen

    Xrm.Page.getControl("cse_expense_category").setValue(" ");

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: TypeError: Cannot read property '0' of null when the lookup field clear

    Hi Aiden

    Thanks the error was gone, but the value  of expense category is this still there,

    I tried to insert it but nothing happen

    Xrm.Page.getControl("cse_expense_category").setValue(" ");

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: TypeError: Cannot read property '0' of null when the lookup field clear

    Use this code:

    function setExpenseCategorybasedonVendor() {
    	if(Xrm.Page.getAttribute("cse_vendor_name") && Xrm.Page.getAttribute("cse_vendor_name").getValue()){
        var lookup1 = Xrm.Page.getAttribute("cse_vendor_name").getValue()[0].id;
        var clientUrl = Xrm.Page.context.getClientUrl();
        var query = clientUrl + "/api/data/v8.0/cse_vendormasters(" + lookup1.slice(1, -1) + ")?$select=_cse_expense_category_value";
        makeRequest('GET', query)
        .then(function (res) {
        var res2 = JSON.parse(res);
        var guid = res2._cse_expense_category_value;
        var query2 = clientUrl + "/api/data/v8.0/cse_expensemasters(" + guid + ")?$select=cse_name,cse_expensemasterid";
        makeRequest('GET', query2)
        .then(function (response) {
        var res3 = JSON.parse(response);
        var value = new Array();
        value[0] = new Object();
        value[0].id = res3.cse_expensemasterid;
        value[0].name = res3.cse_name;
        value[0].entityType = "cse_expensemasters";
        if (lookup1 != null){
        Xrm.Page.getAttribute("cse_expense_category").setValue(value);
        Xrm.Page.getControl("cse_expense_category").setFocus();
        Xrm.Page.getControl("cse_amount").setFocus();
        }
        else{
        Xrm.Page.getElementById("cse_expense_category").clear();	
        }
        })
        .catch(function (err) {
        console.error('there was an error!', err.statusText);
        });
        })
        .catch(function (err) {
        console.error(' there was an error!', err.statusText);
        });
    	}
        }
    


  • Verified answer
    Aiden Kaskela Profile Picture
    Aiden Kaskela 19,692 on at
    RE: TypeError: Cannot read property '0' of null when the lookup field clear

    Hi,

    The error reads "Cannot read property '0' of null" - as far as I can tell that's happening when the vendor lookup is null, and it's happening on this line:

    var lookup1 = Xrm.Page.getAttribute("cse_vendor_name").getValue()[0].id;

    With no vendor, getValue() returns null. You should be checking if it's null after the get value and branching your logic appropriately.

    FWIW, "Xrm.Page.getElementById("cse_expense_category").setValue(null);" should work just fine.

    Hope this helps! I'd appreciate if you'd mark this as Answering your question.

    Thanks,

     Aiden

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans