Friday 18 December 2015

Retrieve Stage Name in the field for Business Process flow

Retrieve Stage Name in the field for Business Process flow


     Recently I have a requirement that we need to show the stage name in the field. I solved it. So here I wanna share how I have done this.

     For achieving this we need to create one workflow.

Step1: Create one field where you wanna get stage name. Let's say field name is "Stage".And insert it in to form.

Step2: Create a real-time workflow. Select "Starts when" as Record Created and Record Fields change like the following screenshot.


In the record, fields change the option to click on the select button and add following fields.


Step 3: Add an update step in the workflow. and click on "Set Properties" Button and click on your field(Stage). Then in your right side navigation bar, you will get one option like Look For. In that select following options.



Step4: Click add and press Ok. Save and Activate workflow. Then go to your entity UI. Then you will get a result like following.



Hope it helps you.
Thank you.





Tuesday 15 December 2015

Retrieve the status of an Entity Record through plugin

Retrieve the status of an Entity Record through plugin


For writing any plugin you should add following references


Microsoft.Crm.Sdk.Proxy
Microsoft.Xrm.Sdk
System.Runtime.Serialization

After that write plugin steps and Register Image like
 if (context.PreEntityImages.Contains("PreImage"))
    {
         Entity myPreImage = (Entity)context.PreEntityImages["PreImage"];
    }


Then write a logic like this 

var status = myPreImage.GetAttributeValue<OptionSetValue>("statecode").Value;

Here the Various States and Status reasons I mentioned here.


So If Record is giving value "0" Means it is active or inactive.
So Final Code is


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;

namespace ActiveEntity
{
    public class Class1 : IPlugin
    {
        public void Execute(IServiceProvider ServiceProvider)
        {
            try
            {
                IOrganizationServiceFactory ServiceFactory = (IOrganizationServiceFactory)ServiceProvider.GetService(typeof(IOrganizationServiceFactory));

                IExecutionContext context = (IExecutionContext)ServiceProvider.GetService(typeof(IExecutionContext));

                IOrganizationService Service = ServiceFactory.CreateOrganizationService(context.UserId);

                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {

                    if (context.PreEntityImages.Contains("PreImage"))
                    {
                        Entity entity = (Entity)context.InputParameters["Target"];
                        Entity myPreImage = (Entity)context.PreEntityImages["PreImage"];
                        if (myPreImage.Attributes.Contains("statecode"))
                        {
                            var status = myPreImage.GetAttributeValue<OptionSetValue>("statecode").Value;

                            if (status == 0)
                            {
                                //Your Custom Logic
                            }
                        }
                    }
                }
            }

            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
    }

}

After Build this plugin make sure Signing the plugin then Register Assembly and Step.
After that Register an Image.

Then you will get a result.

Hope it helps you,
Thank you,
Shiva Ram

Thursday 15 October 2015

Retrieve the field value from one entity using another entity page.

Retrieve the field value from one entity using another entity page.

Hi everyone here I explain how to get a value of a field of another form using GUID.

Here I have a contact lookup in account Form. Based on that selected contact GUID I wanna get credit limit value. For that follow these steps.


Step 1:First try to get ContactId
var ContactId = Xrm.Page.getAttribute("contactname").getValue()[0].id;

Step 2: Go to Advance Finder and Give your custom logic and download fetchXML

<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
  <entity name='contact'>
    <attribute name='contactid' />
      <filter type='and'>
      <condition attribute='contactid' operator='eq' value='+ContactId+' />
      <condition attribute='creditlimit'>
    </filter>
  </entity>
</fetch>

Step 3: Get the result and check the value of Credit Limit

var Results = XrmServiceToolkit.Soap.Fetch(FetchXml);
var CreditLimit = Results[0].attributes["creditlimit"].value;

So Finally your code is like this


function getContactId() {
    var ContactId = Xrm.Page.getAttribute("contactname").getValue()[0].id;
    var FetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
  "<entity name='contact'>" +
    "<attribute name='creditlimit' />" +
      "<filter type='and'>" +
      "<condition attribute='contactid' operator='eq' value='" + ContactId + "' />" +
       "</filter>" +
  "</entity>" +
"</fetch>"
    var Results = XrmServiceToolkit.Soap.Fetch(FetchXml);
    if (Results.length > 0) {
        if (Results[0] != null) {
            var CreditLimit = Results[0].attributes["creditlimit"].value;
            //Write your custom logic here
            }
        }
    }
}

Tuesday 22 September 2015

Get subgrid data from the database using javascript

Get subgrid data from the database using javascript


                Previously I shared how to retrieve Subgrid data from UI. But in CRM the subgrid values may vary from user to user. So if we want all values from that particular entity then we should get the data from the database. For that, we need to generate FetchXML.
Here I am getting all Competitors(Competitor is a subgrid) from related opportunity entity.

Step1:   We need to add javascript files from "XrmServiceToolkit".
Here you can download XrmServiceToolkit: https://xrmservicetoolkit.codeplex.com/



Step2:   Now go to Sales --> Opportunity Entity--> Advance Finder-->Write your own query here. I attached sample query for getting all competitors related to the current entity.



Step3:   Download the fetchXML.

Step4:   Change the format of fetchXML.

Step5:   Start writing Javascript. Here I add my custom javascript code.

function RetrieveSubgridRecord() {
       var Xmlsample =
    "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
    "<entity name='competitor'>" +
    "<attribute name='name' />" +
   "<attribute name='websiteurl' />" +
   "<attribute name='competitorid' />" +
   "<order attribute='name' descending='false' />" +
   "<link-entity name='opportunitycompetitors' from='competitorid' to='competitorid' visible='false' intersect='true'>" +
   "<link-entity name='opportunity' from='opportunityid' to='opportunityid' alias='am'>" +
    "<filter type='and'>" +
      "<condition attribute='opportunityid' operator='eq' uitype='opportunity' value='" + Xrm.Page.data.entity.getId() + "' />" +        //Xrm.Page.data.entity.getId() gives current entity ID
    "</filter>" +
  "</link-entity>" +
"</link-entity>" +
"</entity>" +
"</fetch>"
    var Records = XrmServiceToolkit.Soap.Fetch(Xmlsample);
alert(Records.length);
//Here you can write your own logic based on your requirement.

 Thanks for reading this.Hope it helps you.

Wednesday 9 September 2015

Get Subgrid data using javascript


Get Subgrid data using javascript

Forgetting subgrid values from your entity, you can use the following javascript

function RetrieveSubgridRecord() {
   if (document.getElementById("Competitors")) {     //Competitors is logical name of my grid
        var grid = document.getElementById("Competitors").control;
        for (var rowNo = 0; rowNo <= grid.GetRecordsFromInnerGrid().length; rowNo++)
            for (var cellNo = 0; cellNo <= grid.GetRecordsFromInnerGrid()[rowNo][1].length; cellNo++)
                  alert(grid.GetRecordsFromInnerGrid()[rowNo][3].cells[cellNo].outerText);
  }
}

This is the way to getting subgrid values from local entity. From getting subgrid data from database you need to download FetchXML from your advance find...
I will upload that soon

Hope this helps you



Wednesday 2 September 2015

Important javascript Functions for CRM

Important javascript Functions for CRM


Getting the value of optionset


Xrm.Page.getAttribute("field logical name").getValue();

Getting the Text of optionset

Xrm.Page.getAttribute("fieldlogical name").getText();

Getting the GUID of Lookup


Xrm.Page.getAttribute("fieldlogical name").getValue()[0].id;

Getting the Text of Lookup


Xrm.Page.getAttribute("fieldlogical name ").getValue()[0].name;

Getting the value of single line text


Xrm.Page.getAttribute("fieldlogical name").getValue();

Setting value to the field


Xrm.Page.getAttribute("fieldlogicalname").setValue("Value we want to set");

Getting Entity Name


Xrm.Page.data.entity.getEntityName();

For refresh the Form


Location.Reload();

Save the page

Xrm.Page.data.entity.save();