Wednesday, January 10, 2018

How to restrict Customer field for specific record type?

Customer field allow the users to select Account/Contact. So, to restrict Customer field in such a way it should allow the users to select only contacts. Add defaultcustomer at onload event of the form.



function defaultcustomer(){
    Xrm.Page.getControl("customerid").addPreSearch(addFilter);
}
function addFilter()
{
    var customerAccountFilter = "Use following filter here";
    Xrm.Page.getControl("customerid").addCustomFilter(customerAccountFilter, "account");
    Xrm.Page.getControl("customerid").getAttribute().setLookupTypes(["contact"]);
}


Filter: use following filter after removing internal double quotes.
"<"filter type='and'">""<"condition attribute='accountid' operator='null' "/>""<"/filter">"

~Good Luck~

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;
}

Tuesday, August 9, 2016

How to retrieve the value of an Optionset in Dynamics CRM?

In Microsoft Dynamics CRM, OptionSet labels are stored in stored in the StringMapBase table in Dynamics CRM DB. So, Whenever you need to get the optionset label. You'll have to query that table i.e

 SELECT 

        [ObjectTypeCode] AS 'Object Type Code',
        [AttributeName] AS 'Optionset Name',
        [AttributeValue] AS 'Optionset Item Value',

        [Value] AS 'Optionset Item Text',
        [DisplayOrder] AS 'Display Order',

        [VersionNumber] AS 'Version Number',
        [StringMapId] AS 'String Map ID',

        [LangId] AS 'Lang GUID',
        [OrganizationId] AS 'Organization'
  FROM [StringMap]
  WHERE AttributeName = 'YourOptionSetName'



CHEERS!!

Wednesday, May 4, 2016

CRM 2016: JavaScript – Retrieve records using SOAP Endpoint and XRMServiceToolkit

In my previous blogpost I described how to use XRM Service Toolkit in Dynamics CRM (2011 / 2013 / 2015 and 2016).

Today I am going to show how to retrieve parent account information onChange event of account lookup on Contact form. 

Remember! Don’t forget to add XRMServiceToolkit library in form properties. 

Now, from following code snippet you need to add getParentAccountInfo at onChange event of Parent Account field on contact field 


function getParentAccountInfo(accountLookup)
{
    try{
        var accountID = Xrm.Page.getAttribute(accountLookup).getValue()[0].id;
       
        var cols = ["emailaddress1","telephone1"];

        var retrievedAccount = XrmServiceToolkit.Soap.Retrieve("contact", contactID, cols);
       
        var emailAddress = Xrm.Page.getAttribute("emailaddress1");
        var telePhone = Xrm.Page.getAttribute("telephone1");
       
        var v_emailAddress = typeof retrievedAccount.attributes["emailaddress1"] === 'undefined'?'':
                                    retrievedAccount.attributes["emailaddress1"].value;
                                   
        var v_telephone = typeof retrievedAccount.attributes["telephone1"] === 'undefined'?'':
                                    retrievedAccount.attributes["telephone1"].value;
       
        emailAddress.setValue(v_emailAddress);
        telePhone.setValue(v_telephone);
    }
    catch(e){
        alert("Error Occured: "+e.Message);
    }
}

Enjoy!

Thursday, February 4, 2016

Only the Deployment Administrators are able to use Deployment Manager. You are not a Deployment Administrator

A few days back I come up with following error while accessing the Microsoft Dynamics CRM Deployment Manager on the server

Only the Deployment Administrators are able to use Deployment Manager. You are not a Deployment Administrator.

After spending a few hours, I found directions and fortunately added myself as to Deployment Administrator group.

To try this you just needed access of MSCRM_CONFIG

-- Get Deployment Administrators
SELECT Name, IsDisabled, Name, IsDeleted
FROM [MSCRM_CONFIG].[dbo].[SystemUser]
WHERE DefaultOrganizationId ='00000000-0000-0000-0000-000000000000'


-- Get guid of security roles Administrator
SELECT  Id, [Name], [UniqueifierId] 
FROM [MSCRM_CONFIG].[dbo].[SecurityRole]  
WHERE ((([Name] = 'Administrator')) ) AND (IsDeleted = 0)


-- insert into deployment administrator group
INSERT
INTO [MSCRM_CONFIG].[dbo].[SystemUser](Name, IsDeleted, DefaultOrganizationId, Id, IsDisabled) 

VALUES ('domain\user', 0, '00000000-0000-0000-0000-000000000000', NEWID(), NULL)

--Give security role to user added before
INSERT
INTO [MSCRM_CONFIG].[dbo].[SystemUserRoles](Id, SecurityRoleId, SystemUserId, IsDeleted)
VALUES (NEWID(), 'Administrator GUID', 'User GUID', 0)


 Enjoy!

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!


Tuesday, July 28, 2015

How to Enable/Disable a custom button for specific roles in Dynamics CRM??



I just came across with an urgent requirement to add a custom button in to main ribbon and then enable it if user has CSR Manager Role and disable in other case. It can be done through many ways as you can find multiple blogs posts written on it. But I found a simplest way to do it.
Here it is:
  1. Find guid of CSR Manager Role using the following code:

    function CheckUserRole(){
    //get guid of all assigned roles
    var currentUserRoles = Xrm.Page.context.getUserRoles();
    for (var i = 0; i < currentUserRoles.length; i++) {
            
    var userRoleId = currentUserRoles[i];
             //alert to each role
           
    alert(userRoleId);
    }   
    return false;
    }
  2.  In the following code, I’m checking if array contains the guid of a CSR Manager Role

    function ifCSRManager() {
    //get  guids of the roles assigned to user
    var currentUserRoles = Xrm.Page.context.getUserRoles();
    //check if it contains the guid of CSR Manager
    if (currentUserRoles.indexOf("1117f744-a5bd-e411-8c0a-005056b04acd") > -1)
     
    {    return true;}
    return false;
    }
Now, in ribbon workbench just add custom javascript role in enable rule and then tie it with command of the button.

Step # 1: Add new Enable Rule


Step # 2: Right click on the rule and then Edit Rule 

Step # 3: Select Custom Javascript Rule and click OK 

Step #4: Give Function Name and Library that contains function and click OK 
Step # 5: Add this rule to the command tied with your custom button and publish solution.