Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Javascript error: TypeError: Cannot readt property '0' of null at

Posted on by 1,545

We just migrated from CRM 2011 to CRM 2016.

We are having a problem with one script that throws an error. The script is called on the case form when the contact is updated and when the form is saved. The script works fine if the contact field has a value, but it the field is empty, we get the error:

Javascript error: TypeError: Cannot readt property '0' of null at UpdateCourriel.

I'm not really good with javascript so any help is appreciated.

Here are both scripts that are called:

VideCourriel = function () {
window.Xrm.Page.getAttribute('new_courriel').setSubmitMode('always');
window.Xrm.Page.getAttribute("new_courriel").setValue(null);
};

UpdateCourriel = function () {
var contactid = window.Xrm.Page.getAttribute('responsiblecontactid').getValue()[0].id;

if (contactid !== null) {
var contact = $.odata.getOne("/ContactSet(guid'" + contactid + "')/EMailAddress1");

var courriel = "";
if (typeof contact !== 'undefined' && contact !== null && typeof contact.EMailAddress1 !== 'undefined') {
courriel = contact.EMailAddress1;
}

window.Xrm.Page.getAttribute('new_courriel').setSubmitMode('always');
window.Xrm.Page.getAttribute('new_courriel').setValue(courriel);
}
};

Thanks!

*This post is locked for comments

  • yleclerc Profile Picture
    yleclerc 1,545 on at
    RE: Javascript error: TypeError: Cannot readt property '0' of null at

    Thank you all!! I really should learn JS. Tried all 3 and they all work.

    You guys are the best!!!

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Javascript error: TypeError: Cannot readt property '0' of null at

    Hello,

    Try to use following code:

    VideCourriel = function () {

    window.Xrm.Page.getAttribute('new_courriel').setSubmitMode('always');

    window.Xrm.Page.getAttribute("new_courriel").setValue(null);

    };

    UpdateCourriel = function () {

    if (window.Xrm.Page.getAttribute('responsiblecontactid').getValue() == null)

    return;

    var contactid = window.Xrm.Page.getAttribute('responsiblecontactid').getValue()[0].id;

    if (contactid !== null) {

    var contact = $.odata.getOne("/ContactSet(guid'" + contactid + "')/EMailAddress1");

    var courriel = "";

    if (typeof contact !== 'undefined' && contact !== null && typeof contact.EMailAddress1 !== 'undefined') {

    courriel = contact.EMailAddress1;

    }

    window.Xrm.Page.getAttribute('new_courriel').setSubmitMode('always');

    window.Xrm.Page.getAttribute('new_courriel').setValue(courriel);

    }

    };

  • Verified answer
    jlattimer Profile Picture
    jlattimer 24,558 on at
    RE: Javascript error: TypeError: Cannot readt property '0' of null at

    You'd need to check if contact has a value before trying to extract the id. As you've seen if contact is null, trying to get the id returns an error

    var contact =  window.Xrm.Page.getAttribute('responsiblecontactid').getValue();
    if (!contact) 
    return; var contactid = window.Xrm.Page.getAttribute('responsiblecontactid').getValue()[0].id;


  • Verified answer
    Georgy Profile Picture
    Georgy on at
    RE: Javascript error: TypeError: Cannot readt property '0' of null at

    Your problem is with following line:

    window.Xrm.Page.getAttribute('responsiblecontactid').getValue()[0].id;

    change it to

    var contactIdValue = window.Xrm.Page.getAttribute('responsiblecontactid').getValue();
    
    if (contactIdValue != null && contactIdValue.length > 0)
    
    {
    
    var contactId = contactIdValue[0].id;
    
    ... your code goes here.
    
    }


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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans