web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
Posted on by 1,549

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

I have the same question (0)
  • Verified answer
    Georgy Profile Picture
    on 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.
    
    }


  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on 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
    24,562 on 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;


  • yleclerc Profile Picture
    1,549 on at

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

    You guys are the best!!!

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans