Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Thursday, 9 February 2012

Create,Update,Retrieve, RetrieveMultiple Records in CRM 2011 with Javascript REST endpoint

Hi,

For who are new with Microsoft CRM 2011, this is a first lession that you should know: How to create, update, retrieve and delete a record of an entity in CRM by using javascript. These basic operations help you a lot in developing your CRM by using the client-side language Javascript.

There are two ways to achieve the results:
1. Using directly the REST Endpoint method like in SDK sample in this link: http://msdn.microsoft.com/en-us/library/gg334427.aspx --> for me, this way is complicated, because you have to bring the whole REST Endpoint function to your code, which makes your code like a mess.

2. Download the REST Endpoint method from SDK  and use it like a library for your javascript code.

Now are steps how to do the second way:

- Step 1: Prepare the following required js:
       + JavascriptRESTDataOperations --> from SDK: SampleCode\JS\RESTEnpoint\JavascriptRESTDataOperations
       + jquery1.7.1.js --> from http://jquery.com/
       + json2.js --> from http://www.json.org/json2.js

- Step 2: Creating your javascript as following:

//Create record
function Create_CRMObject()
{ var CRMObject = new Object();
  CRMObject.Id = "";
  //createRecord function is in jsoperation.js
  createRecord(CRMObject,"your entity set name", createCRMObjectCompleted,errorCreate) 
  // entity set name, for example: AccountSet, ContactSet, etc
}
createCRMObjectCompleted = function (data, textStatus, XmlHttpRequest)
{ var newCRMObjectCreated = data["d"];  
}
errorCreate = function (XMLHttpRequest, textStatus, errorThrown) 
{ alert("Error");
}

//Update record
function Update_CRMObject()
{ 
    new CRMObject = new Object();
 var CRMObject_Id = "put the GUID here";
 var CRMObject."Attribute" = "put any attribute value here"
 //updateRecord function is in jsoperation.js
 updateRecord(CRMObject_Id,CRMObject,"you entity set name", updateCRMObjectCompleted,errorUpdate); // entity set name, for example: AccountSet, ContactSet, etc
}
updateCRMObjectCompleted = function (data, textStatus, XmlHttpRequest)
{ alert("your update is successful");  
}
errorUpdate = function (XMLHttpRequest, textStatus, errorThrown) 
{ alert("Error");
}

//Delete Record
function Delete_CRMObject()
{  var CRMObject_Id =" put the GUID here");
   //deleteRecord function is in jsoperation.js
   deleteRecord(CRMObject_Id,"your entity set name", deleteCRMObjectCompleted, errorDelete);   
}
deleteCRMObjectCompleted = function (data, textStatus, XmlHttpRequest)
{}
errorDelete = function (XMLHttpRequest, textStatus, errorThrown) 
{ alert("Error");
}

//Retrieve Record
function retrieve_CRMObject()
{ //retrieveRecord function is in jsoperation.js
   retrieveRecord("put your record GUID here","put your entity set name", retrieveCRMObjectCompleted, errorRetrieve);
// entity set name, for example: AccountSet, ContactSet, etc
}
retrieveCRMObjectCompleted = function (data, textStatus, XmlHttpRequest)
{ var newCRMObjectRetrieve = data["d"];
  //now you could retrieve any field belongs to your record
}
errorRetrieve = function (data, textStatus, XmlHttpRequest) 
{ alert("Error"); 
}

//Retrieve Multiple Records
function retrievemultiple_CRMObject() 
{  retrieveMultiple("your entity set name","", retrievemultipleCRMObjectCompleted, errorRetrieveMultiple);
// entity set name, for example: AccountSet, ContactSet, etc
}
retrievemultipleCRMObjectCompleted = function (data, textStatus, XmlHttpRequest)
{ var CRMObject = data;
  if (CRMObject.length !==0)
   { for (i=0, i<CRMObject.length, i++)
      { CRMObject[0].id = ... //retrieve any field you want
   }   
   }
  else
  { alert("no record");
  }
}
errorRetrieveMultiple = function (data, textStatus, XmlHttpRequest) 
{ alert("Error"); 
}

Save all of above code in CRMObject.js

- Step 3: Add all 4 above js files in your entity's form. Put the function name for onload or onsave event. Then you could do anything you want now !

I hope it helps. Please email to linhhk87@gmail.com if you have any further question to implement it !




    

       

Wednesday, 1 February 2012

Using window.showModalDialog in IE9 - CRM Customization

Hi everyone ( I hope someone takes a look at this blog really..),

Here is a very small tip for you guys to control how your window dialog appear in CRM.

The scenario here is you would like to create a new button in CRM ribbon. When you click the button, a window dialog will be pop up.

 The solution is writing a javascript function to show up the window dialog using window.showModalDialog method. All about this method could be found in this link: http://msdn.microsoft.com/en-us/library/ie/ms536759(v=vs.85).aspx

However, your window dialog appeareance depends on kind of browser you use (IE, Firefox, Chrome, etc..). Now i'm using IE9, then figure out one problem with it.

As you see the following javascript:

 window.showModalDialog(url, null, "dialogHeight:600px;dialogWidth:800px;center:yes; resizable:1;maximize:1;minimize:1;status:no;scroll:no" );

In IE9, all the features defined in the javascript above (underlined) could not be fired. Then you have to enbale the Compatibility View in IE9 in order to run it correctly.

To enable Compatibility View in IE9, the following steps:
- Alt + T to see the toolbars
- Go to Tools tab, you will see Compatibility View Setting


 - Then adding your website (here is your CRM) and select Display all website as shown in the below pic



So now, run your javascript again and see your window dialog appear correctly as your code !

I hope it'll be useful.



Tuesday, 31 January 2012

Creating embedded grid between parent & child entities in CRM form

Last time, I spent 2 days to figure out how to create an embedded-grid showing "child" entities which are related to a parent entity as the following picture:


As you see, in Campaign Activity entity (which is a parent entity), there are many Email entities (which are related entities). So now, I would like to create a navigation when clicked will show you related entities like that.

The key thing needed to understand is: actually, when you create a relationship bet when 2 entities (such as: Campaign Activity & Email), there is an exsiting "tabset" which stores all related entities created. The navigation link just let us to that tabset only. So the task now is: building url to the tabset.

Here is the javascript how to build the navigation link to get the embedded-grid:
function passURLtoNavigation()
{ //Gets navigation item
   var items = Xrm.Page.ui.navigation.items.get();
   for (var i in items)
    {
       var item = items[i];
      var label = item.getLabel();
      if (label == "Email Created")
          { var navId = item.getId();
            var navItem = document.getElementById(navId);
            if (navItem != null && navItem != undefined)
              { var serverUrl = location.protocol + '//' + location.host + '/' +     Xrm.Page.context.getOrgUniqueName();
          var oId = Xrm.Page.data.entity.getId();
          var oType = Xrm.Page.context.getQueryStringParameters().etc;
          var security = "852023";
          var tabSet = "//here is your relationship name";       
                   //building the URL for the navigation link
                   var navUrl = encodeURI(serverUrl + "/userdefined/areas.aspx?oId=" + oId + "&oType=" + oType + "&security=" + security + "&tabSet=" + tabSet);
                   navItem.onclick = function () { loadIsvArea(Mscrm.CrmUri.create(navUrl), false ); };
}
}
}
}

So now, you have the subgrid as shown in the first picture. I hope it will be helpful.

Friday, 20 January 2012

CRM 2011 Customization - Decrypt default Javascript files in CRM Server

 So it's this, the first blog !!!

Two weeks ago, during the time writing a Javascript to customize a CRM button, I found out the way how to decrypt the default Javascript files in CRM server which are might be helpful for you to refer and understand how CRM functions were built.

What I wanted is tracing what they did when building the Campaign Activity entity. Then I exported the default solution and looked what they did with the entity. Opening the customization file, you can see in the picture, there are 3 javascript files applied for it:



As you see, the "CamPaignActivity_main_system_library" was located somewhere in the server and defined in Javascript by "$webresource". It took me several hours to find where actually the file was. Then, the tip is very simple, CRM itself has a database which store all things in the system inside. And in the database, there is an view named "WebResource" where you could look for any webresource belongs to your CRM system. So what you need to do is: opening SQL Server --> CRM Database --> put the query to find WebResource View.

Look at the following pic to refer:


So now I find out the content of the javascript file ! But this content is encrypted. Actually, they just converted them into a different kind of string. Then you need to make some code to decrypt in order to take the real javascript content. The C# code below:

 protected void Button1_Click(object sender, EventArgs e)
        {       
            byte[] binaryData = System.Convert.FromBase64String("//here is your javascript encripted content//");
            string x = System.Text.Encoding.ASCII.GetString(binaryData);              
            Response.Write(x);      

        }

So now, you had the javascript file with decrypted content. I hope it's helpful ;)