Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

Javascript has lot of errors

Posted on by 51

Hi

I am trying to debug this javascript , and this has some old code, please help in finding all the mistakes,

Thanks

var appUrl = "">orgd38959de.crm5.dynamics.com/";
function onFormLoad() {
    Xrm.Page.getAttribute("parentaccount").addOnChange(openRecord);
    setEmployeeNumber();
}

function openRecord() {
    var recordId = Xrm.Page.getAttribute("parentcustomerid").getValue();
    var url = appUrl + "/main.aspx?pagetype=entityrecord&etn=account&id=" + recordId;
    window.open(url);
}

function setEmployeeNumber() {
    var recordId = Xrm.Page.getAttribute("parentcustomerid").getValue();

    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/contacts?$filter=_parentcustomerid_value eq" + recordId + "&$count=true", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var results = JSON.parse(this.response);
                var recordCount = results["@odata.count"];

                Xrm.Page.getAttribute("employeeid").setValue(recordCount);

                var entity = {};
                entity.numberofemployees = recordCount;

                Xrm.WebApi.online.updateRecord("account", recordId, entity).then(
                    function success(result) {
                        var updatedEntityId = result.id;
                    },
                    function (error) {
                        Xrm.Utility.alertDialog(error.message);
                    }
                );
                
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();

}
  • Verified answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Javascript has lot of errors

    Hi,

    What is the error you are getting?

    Replace XRM.Page with formContext

    And pass executionContext as parameter

    www.alphabold.com/.../

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