cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
indiranoppe
Frequent Visitor

Change link webresource

I have to change the link from my webresource based on the lookup value. I used the code that you see on the pictures. But when the person is selected the link doesn't change. What dit i do wrong?

8 REPLIES 8
ChrisPiasecki
Super User
Super User

Hi @indiranoppe ,

 

Xrm.Page is deprecated. You should be using the executionContext to retrieve the formContext to which you can then retrieve your IFRAME control. 

function ChangeHtmlWebResource(executionContext) {
    // use formContext instead of Xrm.Page	
    var formContext = executionContext.getFormContext(); 
    console.log("ChangeHtmlWebResource function called!");
    var webResourceControl = formContext.getControl("IFRAME_test");
    var currentUrl = webResourceControl.getSrc();
    console.log("Current url: " + currentUrl);
    webResourceControl.setSrc("https://example.com");
    var newUrl = webResourceControl.getSrc();
    console.log("New url: " + newUrl);
	
}
 
Also, ensure that you check the option to pass the Execution Context when registering your event handler on your form.
ExecutionContext.PNG
 
---
Please click Accept as Solution if my post answered your question. This will help others find solutions to similar questions. If you like my post and/or find it helpful, please consider giving it a Thumbs Up.
EricRegnier
Super User II
Super User II

Hi @indiranoppe, Couple of points:

  1. To add make sure you registered the OnChange event on the Person lookup. I usually do it on the OnLoad event of the form. See code below.
  2. To set an external URLs you should be using an iFrame control instead of a web resource. Web Resources are for resources hosted within your environment as web resources.
function OnLoad(executionContext) {
    let formContext = executionContext.getFormContext(); 
    formContext.getAttribute("cr45e_verkoper").addOnChange(VerkoperOnChange);
}

function VerkoperOnChange(executionContext) {
    let formContext = executionContext.getFormContext(); 
    //set web resource url or iframe url
}

Also you don't need to set it to null before setting a new URL.

Hope this helps! 

Thank you for your answer. Where do i put the if else method for change of the src? De link is a different site, this for each salesperson. 

Thank you. But i do i insert the if else method for the lookup. I'm sorry Javascript is new for me

Hi @indiranoppe ,

 

What you had before was fine, but as previously mentioned, just replace the Xrm.Page references to use the formcontext like below, then ensure you register your function to the OnChange event of your verkoper field as i shown above.

function ChangeHtmlWebResource(executionContext) {
    // use formContext instead of Xrm.Page	
    var formContext = executionContext.getFormContext(); 
   
    var webResourceControl = formContext.getControl("cr45e_bookings");
    var user = formContext.getAttribute("cr45e_verkoper").getValue()[0].id;

    if (user == "....")
    {
        webResourceControl.setSrc("https://coenso.be");
    }
    else if (user == "....")
    {
        webResourceControl.setSrc("https://...");
    }
}

 

 ---
Please click Accept as Solution if my post answered your question. This will help others find solutions to similar questions. If you like my post and/or find it helpful, please consider giving it a Thumbs Up.

I used the code like you told. I don't get errors but the webresource doesn't change the src link. When i used inspected, the iframe doesn't have a different url. I checked the id from the salesman and it's correct. The parameter option is checked. 

Hi @indiranoppe,

 

Can you add some Console.log statements in the function and see if your event is even firing? You can see the logs in the browser console using the developer tools (press F12 to bring them up). 

You can paste the complete code and screenshot(s) of the registered events on the form? As I mentioned in my post, I think your code is ok but it's not firing on change of the field. You need to register to the event....

Helpful resources

Announcements
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

MBAS Attendee Badge

Claim Your Badge & Digital Swag!

Check out how to claim yours today!

secondImage

Are Your Ready?

Test your skills now with the Cloud Skill Challenge.

secondImage

Demo Extravaganza is Back!

We are excited to announce that Demo Extravaganza for 2021 has started!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Top Solution Authors
Users online (46,349)