Thursday, October 4, 2012

Crm 2011 Ribbon button to open form of a record using javascript





   1.  Create a new  web resource (javascipt) with following code

  function OpenForm(recordtype) {
         var serverUrl;
         var errorMessage = "Context is not available.";
         var context;
         if (typeof GetGlobalContext != "undefined") {
             context = GetGlobalContext();
         }
         else {
             if (typeof Xrm != "undefined") {

                context = Xrm.Page.context;
             }
             else {
                alert(errorMessage);
        return;
         }
    }
      var entityId = context.getQueryStringParameters().id

   var entityEtc = context.getQueryStringParameters().etc

     var serverUrl = context.getServerUrl();
    if (serverUrl.match(/\/$/)) { serverUrl = serverUrl.substring(0, serverUrl.length - 1); }
    var recordUrl = serverUrl + "/main.aspx?";
      var params = "etn="+recordtype;
     params += "&pagetype=entityrecord";
   params += "&extraqs=" + encodeURIComponent("?_CreateFromId=" + entityId + "&_CreateFromType=" + entityEtc) ;
   var URL = recordUrl + params;
     window.open(URL, "_blank", "width=900px,height=600px,resizable=1");
     }
Note:to open already existing record use the following
 var recordUrl = serverUrl + "/main.aspx?";

    var params = "etn=" + recordtype;
    params += "&pagetype=entityrecord";

    params +="&id="+ encodeURIComponent("{" + entityId+"}");

    var URL = recordUrl + params;

Note:to pass parameter and set field values use extraqs
    params += "&extraqs=" + encodeURIComponent("?_CreateFromId=" + entityId + "&_CreateFromType=" + entityEtc + "&" + lookupfield + "=" + lookupfieldid + "&" + lookupfield + "name=" + lookupfieldname);


Note : to set regarding field 
  var serverUrl = context.getServerUrl();
    if (serverUrl.match(/\/$/)) { serverUrl = serverUrl.substring(0, serverUrl.length - 1); }
    var recordUrl = serverUrl + "/userdefined/edit.aspx?";
    var params = "contactInfo=&etc=10004&pId=";
    params += currentformid;
    params += "&pType=3";
   var URL = recordUrl + params;
    window.open(URL, "_blank", "width=900px,height=600px,resizable=1");



//get the above url  by using fiddler

Reference:

http://msdn.microsoft.com/en-us/library/gg334375.aspx

http://www.renauddumont.be/en/2011/crm-2011-ouvrir-une-fenetre-a-laide-dun-bouton-custom-et-dun-peu-de-javascript
2 2.Create a new button using visual ribbon editor and pass parameter   as follows

 Function Name: OpenForm
Library: $webresource:<created web resource name>
String parameter Value: <record type to be created>

No comments:

Post a Comment