Friday, August 31, 2012

Privileges Problems!!!


Dear All,
If you are facing with the below error
 You don’t have enough privileges to access the Microsoft Dynamics CRM object or perform the requested operation. For more information ,Contact your Microsoft Dynamics CRM administrator.

Check the Append and AppendTo privileges for that particular entity set to Org level.

Limit the No. of records per page in MS CRM


if the user want to see only 10 records per view in the entire CRM
You can set this in the Personal options by setting the Records Per Page:
Though records per page can set minimum 25 and  maximum  250.
if some one wants to restrict to 10 records per page??
Then run the below SQL against the Organization you desired to do so.
update UserSettings  set PagingLimit=10 where SystemUserId='User GUID'

Friday, August 24, 2012

How to Speed up the Performance of MS CRM Forms

  • Add your CRM site to the list of trusted sites
  • Disable Form Assistant
  • When you have scores of data in the CRM database, the Form Assistant loads the data during the form loads. Shutting this functionality off could greatly boost the loading time of the form.
  • Disable Antivirus scan for CRM site
  • One of the major factors that affect CRM's performance is the Antivirus scan. Disabling this, the difference in load time of the form becomes quite noticible.
  • Use a higher version on IE. (IE7 or higher)

Thursday, August 23, 2012

How to save a record in CRM 2011 using javascript


In CRM 2011, you could save an entity record using javascript.
There are 3 types of save possible using javascript in CRM 2011.

1. Equivalent to 'SAVE' button found on the entity form
Xrm.Page.data.entity.save();

2. Equivalent to 'SAVE AND NEW' button found on the entity form
Xrm.Page.data.entity.save('saveandnew');

3. Equivalent to 'SAVE AND CLOSE'  button found on the entity form
Xrm.Page.data.entity.save('saveandclose');

Wednesday, August 22, 2012

Checking condition for last 15 days date using JavaScript in MS CRM


var currentDate = new Date();
var dateOfContact = crmForm.all.new_submissiondate.DataValue;
if(dateOfContact != null)
{
  var pastMaxDate = new Date();

  currentDate.setHours(0);
  currentDate.setMinutes(0);
  currentDate.setSeconds(0); 
  currentDate.setMilliseconds(0);
 
  pastMaxDate.setDate(currentDate.getDate()-14);
  pastMaxDate.setHours(0);
  pastMaxDate.setMinutes(0);
  pastMaxDate.setSeconds(0);
  pastMaxDate.setMilliseconds(0);

  //Checking condition for last 15 days
  if( !((dateOfContact <= currentDate) && (dateOfContact >= pastMaxDate)) )
  {
    crmForm.all.new_submissiondate.DataValue = null;
    alert("Valid date range is from "+ pastMaxDate.toLocaleDateString() + " to till today.");
  }
}

Tuesday, August 21, 2012

Microsoft Dynamics CRM E-mail Router


Email router will not send any mails unless we configure Outlook.
We cannot install CRM Email in Windows Server 2008 32-bit editions, Windows Server 2003, Windows Vista, and Windows XP editions are not supported for installing and running Microsoft Dynamics CRM E-mail Router or E-mail Router Configuration Manager.

Please refer the below link for further assistance

Friday, August 17, 2012

Check the User Security Role in CRM 2011/4.0 using JScript


if(UserHasRole("System Administrator")==true)
{
alert("System Administrator");
}
if(UserHasRole("SalesHead")==true)
{
alert("SalesHead");
}

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;  

  
function GetCurrentUserRoles()  
{  
 var xml = "" +  
 "<?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>" +  
 " <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);  
}


Go to Entitiesà Select an entityà Forms [Open the main Form]à select Form Properties à the below screen will appearsà click on Add à create new àcopy the above code and publish



Note: No need to add any Function in Event Handlers. 
we can use the same code in CRM 4.0 Form OnLoad.

Thursday, August 16, 2012

How to get/retreive security roles from database in MSCRM 4.0

SELECT
SU.FullName,
SU.DomainName,
Role.Name,
SUR.RoleId,SUR.SystemUserId
FROM Role 
JOIN SystemUserRoles SUR ON (Role.RoleId = SUR.RoleId)
JOIN SystemUser SU ON (SUR.SystemUserId = SU.SystemUserId)
WHERE
(Role.DeletionStateCode = 0) AND
(SU.IsDisabled = 0) AND
(SU.DeletionStateCode = 0)

Jscript for Microsoft Dynamics CRM 2011


1.  Get the GUID value of a lookup field:
Note: this example reads and pops the GUID of the primary contact on the Account form
function AlertGUID() {
var primaryContactGUID = Xrm.Page.data.entity.attributes.get("primarycontactid").getValue()[0].id;
alert(primaryContactGUID);
}
2.  Get the Text value of a lookup field:
Note: this example reads and pops the name of the primary contact on the Account form
function AlertText() {
var primaryContactName = Xrm.Page.data.entity.attributes.get("primarycontactid").getValue()[0].name;
alert(primaryContactName);
}
3.  Get the value of a text field:
Note: this example reads and pops the value of the Main Phone (telephone1) field on the Account form
function AlertTextField() {
var MainPhone = Xrm.Page.data.entity.attributes.get("telephone1").getValue();
alert(MainPhone);
}
4.  Get the database value of an Option Set field:
Note: this example reads and pops the value of the Address Type (address1_addresstypecode) field on the Account form
function AlertOptionSetDatabaseValue() {
var AddressType = Xrm.Page.data.entity.attributes.get("address1_addresstypecode");
AddressTypeDisplayValue = AddressType.getValue();
if (AddressTypeDisplayValue != null) {
alert(AddressTypeDisplayValue);
}
}
5.  Get the text value of an Option Set field:
Note: this example reads and pops the value of the Address Type (address1_addresstypecode) field on the Account form
function AlertOptionSetDisplayValue() {
var AddressType = Xrm.Page.data.entity.attributes.get("address1_addresstypecode");
AddressTypeDisplayValue = AddressType.getSelectedOption().text;
if (AddressTypeDisplayValue != null) {
alert(AddressTypeDisplayValue);
}
}
6.  Get the database value of a Bit field:
// example GetBitValue("telephone1");
function GetBitValue(fieldname) {
return Xrm.Page.data.entity.attributes.get(fieldname).getValue();
}
7.  Get the value of a Date field:
returns a value like: Wed Nov 30 17:04:06 UTC+0800 2011

and reflects the users time zone set under personal options
// example GetDate("createdon");
function GetDate(fieldname) {
return Xrm.Page.data.entity.attributes.get(fieldname).getValue();
}
8.  Get the day, month and year parts from a Date field:
// This function takes the fieldname of a date field as input and returns a DD-MM-YYYY value
// Note: the day, month and year variables are numbers
function FormatDate(fieldname) {
var d = Xrm.Page.data.entity.attributes.get(fieldname).getValue();
if (d != null) {
var curr_date = d.getDate();
var curr_month = d.getMonth();
curr_month++;  // getMonth() considers Jan month 0, need to add 1
var curr_year = d.getFullYear();
return curr_date + "-" + curr_month + "-" + curr_year;
}
else return null;
}
      
// An example where the above function is called
alert(FormatDate("new_date2"));
9.  Set the value of a string field:
Note: this example sets the Account Name field on the Account Form to “ABC”
function SetStringField() {
var Name = Xrm.Page.data.entity.attributes.get("name");
Name.setValue("ABC");
}
10.  Set the value of an Option Set (pick list) field:
Note: this example sets the Address Type field on the Account Form to “Bill To”, which corresponds to a database value of “1”
function SetOptionSetField() {
var AddressType = Xrm.Page.data.entity.attributes.get("address1_addresstypecode");
AddressType.setValue(1);
}
11.  Set a Date field / Default a Date field to Today:
//set date field to now (works on date and date time fields)
Xrm.Page.data.entity.attributes.get("new_date1").setValue(new Date());
12.  Set a Date field to 7 days from now:
function SetDateField() {
var today = new Date();
var futureDate = new Date(today.setDate(today.getDate() + 7));
Xrm.Page.data.entity.attributes.get("new_date2").setValue(futureDate);
Xrm.Page.data.entity.attributes.get("new_date2").setSubmitMode("always");// Save the Disabled Field
}
13.  Set the Time portion of a Date Field:
// This is a function you can call to set the time portion of a date field
function SetTime(attributeName, hour, minute) {
var attribute = Xrm.Page.getAttribute(attributeName);
if (attribute.getValue() == null) {
attribute.setValue(new Date());
}
attribute.setValue(attribute.getValue().setHours(hour, minute, 0));
}
09          
// Here's an example where I use the function to default the time to 8:30am
SetTime('new_date2', 8, 30);
14.  Set the value of a Lookup field:
Note: here I am providing a reusable function…
// Set the value of a lookup field
function SetLookupValue(fieldName, id, name, entityType) {
if (fieldName != null) {
var lookupValue = new Array();
lookupValue[0] = new Object();
lookupValue[0].id = id;
lookupValue[0].name = name;
lookupValue[0].entityType = entityType;
Xrm.Page.getAttribute(fieldName).setValue(lookupValue);
}
}
Here’s an example of how to call the function (I retrieve the details of one lookup field and then call the above function to populate another lookup field):
var ExistingCase = Xrm.Page.data.entity.attributes.get("new_existingcase");
if (ExistingCase.getValue() != null) {
var ExistingCaseGUID = ExistingCase.getValue()[0].id;
var ExistingCaseName = ExistingCase.getValue()[0].name;
                 SetLookupValue("regardingobjectid", ExistingCaseGUID, ExistingCaseName,"incident");
              }
15.  Split a Full Name into First Name and Last Name fields:
function PopulateNameFields() {
var ContactName = Xrm.Page.data.entity.attributes.get("customerid").getValue()[0].name;
var mySplitResult = ContactName.split(" ");
var fName = mySplitResult[0];
var lName = mySplitResult[1];
Xrm.Page.data.entity.attributes.get("firstname").setValue(fName);
Xrm.Page.data.entity.attributes.get("lastname").setValue(lName);
}

16.  Refresh subgrid using Javascript:

Xrm.Page.ui.controls.get("SubGridName").refresh();


Tuesday, August 14, 2012

How to find the Last week dates from Monday to Sunday in SQL Server


declare @Start  datetime 
declare @End datetime 
set @Start=DATEADD(wk, DATEDIFF(wk,0,GETDATE()-7), 0)
set @End=DATEADD(wk, DATEDIFF(wk,0,GETDATE()-7), 6)
select @Start as Monday
select @End as Sunday

Friday, August 10, 2012

How to find a Note is attached to Account or not using javascript


var test= crmForm.all.new_test;
var notecount=0;
    if (crmForm.ObjectId != null) {
        var ObjectId= crmForm.ObjectId;
        var xml = "<?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>" +
"<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>annotation</q1:EntityName>" +
"<q1:ColumnSet xsi:type='q1:ColumnSet'>" +
"<q1:Attributes>" +
"<q1:Attribute>annotationid</q1:Attribute>" +
"</q1:Attributes>" +
"</q1:ColumnSet>" +
"<q1:Distinct>false</q1:Distinct>" +
"<q1:Criteria>" +
"<q1:FilterOperator>And</q1:FilterOperator>" +
"<q1:Conditions>" +
"<q1:Condition>" +
"<q1:AttributeName>objectid</q1:AttributeName>" +
"<q1:Operator>Equal</q1:Operator>" +
"<q1:Values>" +
"<q1:Value xsi:type='xsd:string'>" + ObjectId+ "</q1:Value>" +
"</q1:Values>" +
"</q1:Condition>" +
"</q1:Conditions>" +
"</q1:Criteria>" +
"</query>" +
"</RetrieveMultiple>" +
"</soap:Body>" +
"</soap:Envelope>";

        // Prepare the xmlHttpObject and send the request.
        var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
        xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
        xHReq.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
        xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xHReq.setRequestHeader("Content-Length", xml.length);
        xHReq.send(xml);

        var resultXml = xHReq.responseXML;
   
        var errorCount = resultXml.selectNodes('//error').length;
        if (errorCount != 0) {
            var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
            //alert(msg);
        }
     
        else {
            var results = resultXml.getElementsByTagName('BusinessEntity');
            if (results.length > 0) {
            notecount=results.length;
  }
        }
    }

if(test=='abc')
{
if(notecount>0)
{
return;
}
else
{
alert("Please attach a Note");
event.returnValue = false;
}
}