Wednesday, July 29, 2015

Actions in CRM2013/15

  • Navigate to Settings->Process->New.
  • Use following details and click on OK :
    1. Process name: ActionDemo
    2. Category: Action
    3. Entity: None(global)
    4. Type: New blank process
  • Add a Input parameter of Entity type for ParentAccount record like below:
entityparameter
  • Add another input parameter of type EntityReference to get Salesperson
salesperson
Now let’s say we want to use these parameters to create our contact record, so follow below steps to create contact record using these parameters:
  • Click on Add Step and select Create Record step under drop down
contacreate
  • Select Contact under Create drop down like below and click on Set Properties.
  • Now we will be using our Entity object input parameter to fetching different property and will be setting in contact record
  • Select the field that you want to set, for example we want to set Account Name, so click on Account Name field and click on OK after setting following options from Form Assistant like below:
Assitent
  • Similar we can select other fields like Email, Mobile, Firstname, Last name from account entity object like belowcotnact
  • Now select the Sales person field and select out second parameter from Form assistant like below (Note: We have created a custom lookup for Sales Person field in Contact entity) and click on Save and Close
salesperson

Now we need to activate our action and we can call it using server side code, client side code and using other Workflow and dialogs as well (new feature in CRM 2015). We ca use following code call our action:
//we have written method to get CRM service object
IOrganizationService service = connection.GetCRMService();
//retrieve account record using that we want to create contact
Entity accountObj = service.Retrieve(“account”, new Guid(“699D9F32-9B10-E511-80FD-C4346BAD3138″), new ColumnSet(true));

//Create organization Request object and pass action name as parameter
OrganizationRequest Req = new OrganizationRequest(“new_actiondemo”);
Req[“ParentAccount”] = accountObj; //passing our first parameter
Req[“Salesperson”] = new EntityReference(“systemuser”, new Guid(“90322292-C8EC-4A72-8B32-B79DEF7DD31C”));
//use execute method to call action
OrganizationResponse Respons = service.Execute(Req);

After execution of the code we can see in CRM contact record is created by action like below:
resultaction
So in above example we show if we need to pass multiple parameter we can use Entity object instead of passing field value one by one and entity reference is used to for passing a single entity reference instance.

Tuesday, July 28, 2015

Create, Update , Delete Record using Javascript in CRM2011/13/15 using Soap Query

Soap Query to Create/Update/Delete using Javascript in CRM2011/13/15

Creating Contact entity record

// Prepare values for the new contact.
var firstname = "Navish";
var lastname = "Jain";
var donotbulkemail = "true";
var address1_stateorprovince = "CHD";
var address1_postalcode = "160036";
var address1_line1 = "#1429/2";
var address1_city = "Chandigarh";
var authenticationHeader = GenerateAuthenticationHeader();

// Prepare the SOAP message.
var xml = "<?xml version='1.0' encoding='utf-8'?>" +
"<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'>" +
authenticationHeader +
"<soap:Body>" +
"<Create xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
"<entity xsi:type='contact'>" +
"<address1_city>" + address1_city + "</address1_city>" +
"<address1_line1>" + address1_line1 + "</address1_line1>" +
"<address1_postalcode>" + address1_postalcode + "</address1_postalcode>" +
"<address1_stateorprovince>" + address1_stateorprovince + "</address1_stateorprovince>" +
"<donotbulkemail>" + donotbulkemail + "</donotbulkemail>" +
"<firstname>" + firstname + "</firstname>" +
"<lastname>" + lastname + "</lastname>" +
"</entity>" +
"</Create>" +
"</soap:Body>" +
"</soap:Envelope>";
// Prepare the xmlHttpObject and send the request.
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Create");
xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq.setRequestHeader("Content-Length", xml.length);
xHReq.send(xml);
// Capture the result
var resultXml = xHReq.responseXML;

// Check for errors.
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0) {
    var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
    alert(msg);
}

XML with a CreateResponse element that returns the ID for the record created


<?xml version="1.0" encoding="utf-8"?>
<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">
 <soap:Body>
  <CreateResponse xmlns="http://schemas.microsoft.com/crm/2007/WebServices">
   <CreateResult>368c8b1b-851c-dd11-ad3a-0003ff9ee217</CreateResult>
  </CreateResponse>
 </soap:Body>
</soap:Envelope>




Updating Contact Record

// Prepare variables for updating a contact.
var contactId = "89a7e456-8098-bb33-b345-0003gg9ff218";
var newAddressLine1 = "#1429/2";
var authenticationHeader = GenerateAuthenticationHeader();

// Prepare the SOAP message.
var xml = "<?xml version='1.0' encoding='utf-8'?>" +
"<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'>" +
authenticationHeader +
"<soap:Body>" +
"<Update xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
"<entity xsi:type='contact'>" +
"<address1_line1>" + newAddressLine1 + "</address1_line1>" +
"<contactid>" + contactId + "</contactid>" +
"</entity>" +
"</Update>" +
"</soap:Body>" +
"</soap:Envelope>";
// Prepare the xmlHttpObject and send the request.
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Update");
xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq.setRequestHeader("Content-Length", xml.length);
xHReq.send(xml);
// Capture the result
var resultXml = xHReq.responseXML;

// Check for errors.
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0) {
    var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
    alert(msg);
}

XML that returns a UpdateResponse


<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">
 <soap:Body>
  <UpdateResponse xmlns="http://schemas.microsoft.com/crm/2007/WebServices" />
 </soap:Body>
</soap:Envelope>


Deleting Contact record

// Identify the contact to delete.
var contactid = "89a7e456-8098-bb33-b345-0003gg9ff218";
var authenticationHeader = GenerateAuthenticationHeader();

// Prepare the SOAP message.
var xml = "<?xml version='1.0' encoding='utf-8'?>"+
"<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'>"+
authenticationHeader+
"<soap:Body>"+
"<Delete xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
"<entityName>contact</entityName>"+
"<id>"+contactid+"</id>"+
"</Delete>"+
"</soap:Body>"+
"</soap:Envelope>";
// Prepare the xmlHttpObject and send the request,
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Delete");
xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq.setRequestHeader("Content-Length", xml.length);
xHReq.send(xml);
// Capture the result,
var resultXml = xHReq.responseXML;

// Check for errors,
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0)
{
 var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
 alert(msg);
}

XML that returns a DeleteResponse


<?xml version="1.0" encoding="utf-8"?>
<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">
 <soap:Body>
  <DeleteResponse xmlns="http://schemas.microsoft.com/crm/2007/WebServices" />
 </soap:Body>
</soap:Envelope>

Monday, July 27, 2015

Assign Team Using Create/Update Operation

Hi Team,

Please Use Below Code to Assign Team using Create/Update operation in Plugins

//Assign a record to a team
 private void AssignRecord(Entity TargetEntity, Guid TargetRecordID, Guid OwningTeamID, IOrganizationService orgService)
 {
     try
     {
         // Create the Request Object and Set the Request Object's Properties
         AssignRequest assign = new AssignRequest
         {
             Assignee = new EntityReference("team", OwningTeamID),
             Target = new EntityReference(TargetEntity.LogicalName, TargetRecordID)
         };
 
         // Execute the Request
         orgService.Execute(assign);
     }
     catch (Exception ex)
     {
         throw new Exception("An error occured while assinging Team to a record." + ex.Message);
     }
 }

Assign Team in CRM Plugin

Hi All,

Please find the Code for the Assign Team Login in C#

SnapShot:



Plugin :

protected void ExecutePreTaskAssign(LocalPluginContext localContext)
{
    if (localContext == null)
    {
        throw new ArgumentNullException("localContext");
    }

    IPluginExecutionContext context = localContext.PluginExecutionContext;
    IOrganizationService service = localContext.OrganizationService;

    try
    {
        //Check if context contains the "Target" parameter and that it 
          is an EntityReference
        if (context.InputParameters.Contains("Target") && 
                     context.InputParameters["Target"is EntityReference)
        {
            EntityReference targetEntity =    
                     (EntityReference)context.InputParameters["Target"];
            //Check if the entity type is Task
            if (targetEntity.LogicalName != "task")
            { return; }

            //Check if context contains the "Assignee" parameter
            if (context.InputParameters.Contains("Assignee"))
            {
                EntityReference assigneeRef = 
                       (EntityReference)context.InputParameters["Assignee"];
                //Check if the "Assignee" is a team
                if (assigneeRef.LogicalName == "team")
                {
                    Team team;
                    //Retrieve the team with all properties
                    team = service.Retrieve(assigneeRef.LogicalName, 
                        assigneeRef.Id, new ColumnSet(true)).ToEntity<Team>();

                    //Put your logic here based on the retrieved Team informations
                }
            }
        }
    }

    catch (Exception e)
    {
        throw new InvalidPluginExecutionException("An error occured for 
                Assign plugin " + e.Message + e.InnerException);
    }
}

Add/Remove Members to Team

Hi all,

Please find the Plugin Code to Add and Remove Members to a Team using C#

//Add Members to Team

public static void AddMembersToTeam(Guid teamId, Guid[] membersId, IOrganizationService service)
        {
            // Create the AddMembersTeamRequest object.
            AddMembersTeamRequest addRequest = new AddMembersTeamRequest();

            // Set the AddMembersTeamRequest TeamID property to the object ID of
            // an existing team.
            addRequest.TeamId = teamId;

            // Set the AddMembersTeamRequest MemberIds property to an
            // array of GUIDs that contains the object IDs of one or more system users.
            addRequest.MemberIds = membersId;

            // Execute the request.
            service.Execute(addRequest);
        }

//RemoveMembers to Team

        public static void RemoveMembersFromTeam(Guid teamId, Guid[] membersId, IOrganizationService service)
        {
            // Create the AddMembersTeamRequest object.
            RemoveMembersTeamRequest addRequest = new RemoveMembersTeamRequest();

            // Set the AddMembersTeamRequest TeamID property to the object ID of
            // an existing team.
            addRequest.TeamId = teamId;

            // Set the AddMembersTeamRequest MemberIds property to an
            // array of GUIDs that contains the object IDs of one or more system users.
            addRequest.MemberIds = membersId;

            // Execute the request.
            service.Execute(addRequest);
        }

Friday, July 24, 2015

Filter Home Page Views using Plugins

Hi all,

As per the CRM, Views we can filtered using Filtering criteria to filter it, some of the conditions we cannot achieve in Filtering Criteria, we can achieve this using Plugins.

Create a Step with the message name as RetrieveMultiple


Copy paste the below code and alter the fields based on your conditon. It will trigger on Page Refresh and View Changes

 if (localContext.PluginExecutionContext.InputParameters.Contains("Query") &&
                        localContext.PluginExecutionContext.InputParameters["Query"] is QueryExpression)
                {

                    QueryExpression query = localContext.PluginExecutionContext.InputParameters["Query"] as QueryExpression;
                    query.ColumnSet.Columns.Add("createdon");
                    query.ColumnSet.Columns.Add("createdby");
                    ConditionExpression condition = new ConditionExpression("name", ConditionOperator.Equal,
                        localContext.PluginExecutionContext.UserId.ToString());
                    ConditionExpression condition1 = new ConditionExpression("createdby", ConditionOperator.Equal,
                       localContext.PluginExecutionContext.UserId.ToString());
                    FilterExpression filter = new FilterExpression();
                    filter.AddCondition(condition);
                    filter.AddCondition(condition1);
                    filter.AddCondition(condition2);
                    query.Criteria.Filters.Add(filter);
                    filter.FilterOperator = LogicalOperator.Or;
                }

Thursday, July 23, 2015

Business Rules with Form Type


Create a Business Rules with Form Type

              --> while Creating a new record Form type wont be available so you can check the created on date to proceed in Business Rules

Here is the rule for create form.

How to Run Microsoft Flow for Every one Month

Introduction: In this Blog we will see how to Run Microsoft Flow for Every one Month. Implementation Steps:   1. Navigate to  https://make.p...