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 CRM (Archived)

Passing a form attribute parameter to a iframe

(0) ShareShare
ReportReport
Posted on by 2,175

Hi all,

I've got a quick question on how to pass a form field attribute as a parameter in the url of the website iframe. I'll give you the situation:

I want to put a iframe which loads the website (url: "https://test.website.com/user/") and the parameter string on change of a tab (expand or collapse) to the end of the url e.g. "https://test.website.com/user/<parameter>". I've seen an article on this to improve performance of the form here. The iframe is put in a form within the Accounts entity and I want to retrieve a field attribute from the contact (field name "new_attributefield") in the Primary Contact lookup field against the account (field name "primarycontactid".

I've created the form tab and have it collapsed by default and inserted the iframe into the tab. I'm just a little lost as to what to put into the webresource javascript so that it passes the parameter to the end of the URL. The nuance is that the field attribute isn't on accounts but from the contact referenced in the primary contact lookup field.

I tried using the javascript provided in the cobalt site but the script wasn't working. Any help would be greatly appreciated.

Kind regards,

Michael

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    Hi Michael,

    I believe microsoft official documentation should be able to help you. Look closely here?  : docs.microsoft.com/.../use-iframe-and-web-resource-controls-on-a-form  

  • MikeC282 Profile Picture
    2,175 on at

    Thanks Rawish,

    I've got the code set to load on tab state change for for some reason I can't pass an attribute from a related entity to the url. The field attribute is basically a attribute from the Primary Contact lookup on accounts (lets call it "new_testStringAttribute").

    I've set the IFrame to blank and only display the set url from the javascript. The code is as followed:

    function tab_custom_onstatechange(executionContext) {
    
    var formContext = executionContext.getFormContext();
    
    var uniqueId = parent.formContext.getAttributes("new_testStringAttribute").getValue();
    
    var IFrame = formContext.ui.controls.get("IFRAME_test");
    
    var url = "https://test.com/user/" + uniqueId;
    
    IFrame.setSrc("url");
    
    }
    


    It's throwing an error and I'm not sure what's causing this. I've also passed the "execution context as first parameter" in the Handler Properties.

    Kind regards,

    Mike

  • Verified answer
    Rawish Kumar Profile Picture
    13,758 on at

    Hi Mike,

    Correct me if i am wrong. are you trying to pass an attribute value from parent(lookup) record to the iframe?

    You wont be able to get the value by doing parent.xrm and thats not even recommended.

    To do this you need to do the following:

    1. Get the id of the item lookup value on your current form.
    2. De a query via the WebApi to retreive the value from the item entity
    3. Use that value in the way you need to

    For both 1 and 2 there are a lot of resources available with examples. If you are not experienced with REST, I would advise you to use the CRM Rest Builder  https://github.com/jlattimer/CRMRESTBuilder/releases

    should be something like this : https://passion4dynamics.com/2017/05/18/get-lookup-value-from-other-entity-and-set-it-on-the-form-using-web-api-in-microsoft-dynamics-crm-2016/  

    alternatively you can create a calculated field to store the value. but its gives an overhead of creating another field on the current entity.

    https://passion4dynamics.com/2018/01/23/how-to-get-value-from-a-look-or-other-related-entity-using-calculated-field-in-microsoft-dynamics-crm/

  • MikeC282 Profile Picture
    2,175 on at

    Thanks Rawish,

    I used the CRM Rest builder to retrieve the contact lookup record. I noted that it's still using the old xrm.page context so I tried to convert it to the new formcontext but it's giving me a error.

    function iframeParameterSet(executionContext) {
            var formContext = executionContext.getFormContext();
            var contact = formContext.getAttribute("primarycontactid").getValue();
            var newid = contact[0].id.slice(1, -1);
            //check if company has primary contact
                    var req = new XMLHttpRequest();
                    req.open("GET", formContext.getClientUrl() + "/api/data/v9.1/contacts(" + newid + ")?$select=new_testStringAttribute", true);
                    req.setRequestHeader("OData-MaxVersion", "4.0");
                    req.setRequestHeader("OData-Version", "4.0");
                    req.setRequestHeader("Accept", "application/json");
                    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
                    req.onreadystatechange = function() {
                        if (this.readyState === 4) {
                            req.onreadystatechange = null;
                            if (this.status === 200) {
                                var result = JSON.parse(this.response);
                                var uniqueId = result["new_testStringAttribute"];
                                var IFrame = formContext.ui.controls.get("IFRAME_test");                            
                                IFrame.setSrc("https://test.com/user/" + uniqueId);
                            }
                            else {
                                alert(this.statusText);
                            }
                        }
                    };
                    req.send();
                }
    


    I can try using the old xrm.page context but I do know it will be deprecated shortly. Or if it's something wrong with my syntax.

    EDIT: Nevermind Rawish, I got it working. It was a syntax issue. The below code works fine when using the formContext.

    function iframeParameterSet(executionContext) {
            var formContext = executionContext.getFormContext();
            var contact = formContext.getAttribute("primarycontactid").getValue();
            var newid = contact[0].id.slice(1, -1);
            //check if company has primary contact
                    var req = new XMLHttpRequest();
                    req.open("GET", formContext.context.getClientUrl() + "/api/data/v9.1/contacts(" + newid + ")?$select=new_testStringAttribute", true);
                    req.setRequestHeader("OData-MaxVersion", "4.0");
                    req.setRequestHeader("OData-Version", "4.0");
                    req.setRequestHeader("Accept", "application/json");
                    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
                    req.onreadystatechange = function() {
                        if (this.readyState === 4) {
                            req.onreadystatechange = null;
                            if (this.status === 200) {
                                var result = JSON.parse(this.response);
                                var uniqueId = result["new_testStringAttribute"];
                                var IFrame = formContext.ui.controls.get("IFRAME_test");                            
                                IFrame.setSrc("https://test.com/user/" + uniqueId);
                            }
                            else {
                                alert(this.statusText);
                            }
                        }
                    };
                    req.send();
                }


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 CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans