Showing posts with label Ribbon. Show all posts
Showing posts with label Ribbon. Show all posts

Tuesday, October 9, 2012

CRM 2011-Trigger a workflow process from cutom ribbon button using javascript

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

function TriggerWorkflow(workflowGuid) {


  /*Generate Soap Body.*/
  var soapBody = "<soap:Body>" +
                 "  <Execute xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
                 "    <Request xsi:type=\'ExecuteWorkflowRequest\'>" +
                 "      <EntityId>" + Xrm.Page.data.entity.getId() + "</EntityId>" +
                 "      <WorkflowId>" + workflowGuid + "</WorkflowId>" +
                 "    </Request>" +
                 "  </Execute>" +
                 "</soap:Body>";

  /*Wrap the Soap Body in a soap:Envelope.*/
  var soapXml = "<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() +
                soapBody +
                "</soap:Envelope>";

  /* Create the XMLHTTP object for the execute method.*/
  var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  xmlhttp.open("POST", "/MSCRMservices/2007/crmservice.asmx", false);
  xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
  xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");

  /* Send the XMLHTTP object. */
  xmlhttp.send(soapXml);
alert("The workflow has been triggered successfully");

}

2.Create the workflow process
3.Create a new button using visual ribbon editor and pass parameter  as follows
 
 
Function Name:TriggerWorkflow
Library: $webresource:<created web resource name>
String parameter Value: < Id of workflow process to be launched>
Reference:

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>

Monday, October 1, 2012

CRM 2011- Set ribbon button visiblity using javascript by Custom Rule



1. Create a web resource (jscript) with the following code.

function GetRelatedRecords() {
    var baseEntityGUID = Xrm.Page.data.entity.getId();
    var query = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"
        + " <entity name='jmh_guncurrentlocation'>"
  + "  <attribute name='jmh_locationdetail' />"
  + "    <attribute name='createdon' />"
   + " <attribute name='jmh_locationrfd' />"
   + " <attribute name='jmh_locationcustomer' />"
   + " <attribute name='jmh_gun' />"
    + " <attribute name='jmh_store' />"
   + " <attribute name='jmh_guncurrentlocationid' />"
   + " <order attribute='jmh_locationdetail' descending='false' />"
   + "<filter type='and'>"
    + "  <condition attribute='statecode' operator='eq' value='0' />"
     + " <condition attribute='jmh_gun' operator='eq' uiname='45' uitype='jmh_gun' value='" + baseEntityGUID + "' />"
    + "</filter></entity></fetch>";
    var retrievedRecords = XrmServiceToolkit.Soap.Fetch(query);
    //alert(retrievedRecords.length);
    if (retrievedRecords.length != 0) {

        var request = "<request i:type='b:WhoAmIRequest' xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts' xmlns:b='http://schemas.microsoft.com/crm/2011/Contracts'>" +
                            "<a:Parameters xmlns:c='http://schemas.datacontract.org/2004/07/System.Collections.Generic' />" +
                            "<a:RequestId i:nil='true' />" +
                            "<a:RequestName>WhoAmI</a:RequestName>" +
                          "</request>";
        var resultXml = XrmServiceToolkit.Soap.Execute(request);
        var buid = resultXml.getElementsByTagName("a:Results")[0].childNodes[1].childNodes[1].text;
        for (var index = 0; index < retrievedRecords.length; index++) {

            var storeid = retrievedRecords[index].attributes['jmh_store'].id;

//            alert(buid);
//            alert(storeid);
            if (buid.toString() == storeid.toString()) {
                return true;
            }
        }
        return false;

    }
    else {
        return false;
    }
}

2.Create a button using visual ribbon editor and specify the created javascript method in cutom rule
 
Note : the above javascript to retrive related entities using Fetch in xrmservicetoolkit

Saturday, September 1, 2012

Crm 2011 - Hidding Ribbon button based on partylist value by using javascript

 function SetRibbonButtonVisiblity()
{

var approvers = Xrm.Page.getAttribute("to").getValue();
var isApprover=false;
if(approvers != null)
{
    for (var index= 0; index < approvers.length; index++)
    {
      if (approvers[index].type == 8)
       {
        var UserGUID = Xrm.Page.context.getUserId();
        if(approvers[index].id == UserGUID)
        {
        isApprover=true;
        }
       }
     }
}

if(isApprover == false)
{
var approveButton= window.top.document.getElementById("jmh_approvalrequest|NoRelationship|Form|Ntier.Form.jmh_approvalrequest.MainTab.Actions.Accept-Large");

var rejectButton= window.top.document.getElementById("jmh_approvalrequest|NoRelationship|Form|Ntier.Form.jmh_approvalrequest.MainTab.Actions.Reject-Large");


if(approveButton != null && rejectButton != null)
{
approveButton.style.display='none';
rejectButton.style.display='none';
}
}

    }

Note : you can get ribbon button id by pressing F12.
To get or set partylist
http://rajeevpentyala.wordpress.com/2012/04/02/get-and-set-partylist-fields-using-jscript/
Reference:
http://www.ahmetcankaya.com/hide-ribbon-button-in-crm-2011-by-using-javascript/

Wednesday, August 29, 2012

Crm 2011-Disbling Ribbon button based on Form field value

1.Create a new button using visual ribbon editor 
2.Open Enable rules tab ,click add button and choose ValueRule as follows

 

3.Enter the field name and value as follows

Note: For OptionSet fields,value is the OptionValue ,not the option Label 

For more details

http://howto-mscrm.blogspot.in/2011/04/how-to-series-5-how-to-use-valuerule.html