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 :
Microsoft Dynamics 365 | Integration, Dataverse...
Unanswered

Form always loading with focus on field.

(0) ShareShare
ReportReport
Posted on by 5
Hello 
 
I am using a javascript function to populate a drop-down list based on the selection of another drop down however this is always setting the drop-down to the from focus despite me adding code to set the focus to another field. Could someone please help ?
 
function Form_onload() {
    //Function to Filter SubPicklist
    filterPicklist = function (oPicklist, oSubPicklist) {
Xrm.Page.getControl(/mx_problemsubcategories/).setFocus(false);
        if (oSubPicklist.getAttribute().getSelectedOption() != null) {
            var currentText = oSubPicklist.getAttribute().getSelectedOption().text;
            var currentValue = oSubPicklist.getAttribute().getSelectedOption().value;
        }
        else {
            var currentText = //;
            var currentValue = 0;
        }
        if (!oSubPicklist.originalPicklistValues) {
            oSubPicklist.originalPicklistValues = oSubPicklist.getAttribute().getOptions();
        }

        // Clear existing options
        var lstLen = parseInt(oSubPicklist.originalPicklistValues.length);
        for (var i = 0; i < lstLen; i++) {
            oSubPicklist.removeOption(oSubPicklist.originalPicklistValues[i].value);
        }

        // Loop through original values and add back any that match the parent
        if (oPicklist.getAttribute().getOptions() != null) {
            for (var i = 0; i < lstLen; i++) {
                var origOpt = oSubPicklist.originalPicklistValues[i];
                var startVal = oPicklist.getAttribute().getValue() * 100;
                var endVal = (oPicklist.getAttribute().getValue() * 100) + 99;
                
                if (parseInt(origOpt.value) >= startVal && origOpt.value <= endVal) {
                    oSubPicklist.addOption(origOpt);
                }
            }
        }

        // put back original value
        try {
            oSubPicklist.getAttribute().setValue(currentValue);
    
            //Xrm.Page.getControl(/subject/).setFocus(true);
            Xrm.Page.getControl(/mx_problemsubcategories/).setFocus(false);
            
        }
        catch (e) {
            alert(/ERROR fn filterPicklist: / + e.InnerException.toString);
        }
        
        
        Xrm.Page.getControl(/mx_problemsubcategories/).setFocus(false);
        //Xrm.Page.getControl(/subject/).setFocus(true);
    } // endfn filterPicklist
    

    var oPicklist = Xrm.Page.getControl(/mx_problemcategories/);
    var oSubPicklist = Xrm.Page.getControl(/mx_problemsubcategories/);
    filterPicklist(oPicklist, oSubPicklist);
    
    //Xrm.Page.getControl(/subject/).setFocus(true);
    Xrm.Page.getControl(/mx_problemsubcategories/).setFocus(false);
    
}
function mx_problemcategories_onchange() {
    
    var oPicklist = Xrm.Page.getControl(/mx_problemcategories/);
    var oSubPicklist = Xrm.Page.getControl(/mx_problemsubcategories/);
    filterPicklist(oPicklist, oSubPicklist);
    
} //endfn mx_keyword_primary_onchange
I have the same question (0)
  • Leah Ju Profile Picture
    Microsoft Employee on at
    Hi Partner,
    Is the dropdown list that is always set to focus the one that is populated with values?
    And which field needs to be set to focus?
    For the setFocus() function: 
    Parentheses shouldn't have values in them, true and false aren't needed.
    Refer to the following blog to know how to use it:
    https://www.crmcrate.com/javascript/using-setfocus-client-api-in-dynamics-365-crm/
    I hope you can mark my answer verified if it is helpful! If you have any questions, please feel free to contact me.
    Regards,
    Leah
     

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 > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 45 Most Valuable Professional

#2
iampranjal Profile Picture

iampranjal 36

#3
Satyam Prakash Profile Picture

Satyam Prakash 31

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans