Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Unanswered

JScript to change iframe values.

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");

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans