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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Problem Adding Script to Form

(0) ShareShare
ReportReport
Posted on by 235

I am working through Mastering-Dynamics-365-customization-development/ 

When I add a web resource

8244.PNG

with the script 

function suggestZipCodes() { // defined possible ZipCode ZipCodes = [ { name: '94102' }, { name: '94103' }, { name: '94104' }, { name: '94105' }, { name: '94107' }, { name: '94108' }]; var OnZipCodekeyPress = function (fld) { var ZipCodetxt = Xrm.Page.getControl(" address1_postalcode"). getValue(); resultSet = { results: new Array(), commands: { id: "ZipCodecmd", label: "Search in Bing", action: function () { window.open(" http:// bing.com"); // Open Bing URL } } }; var ZipCodetxtLowerCase = ZipCodetxt.toLowerCase(); for (i = 0; i < ZipCodes.length; i + +) { if (ZipCodetxtLowerCase = = = ZipCodes[ i]. name.substring( 0, ZipCodetxtLowerCase.length). toLowerCase()) { resultSet.results.push({ id: i, fields: [ZipCodes[ i]. name] }); } if (resultSet.results.length > = 10) { break;} } if (resultSet.results.length > 0) { fld.getEventSource(). showAutoComplete( resultSet); // Show Auto Complete } else { fld.getEventSource(). hideAutoComplete();// Hide Auto Complete } }; Xrm.Page.getControl(" address1_postalcode"). addOnKeyPress( OnZipCodekeyPress); }

and then add this to the Contacts Main Form

1780.png

 

I get the following error on opening the form:

ReferenceError: Web resource method does not exist: suggestZipCodes
at jp.execute (ghdunn1.crm11.dynamics.com/.../app.js
at Ap._executeIndividualEvent (ghdunn1.crm11.dynamics.com/.../app.js
at Ap._executeEventHandler (ghdunn1.crm11.dynamics.com/.../app.js
at Object.execute (ghdunn1.crm11.dynamics.com/.../app.js
at v._executeSyncAction (ghdunn1.crm11.dynamics.com/.../app.js
at v._executeSync (ghdunn1.crm11.dynamics.com/.../app.js
at v.executeAction (ghdunn1.crm11.dynamics.com/.../app.js
at t.dispatch (ghdunn1.crm11.dynamics.com/.../app.js
at Object.dispatch (ghdunn1.crm11.dynamics.com/.../app.js
at Object.dispatch (https://ghdunn1.crm11.dynamics.com/uclient/scripts/app.js?v=1.3.177-1911.3:563:5034)

 

Can anyone suggest where i am going wrong?

 

Thanks

I have the same question (0)
  • Verified answer
    Henry J. Profile Picture
    5,237 on at

    Hi,

    You could probably reach out to the author of that book, Deepesh Somani on LinkedIn to get help or updates on the examples he has provided in his book.

    Firstly, the code you pasted seems to have some formatting issues.
    You should use a code editor such as Visual Studio Code  (it's free) so that it highlights what's wrong in your JavaScript code.

    I've tried to fix it:

    function suggestZipCodes() { // defined possible ZipCode 
        ZipCodes = [
            { name: '94102' },
            { name: '94103' },
            { name: '94104' },
            { name: '94105' },
            { name: '94107' },
            { name: '94108' }
        ];
        var OnZipCodekeyPress = function (fld) {
            var ZipCodetxt = Xrm.Page.getControl(" address1_postalcode").getValue();
            resultSet = {
                results: new Array(),
                commands: {
                    id: "ZipCodecmd",
                    label: "Search in Bing",
                    action: function () {
                        window.open(" http:// bing.com"); // Open Bing URL
                    }
                }
            };
            var ZipCodetxtLowerCase = ZipCodetxt.toLowerCase();
            for (i = 0; i < ZipCodes.length; i  ) {
                if (ZipCodetxtLowerCase === ZipCodes[i].name.substring(0, ZipCodetxtLowerCase.length).toLowerCase()) {
                    resultSet.results.push({
                        id: i,
                        fields: [ZipCodes[i].name]
                    });
                }
                if (resultSet.results.length >= 10) {
                    break;
                }
            }
            if (resultSet.results.length > 0) {
                fld.getEventSource().showAutoComplete(resultSet); // Show Auto Complete
            } else {
                fld.getEventSource().hideAutoComplete(); // Hide Auto Complete 
            }
        };
        Xrm.Page.getControl(" address1_postalcode").addOnKeyPress(OnZipCodekeyPress);
    }

    Secondly, the code sample isn't supposed to work on the Unified Interface, so it will ultimately not do anything.
    While the Xrm.Page method that has been deprecated might still work as of today, the addOnKeyPress method doesn't work in the Unified Interface.

    Deprecation announcements and suggested alternatives are documented here:
    https://docs.microsoft.com/en-us/power-platform/important-changes-coming#some-client-apis-are-deprecated

    Henry

  • ghdunn Profile Picture
    235 on at

    Thanks for the expert guidance...will be using VS Code going forwards,

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Pallavi Phade Profile Picture

Pallavi Phade 102 Super User 2026 Season 1

#2
Abhilash Warrier Profile Picture

Abhilash Warrier 55 Super User 2026 Season 1

#3
ManoVerse Profile Picture

ManoVerse 51 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans