I have 3 forms in the accounts entity. AccountA (default out of the box form), AccountB, AccountC
I have created a custom field (type: single line of text) to store the value of the form the record was created in.
I used the following javascript to open the correct form based on the field value. Although the correct form is opening onLoad, I'm now experiencing other issues: When I create a new record and click on "save and close" or "save" it saves and automatically opens a new AccountA form. When I try to switch to AccountB or Account C form from the dropdown, it gives this the following pop up and again opens AccountA form.
Here is the code I used:
function onLoad() { //if the form is update form if (Xrm.Page.ui.getFormType()==2) // variable to store the name of the form var lblForm; // get the value picklist field var relType = Xrm.Page.getAttribute("new_formname").getValue(); // switch statement to assign the form to the picklist value //change the switch statement based on the forms numbers and picklist values switch (relType) { case 'AccountC': lblForm = "AccountC"; break; case 'AccountB': lblForm = "AccountB"; break; default: lblForm = "AccountA"; } //check if the current form is form need to be displayed based on the value if (Xrm.Page.ui.formSelector.getCurrentItem().getLabel() != lblForm) { var items = Xrm.Page.ui.formSelector.items.get(); for (var i in items) { var item = items[i]; var itemId = item.getId(); var itemLabel = item.getLabel() if (itemLabel == lblForm) { //navigate to the form item.navigate(); return; } //endif } //end for } //endif }//endif
Please help me solve this problem. Thank you!
*This post is locked for comments
It worked thanks so much! Can't believe it was such a small mistake.
Hi,
I think you have a bracket problem.
Could you please try this:
function onLoad() { //if the form is update form if (Xrm.Page.ui.getFormType()==2){ // variable to store the name of the form var lblForm; // get the value picklist field var relType = Xrm.Page.getAttribute("new_formname").getValue(); // switch statement to assign the form to the picklist value //change the switch statement based on the forms numbers and picklist values switch (relType) { case 'AccountC': lblForm = "AccountC"; break; case 'AccountB': lblForm = "AccountB"; break; default: lblForm = "AccountA"; } //check if the current form is form need to be displayed based on the value if (Xrm.Page.ui.formSelector.getCurrentItem().getLabel() != lblForm) { var items = Xrm.Page.ui.formSelector.items.get(); for (var i in items) { var item = items[i]; var itemId = item.getId(); var itemLabel = item.getLabel() if (itemLabel == lblForm) { //navigate to the form item.navigate(); return; } //endif } //end for } //endif } }//endif
changing it to getText() throws a script error onload of form
If new_formname field is option set then your code wont work. getValue will give you optionset value not the label. to get label use this line
var relType = Xrm.Page.getAttribute("new_formname").getText();
André Arnaud de Cal...
291,965
Super User 2025 Season 1
Martin Dráb
230,817
Most Valuable Professional
nmaenpaa
101,156