Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Answered

BIZARRE ERRORS: Javascript openForm Function Somehow Calls retrieveRecord Function

Posted on by 279

I'm using JS to open a new Phone Call record on a button click from a Contact record. My code takes the user and contact info to prepopulate the new Phone Call record. Everything works great except I get the following duplicated errors each time it's called:

0246.crmerror.png
These errors do not appear if I comment out the openForm function in my code. Somehow, when this function executes, it is calling twice a retrieveRecord function to get a contacts email address using my User GUID, hence the error.

I've tried using breakpoints but the code doesn't pause! I had to add a break point at the beginning of the code to get it to pause and then add breakpoints in throughout the rest of the code - but when I do that the error doesn't appear!

This doesn't break my code's function but I'd really like to not make random API calls or have unnecessary errors popping up.

Here's my code in question:

function makeCall() {
    Xrm.Utility.showProgressIndicator("Connecting...");

    var userName = Xrm.Utility.getGlobalContext().userSettings.userName;
    var userId = Xrm.Utility.getGlobalContext().userSettings.userId;
        userId = userId.substring(1, userId.length - 1);

    var contactName = window.parentFormContext.data.entity.getPrimaryAttributeValue();
    var contactId = window.parentFormContext.data.entity.getId();
        contactId = contactId.substring(1,contactId.length-1);

    Xrm.WebApi.retrieveRecord("systemuser", userId, "?$select=mobilephone").then(
        function success(result) {
            var userPhone = result.mobilephone.replace(/\(|\)|\-|\ |\./g, "");
            var contactPhone = window.parentFormContext.getAttribute("telephone1").getValue().replace(/\(|\)|\-|\ |\./g, "");

            var body = {
                "Url": "https://handler.twilio.com/twiml/####?contactPhone="   contactPhone,
                "To": userPhone,
                "From": " 13219999290"
            };
            body = JSON.stringify(body);

            var url = "https://####";
            var req = new XMLHttpRequest();
            req.open("POST", url, true);
            req.setRequestHeader('Content-Type', 'application/json');
            req.send(body);

            req.onreadystatechange = function () {

                if (this.readyState === 4) {
                    req.onreadystatechange = null;

                    if (this.status === 200) {
                        Xrm.Utility.closeProgressIndicator();
                        
                        var entityFormOptions = {};
                        entityFormOptions["entityName"] = "phonecall";

                        var formParameters = {};
                        formParameters["subject"] = "RFI Follow-Up Call";

                        formParameters["regardingobjectid"] = contactId;
                        formParameters["regardingobjectidname"] = contactName;
                        formParameters["regardingobjectidtype"] = "contact";

                        var to = new Array();
                        to[0] = new Object();
                        to[0].id = contactId;
                        to[0].name = contactName;
                        to[0].entityType = 'contact';
                        formParameters['to'] = to;

                        var from = new Array();
                        from[0] = new Object();
                        from[0].id = userId;
                        from[0].name = userName
                        from[0].entityType = 'contact';
                        formParameters['from'] = from;

                        Xrm.Navigation.openForm(entityFormOptions, formParameters).then(
                            function (success) {
                                console.log(success);
                            },
                            function (error) {
                                console.log(error);
                            });
                    }
                    else if (this.status === 400) {
                        Xrm.Utility.closeProgressIndicator();
                        var result = "ERROR: Please contact your CRM administrator.";
                        Xrm.Navigation.openErrorDialog({ message: result });
                    }
                }
            }
        },
        function (error) {
            Xrm.Utility.closeProgressIndicator();
            Xrm.Navigation.openErrorDialog({ message: error.message });
        }
    )
}

  • Suggested answer
    Lucas H Profile Picture
    Lucas H 279 on at
    RE: BIZARRE ERRORS: Javascript openForm Function Somehow Calls retrieveRecord Function

    Yup, that was it! Thanks!

  • Verified answer
    Ram Prakash Profile Picture
    Ram Prakash 2,285 on at
    RE: BIZARRE ERRORS: Javascript openForm Function Somehow Calls retrieveRecord Function

    Dear Lucas,

    Try Changing the Line 59

    From

    from[0].entityType = 'contact';

    To

    from[0].entityType = 'systemuser';

    Because in Line no : 5 you are retrieving the User Details.

  • Suggested answer
    Charan Raju C R Profile Picture
    Charan Raju C R 3,155 Moderator on at
    RE: BIZARRE ERRORS: Javascript openForm Function Somehow Calls retrieveRecord Function

    Hi Lucas,

    At line# 59, entity type should be systemuser, not contact. Try replacing from[0].entityType = 'contact'; with from[0].entityType = 'systemuser';

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,558 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,647 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans