Showing posts with label crm form. Show all posts
Showing posts with label crm form. Show all posts

Tuesday, August 30, 2016

How to make fields auto-complete in Dynamics CRM using addOnKeyPress?

In this post I am going to write code snippet to use new Auto Complete feature of Dynamics CRM 2016 to make Country/Region field auto-complete on Account form. Although, this feature is limited to text fields only so you can further check in detail the limitations of this feature, addOnKeyPress, removeOnKeyPress, and fireOnKeyPress methods at  Client Side References

Disclaimer: The field will not complete automatically untill you type a letter and select the word from the list.

Steps:
  1. Create a webresource of type javascript, give it proper name and the label accordingly
  2. Copy and paste following code snippet into the Text Editor
  3. Save and Publish the web resource
  4. Add webresource to the form that you created in step 1
  5. Create an onLoad event and set the library name and function to 'Countries_AC'
  6. Save and Publish the form and you are DONE!!!
 Code Snippet:

function Countries_AC(){
try{
var countriesArray = Countries();
    var keyPressFcn = function (ext) {
        try {
            var userInput = Xrm.Page.getControl("address1_country").getValue();
            resultSet = {
                results: new Array(),
                commands: {
                    id: "sp_commands",
                    label: "Learn More",
                    action: function () {
                        // Specify what you want to do when the user
                        // clicks the "Learn More" link at the bottom
                        // of the auto-completion list.
                        // For this sample, we are just opening a page
                        // that provides information on working with
                        // accounts in CRM.
                        window.open("http://www.state.gov/misc/list/");
                    }
                }
            };

            var userInputLowerCase = userInput.toLowerCase();
            for (i = 0; i < countriesArray.length; i++) {
                if (userInputLowerCase === countriesArray[i].name.substring(0, userInputLowerCase.length).toLowerCase()) {
                    resultSet.results.push({
                        id: i,
                        fields: [countriesArray[i].name]
                    });
                }
                if (resultSet.results.length >= 10) break;
            }

            if (resultSet.results.length > 0) {
                ext.getEventSource().showAutoComplete(resultSet);
            } else {
                ext.getEventSource().hideAutoComplete();
            }
        } catch (e) {
            // Handle any exceptions. In the sample code,
            // we are just displaying the exception, if any.
            console.log(e);
        }
    };

    Xrm.Page.getControl("address1_country").addOnKeyPress(keyPressFcn);

}
catch(e){
    alert(e.message);
}
}

function Countries(){
var countriesArray = [
            { name:'Afghanistan' },
            { name:'Albania' },
            { name:'Algeria' },
            { name:'Andorra' },
            { name:'Angola' },
            { name:'Antigua and Barbuda' },
            { name:'Argentina' },
            { name:'Armenia' },
            { name:'Aruba' },
            { name:'Australia' },
            { name:'Austria' },
            { name:'Azerbaijan' },
            { name:'Bahamas, The' },
            { name:'Bahrain' },
            { name:'Bangladesh' },
            { name:'Barbados' },
            { name:'Belarus' },
            { name:'Belgium' },
            { name:'Belize' },
            { name:'Benin' },
            { name:'Bhutan' },
            { name:'Bolivia' },
            { name:'Bosnia and Herzegovina' },
            { name:'Botswana' },
            { name:'Brazil' },
            { name:'Brunei' },
            { name:'Bulgaria' },
            { name:'Burkina Faso' },
            { name:'Burma' },
            { name:'Burundi' },
            { name:'Cambodia' },
            { name:'Cameroon' },
            { name:'Canada' },
            { name:'Cabo Verde' },
            { name:'Central African Republic' },
            { name:'Chad' },
            { name:'Chile' },
            { name:'China' },
            { name:'Colombia' },
            { name:'Comoros' },
            { name:'Congo, Democratic Republic of the' },
            { name:'Congo, Republic of the' },
            { name:'Costa Rica' },
            { name:'Cote d\'Ivoire' },
            { name:'Croatia' },
            { name:'Cuba' },
            { name:'Curacao' },
            { name:'Cyprus' },
            { name:'Czech Republic' },
            { name:'Denmark' },
            { name:'Djibouti' },
            { name:'Dominica' },
            { name:'Dominican Republic' },
            { name:'East Timor (see Timor-Leste)' },
            { name:'Ecuador' },
            { name:'Egypt' },
            { name:'El Salvador' },
            { name:'Equatorial Guinea' },
            { name:'Eritrea' },
            { name:'Estonia' },
            { name:'Ethiopia' },
            { name:'Fiji' },
            { name:'Finland' },
            { name:'France' },
            { name:'Gabon' },
            { name:'Gambia, The' },
            { name:'Georgia' },
            { name:'Germany' },
            { name:'Ghana' },
            { name:'Greece' },
            { name:'Grenada' },
            { name:'Guatemala' },
            { name:'Guinea' },
            { name:'Guinea-Bissau' },
            { name:'Guyana' },
            { name:'Haiti' },
            { name:'Holy See' },
            { name:'Honduras' },
            { name:'Hong Kong' },
            { name:'Hungary' },
            { name:'Iceland' },
            { name:'India' },
            { name:'Indonesia' },
            { name:'Iran' },
            { name:'Iraq' },
            { name:'Ireland' },
            { name:'Israel' },
            { name:'Italy' },
            { name:'Jamaica' },
            { name:'Japan' },
            { name:'Jordan' },
            { name:'Kazakhstan' },
            { name:'Kenya' },
            { name:'Kiribati' },
            { name:'Korea, North' },
            { name:'Korea, South' },
            { name:'Kosovo' },
            { name:'Kuwait' },
            { name:'Kyrgyzstan' },
            { name:'Laos' },
            { name:'Latvia' },
            { name:'Lebanon' },
            { name:'Lesotho' },
            { name:'Liberia' },
            { name:'Libya' },
            { name:'Liechtenstein' },
            { name:'Lithuania' },
            { name:'Luxembourg' },
            { name:'Macau' },
            { name:'Macedonia' },
            { name:'Madagascar' },
            { name:'Malawi' },
            { name:'Malaysia' },
            { name:'Maldives' },
            { name:'Mali' },
            { name:'Malta' },
            { name:'Marshall Islands' },
            { name:'Mauritania' },
            { name:'Mauritius' },
            { name:'Mexico' },
            { name:'Micronesia' },
            { name:'Moldova' },
            { name:'Monaco' },
            { name:'Mongolia' },
            { name:'Montenegro' },
            { name:'Morocco' },
            { name:'Mozambique' },
            { name:'Namibia' },
            { name:'Nauru' },
            { name:'Nepal' },
            { name:'Netherlands' },
            { name:'Netherlands Antilles' },
            { name:'New Zealand' },
            { name:'Nicaragua' },
            { name:'Niger' },
            { name:'Nigeria' },
            { name:'North Korea' },
            { name:'Norway' },
            { name:'Oman' },
            { name:'Pakistan' },
            { name:'Palau' },
            { name:'Palestinian Territories' },
            { name:'Panama' },
            { name:'Papua New Guinea' },
            { name:'Paraguay' },
            { name:'Peru' },
            { name:'Philippines' },
            { name:'Poland' },
            { name:'Portugal' },
            { name:'Qatar' },
            { name:'Romania' },
            { name:'Russia' },
            { name:'Rwanda' },
            { name:'Saint Kitts and Nevis' },
            { name:'Saint Lucia' },
            { name:'Saint Vincent and the Grenadines' },
            { name:'Samoa' },
            { name:'San Marino' },
            { name:'Sao Tome and Principe' },
            { name:'Saudi Arabia' },
            { name:'Senegal' },
            { name:'Serbia' },
            { name:'Seychelles' },
            { name:'Sierra Leone' },
            { name:'Singapore' },
            { name:'Sint Maarten' },
            { name:'Slovakia' },
            { name:'Slovenia' },
            { name:'Solomon Islands' },
            { name:'Somalia' },
            { name:'South Africa' },
            { name:'South Korea' },
            { name:'South Sudan' },
            { name:'Spain' },
            { name:'Sri Lanka' },
            { name:'Sudan' },
            { name:'Suriname' },
            { name:'Swaziland' },
            { name:'Sweden' },
            { name:'Switzerland' },
            { name:'Syria' },
            { name:'Taiwan' },
            { name:'Tajikistan' },
            { name:'Tanzania' },
            { name:'Thailand' },
            { name:'Timor-Leste' },
            { name:'Togo' },
            { name:'Tonga' },
            { name:'Trinidad and Tobago' },
            { name:'Tunisia' },
            { name:'Turkey' },
            { name:'Turkmenistan' },
            { name:'Tuvalu' },
            { name:'Uganda' },
            { name:'Ukraine' },
            { name:'United Arab Emirates' },
            { name:'United Kingdom' },
            { name:'Uruguay' },
            { name:'Uzbekistan' },
            { name:'Vanuatu' },
            { name:'Venezuela' },
            { name:'Vietnam' },
            { name:'Yemen' },
            { name:'Zambia' },
            { name:'Zimbabwe' } 
        ];
   
    return countriesArray;
}

Wednesday, July 29, 2015

How to set header field value in Dynamics CRM form?



Setting the header field values is not supported in xrm. So, we must move to unsupported ways to set the value of the fields added in header.
Following code snippet will help us to achieve the goal.

document.getElementById("header_xxx_d").childNodes[0].innerText = value;

In the above code snippet, replace xxx with your field name that exists in the header and value with your value you need to set.

But it'll not save in the database. To achieve this you should add a hidden field on  the form and set its value when you are setting value for the header field and then at  on-load event again get the value and assign to the above code snippet.

Cheers!