Skip to main content

Notifications

Announcements

No record found.

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

formContext.data.attributes returns null when trying to retrieve form parameters passed through another form

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

as MS will deprecated Xrm.Page.context.getQueryStringParameters, i am trying to switch it to formContext.data.attributes as MS suggested. but after doing the following, i am not able to get the passed parameter:

1. add the parameter to the form property

pastedimage1597954377667v1.png

2.  js code open form with this parameter value passed:

var entityFormOptions = {};
entityFormOptions["entityName"] = "new_privatesalenotice";

var formParameters = {};
formParameters["new_bypassprivatesalenoticereason"] = "test";

// Open the form.
Xrm.Navigation.openForm(entityFormOptions, formParameters);

3. read value on form load:

var onLoad = function (executionContext) {
var formContext = executionContext.getFormContext();
if (formContext.data.attributes.get("new_bypassprivatesalenoticereason") !== null) // sample code for getting non-entity bound data
{
Xrm.Navigation.openAlertDialog({ text: formContext.data.attributes.get("new_bypassprivatesalenoticereason").getValue() }); // This will show the alert "Test parameter value"
}};

but i am getting formContext.data.attributes null error. 

anyone has an idea? thanks a lot!

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: formContext.data.attributes returns null when trying to retrieve form parameters passed through another form

    hi Clofly,

    i am running this code  in the CRM online, not on-premise; but you are right, i am running the code in legacy web client instead of Unified interface. we will work on the unified UI conversion next after on-premise  to online upgrade. thank you for looking into this for me. really appreciate it. i will test it again after converting to unified interface then.

  • Verified answer
    cloflyMao Profile Picture
    cloflyMao 25,202 on at
    RE: formContext.data.attributes returns null when trying to retrieve form parameters passed through another form

    Hi Ruonan,

    I think I had found reason of the issue.

    Did you run the code in legacy web client

    e.g: I opened quick create form of my custom entity in legacy web client and then encountered you issue.

    Also, formContext.data.attributes is null.

    3326.PNG

    It is because that executionContext in legacy web client and UCI is different, hence name space is not the reason actually and you can still use it.

    If above would be your situation, then please switch to UCI, the legacy web client will be deprecated in October.

    Regards,

    Clofly

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: formContext.data.attributes returns null when trying to retrieve form parameters passed through another form

    hi Clofly,

    thanks for the reply. i tried another custom entity called "new_repo", which on the js i am not using the name space, but still not seeing anything for the data.attributes

    here is my new_repo form property:

    pastedimage1598466594761v2.png

    pastedimage1598466617208v3.png

    pastedimage1598466644125v4.png

    here is my new_repo form js. the function is called on the new_repo entity from loaded. so when the formContext.ui.getFormType() === 2, which is edit mode, i will open a new new_repo from with parameters "bypass_reason", the the form with be opened in the create mode with parameter passed.

    pastedimage1598466685367v5.png

    here is the js debugger result. you can see formContext.data.attributes is null. any ideas? thank you!

    pastedimage1598466369349v1.png

  • cloflyMao Profile Picture
    cloflyMao 25,202 on at
    RE: formContext.data.attributes returns null when trying to retrieve form parameters passed through another form

    Hi Ruonan,

    It seems that function format of the JS library is complicated, I can see it involves closure, it might had affected the executionContext.

    I tried to reproduce your issue with function in anonymous format, however, I am still not able to run function in such format xxx.yyy.zzz.

    var cloflyUtils = cloflyUtils || {};
    
    cloflyUtils.test = (function () {
        
        var onLoad = function (executionContext) {
            debugger;
            var formContext = executionContext.getFormContext();
            if (formContext.data.attributes.get("custom_param") !== null) {
                console.log(formContext.data.attributes.get("custom_param").getValue());
            }
        }
        return onLoad;
    })()
    
    var OnLoad = cloflyUtils.test;

    To invoke onLoad function of cloflyUtils.test, I return it at the end of cloflyUtils.test, then assign it to a new variable, but the function to be executed at OnLoad event of form need to be OnLoad instead.

    pastedimage1598424787258v1.png

    Anyway, I can still get formContext.data.attributes 

    pastedimage1598424936688v2.png

    and the formContext contains entity information of current form.

    pastedimage1598425014503v3.png

    It would be helpful for me if you could share me full code of your js library, then I will able to investigate your code and functions in depth.

    Or you could just create a new blank js file to run your own logic, a simple function call might works compared with complex namespace.

    Regards,

    Clofly
     

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: formContext.data.attributes returns null when trying to retrieve form parameters passed through another form

    hi Clofly,

    thanks for the reply. here is what i am doing.

    on the form property, i am setting up the function will be calling while form onload. i need to check "Pass execution context as first parameter" so i have value of   var formContext = executionContext.getFormContext();    

    pastedimage1598374789092v1.png

    pastedimage1598374823840v2.png

    i am wondering when you refer the test function in your case, how you pass the parameter "executionContext". i might miss something since i am still new to CRM online customization. thank you in advance!

  • cloflyMao Profile Picture
    cloflyMao 25,202 on at
    RE: formContext.data.attributes returns null when trying to retrieve form parameters passed through another form

    Hi Ruonan,

    Could you teach me how to run function in your format?

    As per my understanding, did you execute BFC.PrivateSaleNotice.onLoad function at OnLoad event of form?

    By imitating your format, I was only able to run cloflyutils.test, but I don't know how to run onload inside the cloflyutils.test

    (I have tried following formats and they all failed to be executed, xxx is not defined in web resource.

    cloflyutils.test.onload 

    onload

    )

    var cloflyUtils = cloflyUtils || {};
    
    cloflyUtils.test = (function () {
      console.log(123);
      var onload = function (executionContext) {
        var formContext = executionContext.getFormContext();
        if (formContext.data.attributes.get("custom_param") !== null) {
          console.log(formContext.data.attributes.get("custom_param").getValue());
        }
      }
    
    })

    It seems that you're modifying a system js and adding your custom code to it, however, the executionContext might be different in system js library, thus there was no entity context in formContext.data.

    If above would be your situation, you could create a new js library and run a simple format function

    function xxx() {

    }

    at OnLoad event of form to test whether it could work.

    Regards,

    Clofly 

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: formContext.data.attributes returns null when trying to retrieve form parameters passed through another form

    hi Clofly,

    thanks for the reply. i am working on the CRM Online instead of on-premise, not sure if that matters.

    i debug the code and here is what i saw. you can see formContext.data.attributes is null. 

    pastedimage1598050177286v1.png

    pastedimage1598050231424v2.png

    here is the code i am opening the form:

    pastedimage1598050298086v3.png

    and here is the parameter on the form property:

    pastedimage1598050348445v4.png

    thank you very much!

  • cloflyMao Profile Picture
    cloflyMao 25,202 on at
    RE: formContext.data.attributes returns null when trying to retrieve form parameters passed through another form

    Hi Ruonan,

    Try to debug your code with debugger, I followed your steps and the code works for me.

    Please check whether you had missed anything carefully.

    1. I have a custom entity Country, add parameter(SafeString) to form properties.

    2. Run the function at OnLoad event of Country form.

    3. In Contact form, open Country form by executing function Xrm.Navigation.openForm

    var entityFormOptions = {};
    entityFormOptions["entityName"] = "new_country";
    
    var formParameters = {};
    formParameters["custom_param"] = "Clofly";
    
    Xrm.Navigation.openForm(entityFormOptions, formParameters);

    4. Debugging:

    Everything goes well

    pastedimage1597993671146v1.png

    Or you could try formContext.getAttribute("your attribute name") !== null

    Regards,

    Clofly

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: formContext.data.attributes returns null when trying to retrieve form parameters passed through another form

    hi there, i am trying to pass the parameter from one form to another, not setting up the field value.

    formContext.getAttribute("firstname").getValue(); is working, but that's for getting the field value on the from. thanks~

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,959 Super User 2024 Season 1 on at
    RE: formContext.data.attributes returns null when trying to retrieve form parameters passed through another form

    HI,

    Can you try below code and see if it works?

    var firstName = formContext.getAttribute("firstname").getValue();

    docs.microsoft.com/.../clientapi-form-context

    Field value is getting set on new form?

    Also try adding settimeout.

    carldesouza.com/.../

    Please mark my answer verified if i were helpful

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey Pt 2

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,820 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,514 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans