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,...
Unanswered

JScript to change iframe values.

(0) ShareShare
ReportReport
Posted on by 15

We use an option set to change the URL of an iFrame. We are getting occasional errors that there are extra spaces in the URL.

Original Code: --- I modified the URL's to keep private, but this code works. (Note it is using Xrm.Page)

function SetDocumentFrame(){
    //Get the value of an option set attribute 

    var personid = Xrm.Page.data.entity.attributes.get("do_personid").getValue();
    var value = Xrm.Page.data.entity.attributes.get("do_appreg_recordreviewtype").getValue();
    var newTarget = "";  
    //Set the target based on the value of the option set  
    switch (value) {  
        case 1:  //Document
            newTarget = "https://1.cgi?person_id=" personid "&output_style=p";// Removes the extra characters on the end.  
            break;  
        case 2:  //Academic Record Summary (RECSUM)
            newTarget = "https://1.cgi?person_id=" personid "&output_style=p" "&foo="; 
            break;  
        case 3:  // Work Experience (GS08)
            newTarget = "https://1.cgi?person_id=" personid "&output_style=p" "&foo="; 
            break;  
        case 4:  //Exams
            newTarget = "https://1.cgi?person_id=" personid "&output_style=p" "&foo="; 
            break; 
        case 5:  //GS02 Application
            newTarget = "https://1.cgi?person_id=" personid "&output_style=p" "&foo="; 
            break;  
        case 6:  //GS02 Letters
            newTarget = "https://1.cgi?person_id=" personid "&output_style=p" "&foo=";
            break;  
        default:  
            newTarget = "https://sample.com//WebResources/docreviewdefault";  
            break;  
    }  
    //Get the default URL for the IFRAME, which includes the   
    // query string parameters  
    var IFrame = Xrm.Page.ui.controls.get("IFRAME_reviewframe");  
    var Url = IFrame.getSrc();  
    // Capture the parameters  
    var params = Url.substr(Url.indexOf("?"));  
    //Append the parameters to the new page URL  
    newTarget = newTarget   params;  
    // Use the setSrc method so that the IFRAME uses the  
    // new page with the existing parameters  
    IFrame.setSrc(newTarget);  
}

Thinking that the extra space may be related to the API used. We changed to use the getFormContext (Client API reference) and attempted to trim for the extra spaces.

REFERENCE
https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/executioncontext/getFormContext
https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/getglobalcontext-clientglobalcontext.js.aspx
https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/clientapi-form-context
https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-utility/getglobalcontext

New Code: 

function SetDocumentFrame(executionContext){
    var formContext = executionContext.getFormContext();
    var personid = formContext.getAttribute("do_personid").getValue(); 
    var value = formContext.getAttribute("do_appreg_recordreviewtype").getValue(); 
    var newTarget = "";  
    //Set the target based on the value of the option set  
    switch (value) {  
        case 1:  
            newTarget = `https://1.cgi?person_id=${personId}`.trim()
            break;  
        case 2:  
            newTarget = `https://2.cgi?person_id=${personId}`.trim()
            break;  
        case 3: 
            newTarget = `https://3.cgi?person_id=${personId}`.trim()
            break;  
        case 4: 
            newTarget = `https://4.cgi?person_id=${personId}`.trim()
            break; 
        case 5:  
            newTarget = `https://5.cgi?person_id=${personId}`.trim()
            break;  
        case 6:  
            newTarget = `https://6.cgi?person_id=${personId}`.trim()
            break;  
        default:  
            newTarget = `https://./WebResources/websitemessage.html`.trim();  
            break;  
    }  
    //Get the default URL for the IFRAME, which includes the   
    // query string parameters  
    var IFrame = formContext.ui.controls.get("IFRAME_reviewframe");  
    var Url = IFrame.getSrc();  
    // Capture the parameters  
    var params = Url.substr(Url.indexOf("?"));  
    //Append the parameters to the new page URL  
    newTarget = newTarget   params;  
    // Use the setSrc method so that the IFRAME uses the  
    // new page with the existing parameters  
    IFrame.setSrc(newTarget);  
}

QUESTIONS:

1. Is this the correct way to trim?

2. We are thinking that the Xrm.Page may be causing spaces, but when I run using getFormContext. I get an error: Cannot read property 'getFormContext' of undefined.  Why?

3. How do I modify the line : var IFrame = Xrm.Page.ui.controls.get("IFRAME_reviewframe"); to use the new API method? Is this right?  var IFrame = formContext.ui.controls.get("IFRAME_reviewframe");

I have the same question (0)

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 146 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 59

#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