In my model-driven app, I have a polymorphic field, Resource that refers to 2 entity types-- Facility and LabourRate. On my form, I've set the Resource field to refer to a view from Facility and a view from LabourRate. Under some conditions, I need the Resource lookup to only show Facilities or only show LabourRates.
I think this should be possible by using javascript to temporarily change the lookup views that the Resource field refers to. But I'm not clear on the document model so I don't know how to best approach this.
Any hints or good info sources?
Hello @JenniferK,
You can try below code
1. Navigate to https://make.powerapps.com
2. Click Dataverse
3. Expand Table where Polymorphic field available
4. Click Forms
5. Open Main Form then Click Events --> Click Add Library --> Click Create New Webresouce --> name it and select type as Javascript and write below code
function SetDefaultView(executionContext) {
var formContext = executionContext.getFormContext();
if(formContext.getAttribute("FIELDTOVERIFY").getValue()===true){
var customerControl = Xrm.Page.getControl("FILEDLOGICLNAME");
if (customerControl.getEntityTypes().length > 1) {
customerControl.setEntityTypes(['Facilities']);
}
else{
var customerControl = Xrm.Page.getControl("FILEDLOGICLNAME");
if (customerControl.getEntityTypes().length > 1) {
customerControl.setEntityTypes(['LabourRates']);
}
}}
Click Ok and in the ONLOAD operation call the funciton SetDefaultView
Thats it 🙂
Save Publish and Refresh the Form to cross check
Please mark as Answer if it is helpful and provide Kudos
Subscribe : https://www.youtube.com/channel/UCnGNN3hdlKBOr6PXotskNLA
Blog : https://microsoftcrmtechie.blogspot.com
Hi,
Use below javascript code to achieve your requirement.
function onLoad(executionContext){
formContext = executionContext.getFormContext();
var isFTE=formContext.getControl("msft_isfulltimeemployee").getValue();
alert("Full Time Employee"+isFTE);
if(isFTE=="Yes")
{
alert(1);
formContext.getControl("new_polymorphicfieldid").setEntityTypes(['bc_visit']);
}
else
{
alert(2);
formContext.getControl("new_polymorphicfieldid").setEntityTypes(['bc_building']);
}
}
I have created the polymorphic lookup field pointing to Visit and Building entity.
Add this code onload and onchange of field event.
Please mark my answer verified if this is helpful!
Regards,
Bipin Kumar
Follow my Blog: https://xrmdynamicscrm.wordpress.com/
Hi @JenniferK ,
Please find the link below which is very useful for multi table lookups (polymorphic field)
-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
My Blog: Dynamics 365 Key Topics – https://d365topicsbydk.com/
My Youtube Channel : https://www.youtube.com/channel/UCxSIryP2ah2VpEFr-Z72t1A
Regards
Devi