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 :
Customer experience | Sales, Customer Insights,...
Answered

BIZARRE ERRORS: Javascript openForm Function Somehow Calls retrieveRecord Function

(0) ShareShare
ReportReport
Posted on by 283

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 });
        }
    )
}

I have the same question (0)
  • Suggested answer
    Charan Raju C R Profile Picture
    7 Moderator on at

    Hi Lucas,

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

  • Verified answer
    Ram Prakash Duraisamy Profile Picture
    2,287 on at

    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
    Lucas H Profile Picture
    283 on at

    Yup, that was it! Thanks!

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 61

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 52 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans