Wednesday, September 12, 2012

Data Access using O-Data Service in CRM 2011.

Introduction

Here I am going to show you how to access the Data using O-Data service.
Here the Scenario..Lets see In Contact Form when I Select the Parent Customer the Main Phone of the Parent Customer value will be stored in Home Phone of the Contact.For that we have ro create a Function and call the Function in Onchange Event of that ParentCustomer in Contact Form.

function checkParentAccountPhone()
{
       //alert("hello");
if(Xrm.Page.data.entity.attributes.get("parentcustomerid").getValue() != null)
{
                   // if  CRM has multiple organization.                    
                   var serverUrl = location.protocol + '//' + location.host+ '/' +  Xrm.Page.context.getOrgUniqueName();;
                  //else
                   var serverUrl = location.protocol + '//' + location.host;
                   var parentid=Xrm.Page.data.entity.attributes.get("parentcustomerid").getValue()[0].id;
   alert(parentid);
   var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";

  oDataEndpointUrl += "AccountSet?$select=Telephone1&$filter=AccountId eq guid'" +  parentid + "'"
  var service = GetRequestObject();
                  Xrm.Page.getAttribute("address1_line1").setValue(oDataEndpointUrl );
  if (service != null)
 {
service.open("GET", oDataEndpointUrl, false);
       service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
service.setRequestHeader("Accept", "application/json, text/javascript, */*");
service.send(null);
var requestResults = eval('(' + service.responseText + ')').d;

if (requestResults != null && requestResults.results.length > 0)
{
                                 Xrm.Page.getAttribute("telephone2").setValue(requestResults.results[0].Telephone1);

}
}
    }
    return false;
}
function GetRequestObject()
{
    if (window.XMLHttpRequest)
        return new window.XMLHttpRequest;
    else
    {
        try
        {
            return new ActiveXObject("MSXML2.XMLHTTP.3.0");
        }
        catch (ex)
        {
            return null;
        }
    }
}

function errorDisplay(XmlHttpRequest, textStatus, errorThrown)
{
     alert(errorThrown);
}

Save above in .js File.and Call the Function checkParentAccountPhone from OnChange Event of Parent Customer.
For How to call Javascript use my below link

http://microsoftcrmkartik.blogspot.in/2012/06/javascript-in-crm-2011.html

Tuesday, September 11, 2012

Qualified Lead in CRM 2011.


SetStateRequest request = new SetStateRequest
{
               EntityMoniker = new EntityReference("lead", new Guid(leadduns["leadid"].ToString())),
               State = new OptionSetValue(1),
               Status = new OptionSetValue(3)
};
 _service.Execute(request);

Friday, September 7, 2012

How to Debug Plugins using Profiler.


One can debug CRM plug ins without connecting to CRM server or without remote debugging.

Here are the steps as in how you can use Profiler for debugging plug ins:
   1>     Connect to CRM using plugin registration tool of March SDK 2012.

   2>      Click on Install Profiler
         

   3>     You will find a new node attached to registered plugins “Plugin Profiler”.






   4>     Select a plug-in step and click Profile to enable profiling.



5>     Then start your plugin from MSCRM i.e if your plugin is on update perform  update operation and download the error file.
6>     Then in Visual Studio attach to process “plugin registeration.exe”. Add the breakpoint  from where you would like to debug.



7>     Then click on Debug in plugin registration tool.


8>     In Profile location provide the path of the error log of the plugin.

9>     In Assembly location provide the dll of the plugin from which you got error.

10>     Then select the Plugin class from Plug-in. This drop down will contains all classes present in the dll.

11>     To start debugging just click on Start Plug-in Execution.


Monday, August 27, 2012

Prevent window from closing on Save and Close.



function OnSave(event) {
         // Cancel the save operation.
         event.returnValue = false;
         return false;
}
or
function OnSave(event) {
        // Cancel the save operation.
        event.getEventArgs().preventDefault();
        return false;
}
My understanding is that event.returnValue = false; is CRM 4.0 syntax (still works in CRM 2011), while event.getEventArgs().preventDefault(); is CRM 2011 syntax

Note: The pass execution context as first parameter checkbox is very important to this process. Without it, it will not work.

Thursday, August 23, 2012

How to change the form header/footer color in MS CRM 2011 or Set Gradient color in MS CRM 2011 Header and Footer.


Lets I am creating in Account Entity.

First Create a Css called Header.css  using Web Resource in CRM.I give the Name new_Header in CRM.

.ms-crm-Form-HeaderContainer{ 
   filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#ffB3CAEC, 

endColorstr=#fff6f8faE); 
}

.ms-crm-Form-Footer{ 
   filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#ffB3CAEC, 

endColorstr=#fff6f8faE); 
   background-image: none; 
}

Now Create a Javascript using Web Resource that call this css during OnLoad Event.
I give Name new_HeaderJS in CRM.The Javascript is like this

function load_css_file(filename) { 
    var fileref = document.createElement("link") 
    fileref.setAttribute("rel", "stylesheet") 
    fileref.setAttribute("type", "text/css") 
    fileref.setAttribute("href", filename) 
    document.getElementsByTagName("head")[0].appendChild(fileref) 

}

function myFormOnLoad() { 

    load_css_file('/WebResources/new_Header'); 

}

Now Open Account Form and add the Library new_HeaderJS in Form Properties and add Event Handler and Pass a Function Name=myFormOnLoad.Then Save it and Publish it.

The Output is Like this.

Monday, August 13, 2012

Set the Default Time in Date type Attribute.

Introduction
                   Today i am going to show you the javascript of set the time in Date Field.Some times back i have requirement of set the time 7:00 when i select any Date From Date Field.If you check then if you select any date the time 00:00 show you.but i have reqirement that i have to set default time value 7:00 when i select the date.and that should only happen when you creating new record.so you have to call the Function in Onchange Event of date Field and use of the FormType for only call it on Create.


function SetDefaultTimeValue()
{
var FORM_TYPE_CREATE = 1;

var formType = Xrm.Page.ui.getFormType();
if (formType == FORM_TYPE_CREATE)
{
var att = Xrm.Page.getAttribute('new_setnextcall');//new_setnextcall is Field name
var curDate = att.getValue();
curDate.setHours(7,0,0);
att.setValue(curDate);
}
}

Now create a new record where this function and field is then and select any date you will see output like


Activate or Deactivate record in CRM 2011

We can Activate\Deactivate record. Below is the code snippet.


For Ex.


  Entity entNewLead = new Entity("lead");
  entNewLead["leadid"] = new Guid(leadduns["leadid"].ToString());
  entNewLead["statuscode"] = new OptionSetValue(2);
  entNewLead["statecode"] = new OptionSetValue(1);
  _service.Update(entNewLead);