Skip to main content

Notifications

Announcements

No record found.

Service | Customer Service, Contact Center, Fie...
Answered

ReferenceError: formContext is not defined - Javascript Web Resource on Email main form

Posted on by 20

Hello Team,

I am brand new to javascript. So, I'm struggling to understand the correct syntax for my requirement. I have a main email form and for that specific form, I'd like to populate the From column with a queue mailbox when the form state (getformtype) value is Create (1). 

Here is the code from the .js web resource I've created:

function _setFromUser() {
    var formType = formContext.ui.getFormType();
    var legalQueueID = "dcc80a43-39e4-e711-817f-e0071b715bc1";
    var recordName = "";
    var entityName = "queue";
    if(formType == 1) {
        Xrm.Page.getControl("from").getAttribute().setValue([{ id: legalQueueID, name: recordName, entityType: entityName }]);
}}

Here is how I have the event handler configured on the form:

pastedimage1640704984891v2.png

Here is the error I receive: 

ReferenceError: formContext is not defined
    at eval (eval at _executeFunctionInternal (https://********.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3618-2111.2:1988:5296), :1:2)
    at eval ()
    at y._executeFunctionInternal (https://********.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3618-2111.2:1988:5296)
    at y.executeSync_DO_NOT_USE (https://********.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3618-2111.2:1988:3404)
    at r (https://********.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3618-2111.2:1719:61)
    at ee._convertEventDescriptorToCustomEvent (https://********.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3618-2111.2:151:32656)
    at https://********.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3618-2111.2:151:32306
    at Array.map ()
    at ee._getEventHandlerList (https://********.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3618-2111.2:151:32294)
    at Object.execute (https://********.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3618-2111.2:151:20784)

I've tried passing the formContext parameter to the function using the "Comma separated list of parameters that will be passed to the function" column in the event handler dialog. But, that doesn't seem to work either. Any ideas? 

  • AbleAmazing Profile Picture
    AbleAmazing 20 on at
    RE: ReferenceError: formContext is not defined - Javascript Web Resource on Email main form

    Thanks so much. This worked perfectly. The only change I had to make was adding an additional return value to the if(formType) statement. I added an OR operator to include the "undefined" form state because it seems as though the client API does not recognize draft replies as a "create" form state. So, code on line 7 now looks like this:

    if(formType == 1 || formType ==0)

    And now it appears to be working perfectly for my requirement. Thanks again!

  • Verified answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: ReferenceError: formContext is not defined - Javascript Web Resource on Email main form

    Hi,

    You need to update your code as below -

    function _setFromUser(executionContext) {
    var formContext=executionContext.getFormContext();
        var formType = formContext.ui.getFormType();
        var legalQueueID = "dcc80a43-39e4-e711-817f-e0071b715bc1";
        var recordName = "";
        var entityName = "queue";
        if(formType == 1) {
            Xrm.Page.getControl("from").getAttribute().setValue([{ id: legalQueueID, name: recordName, entityType: entityName }]);
    }}

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Verified answer
    Nya Profile Picture
    Nya 29,056 on at
    RE: ReferenceError: formContext is not defined - Javascript Web Resource on Email main form

    Hi,

    The new formContext object is used to in place of the Xrm.Page object.

    It should be used in its normal form as follows:

    function displayName(executionContext)
    {
    	var formContext = executionContext.getFormContext(); // get formContext
    
    	// use formContext instead of Xrm.Page	
    	...
    }

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans