Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Javascript onload error

Posted on by 1,540

Hi,

A script is running onLoad of an opportunity form that is causing an error. It could be related to some security role changes but the error isn't a plugin one that I'm used to seeing regarding security.

Error: "Cannot read property 'getId' of null"

The script is:

function onDocumentsLoad() {

    var navDocument;
    if (Xrm.Page.ui.getFormType() == 1)
        return;
    var recordId = Xrm.Page.data.entity.getId().replace("{", "").replace("}", "");
    var oTypeCode = Xrm.Page.context.getQueryStringParameters().etc;
    var CurrentFormId = Xrm.Page.ui.formSelector.getCurrentItem().getId().replace("{", "").replace("}", "");
    if (Xrm.Page.getControl("IFRAME_Documents") != null) {
        Xrm.Page.getControl("IFRAME_Documents").setSrc(Xrm.Page.context.getClientUrl() + "/userdefined/areas.aspx?formid=" + CurrentFormId + "&inlineEdit=1&navItemName=Documents&oId=%7b" + recordId + "%7d&oType=" + oTypeCode + "&pagemode=iframe&rof=true&security=852023&tabSet=areaSPDocuments&theme=Outlook15White");
    }
}

From what I've read this might be a legacy thing if the script was written for 2011?

Can I either update the role (if that is the cause) or put a condition in there to handle the 'null'?

Thanks

*This post is locked for comments

  • Suggested answer
    Indika Abayarathne Profile Picture
    Indika Abayarathne 671 on at
    RE: Javascript onload error

    "Form Items are available from the Xrm.Page.ui.formSelector.getCurrentItem or the Xrm.Page.ui.formSelector.items collection. A form item represents a form that is available to a user because it is associated with a security role that the user is also associated to. Often there will be only one form. When more than one form is available, these methods can be used to change the form the user is viewing."

    Cited from: msdn.microsoft.com/.../gg309560.aspx

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: Javascript onload error

    Hi,

    Please modify the code as shown below.

    function onDocumentsLoad() {
    
        var navDocument;
        if (Xrm.Page.ui.getFormType() == 1)
            return;
        var recordId = Xrm.Page.data.entity.getId().replace("{", "").replace("}", "");
        var oTypeCode = Xrm.Page.context.getQueryStringParameters().etc;
        var CurrentForm =  Xrm.Page.ui.formSelector.getCurrentItem();
        var CurrentFormId;
         if(CurrentForm != null){
           CurrentFormId = CurrentForm.getId().replace("{", "").replace("}", "");
        }
        else{
           CurrentFormId = Xrm.Page.ui.formSelector.items[0].getId().replace("{", "").replace("}", ""); 
        }
        if (Xrm.Page.getControl("IFRAME_Documents") != null) {
            Xrm.Page.getControl("IFRAME_Documents").setSrc(Xrm.Page.context.getClientUrl() + "/userdefined/areas.aspx?formid=" + CurrentFormId + "&inlineEdit=1&navItemName=Documents&oId=%7b" + recordId + "%7d&oType=" + oTypeCode + "&pagemode=iframe&rof=true&security=852023&tabSet=areaSPDocuments&theme=Outlook15White");
        }
    }

    Hope this helps.

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Javascript onload error

    There is a form type condition - it won't run for on create:

    if (Xrm.Page.ui.getFormType() == 1)

     return;

    I'd vote for what Gopalan suggested. :)

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Javascript onload error

    Of course will hit into error if you are running this on CREATE form. (It should be applicable for non-create form only)

    var recordId = Xrm.Page.data.entity.getId().replace("{", "").replace("}", "");

  • Suggested answer
    Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: Javascript onload error

    Hi

    When there is only one form available for the user then

    Xrm.Page.ui.formSelector.getCurrentItem()

    would return null.

    [View:https://msdn.microsoft.com/en-us/library/gg327828.aspx#BKMK_getCurrentItem:750:50]

    https://community.dynamics.com/crm/f/117/t/207551

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,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans