Tuesday, November 2, 2010

Dependent Picklist Issue in CRM 2011

To implement Cascading picklist in crm 2011, Add the following piece of code to the first drop down change event


function firstPickListOnChange()
{
  var firstPickListValue = Xrm.Page.getAttribute("new_ddl1").getValue();
    var secondPickListControl = Xrm.Page.ui.controls.get("new_ddl2");
    var secondPickListOptions = secondPickListControl.getAttribute().getOptions();
    secondPickListControl.clearOptions();

switch(firstPickListValue)
  {
     case 100000001:
     secondPickListControl.addOption(secondPickListOptions [0], 0);
     secondPickListControl.addOption(secondPickListOptions [1], 1);
     secondPickListControl.addOption(secondPickListOptions [2], 2);
     break;

     case 100000002:
     secondPickListControl.addOption(secondPickListOptions [3], 0);
     secondPickListControl.addOption(secondPickListOptions [4], 1);
     secondPickListControl.addOption(secondPickListOptions [5], 2);  
     break;
    }
}

updating field/Attribute using crmservice in java script

Here is the process to update field using crmservice

var xml1 = "<?xml version='1.0' encoding='utf-8'?>"+
"<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'"+
" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"+
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"+
GenerateAuthenticationHeader()+
"<soap:Body>"+
"<Update xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
"<entity xsi:type='new_entity'>" +
"<new_field1>"+ xmlencode(data) +"</new_field1>" +
"<new_primarykeyfield>" + crmForm.ObjectId + "</new_primarykeyfield>" +
"</entity>"+
"</Update>"+
"</soap:Body>"+
"</soap:Envelope>";
var xHReq1 = new ActiveXObject("Msxml2.XMLHTTP");
xHReq1.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq1.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Update");
xHReq1.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq1.setRequestHeader("Content-Length", xml1.length);
xHReq1.send(xml1);
// Capture the result
var resultXml1 = xHReq1.responseXML;
var errorCount1 = resultXml1.selectNodes('//error').length;


//  Encoding method to replace special characters of the field data

function xmlencode(string) {
    return string.replace(/\&/g,'&'+'amp;').replace(/</g,'&'+'lt;').replace(/>/g,'&'+'gt;').replace(/\'/g,'&'+'apos;').replace(/\"/g,'&'+'quot;');
}

Microsoft Dynamics CRM Pre-Filtering for reports




CRM Pre-Filtering is a very useful option that can be enabled on CRM reports to make them context sensitive and to allow the report to be filtered using the Advanced Find functionality.  Although this is a great feature, it is often an area that is not fully understood which can lead to someone encountering unexpected results.

How is it Enabled?

Automatic Prefiltering (CRMAF_)

There are 2 ways to enable the CRM Pre-Filtering functionality. The easiest option is the CRMAF_ method which simply requires aliasing the filtered views with a name that starts with “CRMAF_”.   A query such as “Select name from FilteredAccount” can simply be changed to “Select name from FilteredAccount as CRMAF_Account”.  Aliasing the Filtered View with a prefix of CRMAF_ will allow CRM to recognize that you would like to enable this entity for pre-filtering.

When you enable the CRM Pre-filtering functionality using the CRMAF_ method, CRM will take a query such as the following and modify it when it is uploaded into CRM:

SELECT name, accountnumber
FROM FilteredAccount as CRMAF_Account

Becomes:

SELECT name, accountnumber
FROM (@P1) as CRMAF_Account

Then CRM will pass a query to the P1 parameter depending on how the report is being filtered. For example: If you are running the report from the Reports area and use the Pre-filtering functionality to filter to only show Accounts that are Active, the resulting query would be something like:

SELECT name, accountnumber
FROM (select FilteredAccount.* from FilteredAccount where statecode = 0) as CRMAF_Account

If you are within a specific Account and run the report, the resulting query would be something like:

SELECT name, accountnumber
FROM (select FilteredAccount.* from FilteredAccount where AccountId = '<CurrentAccountId>') as CRMAF_Account

When you are looking at a list of Accounts with 3 selected and choose the option to run the report against the selected records, the resulting query would be something like:

SELECT name, accountnumber
FROM (select FilteredAccount.* from FilteredAccount where AccountId in ('<1stAccountId>', '<2ndAccountId>', '<3rdAccountId>') as CRMAF_Account

How to find roles of the logged-in user in MS CRM 4.0 using JavaScript

Function returns all the roles of the Current Logged-In User.

function GetCurrentUserRoles()
{
 var xml = "" +
 "" +
 "<soap:envelope xmlns:soap="\"http://schemas.xmlsoap.org/soap/envelope/\"" xmlns:xsi="\"http://www.w3.org/2001/XMLSchema-instance\"" xmlns:xsd="\"http://www.w3.org/2001/XMLSchema\"">" +
 GenerateAuthenticationHeader() +
 " <soap:body>" +
 " <retrievemultiple xmlns="\"http://schemas.microsoft.com/crm/2007/WebServices\"">" +
 " <query xmlns:q1="\"http://schemas.microsoft.com/crm/2006/Query\"" xsi:type="\"Q1:QueryExpression\"">" +
 " <q1:entityname>role</q1:entityname>" +
 " <q1:columnset xsi:type="\"Q1:ColumnSet\"">" +
 " <q1:attributes>" +
 " <q1:attribute>name</q1:attribute>" +
 " </q1:attributes>" +
 " </q1:columnset>" +
 " <q1:distinct>false</q1:distinct>" +
 " <q1:linkentities>" +
 " <q1:linkentity>" +
 " <q1:linkfromattributename>roleid</q1:linkfromattributename>" +
 " <q1:linkfromentityname>role</q1:linkfromentityname>" +
 " <q1:linktoentityname>systemuserroles</q1:linktoentityname>" +
 " <q1:linktoattributename>roleid</q1:linktoattributename>" +
 " <q1:joinoperator>Inner</q1:joinoperator>" +
 " <q1:linkentities>" +
 " <q1:linkentity>" +
 " <q1:linkfromattributename>systemuserid</q1:linkfromattributename>" +
 " <q1:linkfromentityname>systemuserroles</q1:linkfromentityname>" +
 " <q1:linktoentityname>systemuser</q1:linktoentityname>" +
 " <q1:linktoattributename>systemuserid</q1:linktoattributename>" +
 " <q1:joinoperator>Inner</q1:joinoperator>" +
 " <q1:linkcriteria>" +
 " <q1:filteroperator>And</q1:filteroperator>" +
 " <q1:conditions>" +
 " <q1:condition>" +
 " <q1:attributename>systemuserid</q1:attributename>" +
 " <q1:operator>EqualUserId</q1:operator>" +
 " </q1:condition>" +
 " </q1:conditions>" +
 " </q1:linkcriteria>" +
 " </q1:linkentity>" +
 " </q1:linkentities>" +
 " </q1:linkentity>" +
 " </q1:linkentities>" +
 " </query>" +
 " </retrievemultiple>" +
 " </soap:body>" +
 "</soap:envelope>" +
 "";

 var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

 xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
 xmlHttpRequest.setRequestHeader("SOAPAction"," http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
 xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
 xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
 xmlHttpRequest.send(xml);

 var resultXml = xmlHttpRequest.responseXML;
 return(resultXml);
}

Function which calls the above function and search for indivisual role:

function UserHasRole(roleName)
{
 //get Current User Roles, oXml is an object
 var oXml = GetCurrentUserRoles();
 if(oXml != null)
 {
  //select the node text
  var roles = oXml.selectNodes("//BusinessEntity/q1:name");
  if(roles != null)
  {
   for( i = 0; i < roles.length; i++)
   {
    if(roles[i].text == roleName)
    {
     //return true if user has this role
     return true;
    }
   }
  }
 }
 //otherwise return false
 return false;                                              
}

How to use UserHasRole function:

if(UserHasRole(“System Administrator”) == true)
{
           alert(‘User has admin role’);
}
else
{
           alert(‘User does not have admin role’);
}