RSS

Category Archives: CRM WorkFlow

How to Create Records through Dialog Process and do fields mapping with the Parent Record

Introduction

As we know, in MS CRM, we always try to find solution of any requirement through Out Of The Box. It doesn’t mean that, we can’t do it by writing code. But Out of The Box gives better performance. So, let’s discuss how to create records through Dialog Process and do fields mapping with the Parent Record.

Background

According to one of my project requirement, we had to create new record(s) on custom button click, from one existing record and that will be done only through Dialog Process. Before record create, in background, some of data from parent record should automatically get populated to new record’s fields (i.e. Fields mapping). I have found a solution by doing some research.

Steps

Let’s explore step by step.

Here I will open Dialog Process from existing Contact record and create Contact and Opportunity records through dialog sequentially.

Step 1: Create a Process of Dialog Category

Create Dialog Process

Step 2: Add a step of “Page” and under which add “Prompt and Response” step, then click “Set properties” to to create control for record inside dialog

See the following picture.
Add Step Page

Step 3: Mapping Dialog Fields with Contact Fields

After creating all fields required, add a step of “Create record” from Add Steps DropDown. Then choose the record type and click “Set Properties“. Here we will be able to do fields mapping using both Parent data and Dialog Fields data.
The Red colored box on the right side is showing all fields contains in Dialog. Map those fields with corresponding fields in Contact.
Mapping Dialog Fields with Contact Fields

Step 4: Populating Parent Fields data to New Record

The Red Colored box showing fields mapping between Parent record and New record. Same way choose any field and map that with field of Contact
Populating Parent Fields data to new record

Step 5: To set Parent record with one Lookup field of new record

Follow the below Picture.
Set Parent record with Lookup field of new record

Step 6: To add a created record’s link in the end of Dialog

Add a new page and again add a step of “Prompt and Response” under it. Then click Set properties and follow the below image. After that activate the Dialog Process.
Add created record link in the end of Dialog

Step 7: Script to open created Dialog on Button Click

  • Copy the Id of the new Dialog Process from location bar and then do the following
  • Create a Web Resource of Script type using below script
  • Add the Web Resource to the Button Click Event
function LaunchModalDialog()
{
    var dialogId = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"; //id of dialog process
    var recordId = Xrm.Page.data.entity.getId();
    var typeName = Xrm.Page.data.entity.getEntityName();
    recordId = recordId.toString();
    recordId = recordId.replace("{", "");
    recordId = recordId.replace("}", "");

    var serverUrl = Xrm.page.getServerUrl();
    
    // Load modal
    var serverUri = serverUrl +'/cs/dialog/rundialog.aspx';

    var mypath = serverUri +'?DialogId=%7b' + dialogId.toUpperCase() +'%7d&EntityName=' + typeName + '&ObjectId=%7b' + recordId +'%7d';

    // First item from selected contacts only
    window.showModalDialog(mypath);

    // Reload form.
    window.location.reload(true);
}

By Sukanta Mangal
Software Engineer @Team DynamicsCRM.
Mindfire Solutions

Advertisements
 
 

Tags: , , , , , , , , , , , , , , , ,

How to Bulk Delete Workflow Jobs in Microsoft Dynamics CRM

Introduction

Whenever a Workflow is triggered, a System Job record is created in Microsoft Dynamics CRM and this will be a burden on CRM database. So, eventually Microsoft Dynamics CRM Administrator needs to clean-up the existing Workflow Jobs. Here, we will explore a very quick and easy way to bulk delete all the existing completed System Jobs created by Workflows in Microsoft Dynamics CRM.

 Steps

Image

Login in to Microsoft CRM >> Settings >> Data Management >> Bulk Record Deletion >> click New to start the Bulk Deletion Wizard.

Click Next on the Wizard >> Look for: “System Jobs” >> set the following criteria:

  • Status Equals Completed
  • System Job Type Equals Workflow

Image

Click Next to set the options such as Start Date and Time, set Number of Days to run this recursively & to send a notification when job is finished.

Image

Click Next and Submit it.

By Abhisek Sriramka
Senior Software Engineer @Team DynamicsCRM.
Mindfire Solutions

 
Leave a comment

Posted by on December 24, 2013 in CRM WorkFlow

 

Tags: , , , , , , , , , , , , , , , , , , ,

Custom WorkFlow to Send Email with PDF formatted Report as Attachment

Introduction

In some cases, we have to send Email to customers with a Report in Pdf format as an attachment.

Background

Once I had the same requirement. To do this, we have to follow three steps.

  1. First to retrieve the Report,
  2. Second to convert the Report to Pdf format and
  3. Last to send an Email with the Pdf as attachment.

Code

To achieve this, I have written a Custom Workflow.
Here is the code snippet to get it done. This code is for CRM OnPremise only. I have not tried in CRM Online.

using System;
using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Crm.Sdk.Messages;
using CustomWorkflows.ReportService; //Have to add the Reporting Service as Web Reference

namespace SendReport
{
    public class SendReport : CodeActivity
    {
        protected override void Execute(CodeActivityContext executionContext)
        {
            try
            {
                IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
                IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                Guid accountId = context.PrimaryEntityId;
                byte[] result = null;

                CustomWorkflows.ReportService.ReportExecutionService rs = new ReportExecutionService();

                // Credential to connect with CRM
                rs.Credentials = new System.Net.NetworkCredential(username, password, domain);

                // Setting the URL of the Reporting Server
                rs.Url = "http://test10:80/ReportServer/ReportExecution2005.asmx";

                string reportPath = "/Test_MSCRM/TestReport";
                // Specify the report path from the reporting server
                // Note: To get the report name, report must be published for the external use.
                // To do this edit the report from CRM and publish it for external use.
                // After publishing it for external use report name will be visible in the reporting server instead of the report id.

                string format = "PDF";
                string historyID = null;
                string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
                string encoding;
                string mimeType;
                string extension;
                Warning[] warnings = null;
                string[] streamIDs = null;

                ExecutionInfo execInfo = new ExecutionInfo();
                ExecutionHeader execHeader = new ExecutionHeader();
                rs.ExecutionHeaderValue = execHeader;
                execInfo = rs.LoadReport(reportPath, historyID);

                String SessionId = rs.ExecutionHeaderValue.ExecutionID;

                result = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);

                //Create email activity
                Entity email = new Entity();
                email.LogicalName = "email";
                email.Attributes.Add("regardingobjectid", new EntityReference("account", accountId);

                //Creating EntityReference for from, to and cc. Need to be changed according to your requirement
                EntityReference from = new EntityReference("systemuser", senderUserId);
                EntityReference to = new EntityReference("contact", recieverUserId);
                EntityReference cc = new EntityReference("contact", recieverCCUserId);

                //Creating party list
                Entity fromParty = new Entity("activityparty");
                fromParty.Attributes.Add("partyid", from);
                Entity toParty = new Entity("activityparty");
                toParty.Attributes.Add("partyid", to);
                Entity ccParty = new Entity("activityparty");
                ccParty.Attributes.Add("partyid", cc);

                EntityCollection collFromParty = new EntityCollection();
                collFromParty.EntityName = "systemuser";
                collFromParty.Entities.Add(fromParty);

                EntityCollection collToParty = new EntityCollection();
                collToParty.EntityName = "contact";
                collToParty.Entities.Add(toParty);

                EntityCollection collccParty = new EntityCollection();
                collccParty.EntityName = "contact";
                collccParty.Entities.Add(ccParty);

                // Adding from, to and cc to the email
                email.Attributes.Add("from", collFromParty);
                email.Attributes.Add("to", collToParty);
                email.Attributes.Add("cc", collccParty);

                email.Attributes.Add("subject", "Here goes subject message.. : ");
                email.Attributes.Add("description", "Here goes description text..");
                Guid emailID = service.Create(email); // Create the email

                // Attaching Pdf Report
                int NextActorID = new int();
                RetrieveEntityRequest request = new RetrieveEntityRequest();
                request.LogicalName = "email";
                RetrieveEntityResponse response = (RetrieveEntityResponse)service.Execute(request);
                int objecttypecode = response.EntityMetadata.ObjectTypeCode.Value;

                Entity attachment = new Entity("activitymimeattachment");
                attachment.Attributes.Add("subject", "Report");
                attachment.Attributes.Add("filename", "Report.pdf");
                attachment.Attributes.Add("body", Convert.ToBase64String(result));
                attachment.Attributes.Add("filesize", Convert.ToInt32(result.Length.ToString()));
                attachment.Attributes.Add("mimetype", "text/plain");
                attachment.Attributes.Add("attachmentnumber", NextActorID);
                attachment.Attributes.Add("objectid", new EntityReference("email", new Guid(email.Id.ToString())));
                attachment.Attributes.Add("objecttypecode", objecttypecode);
                service.Create(attachment); //Create the attachment

                // Sending email
                SendEmailRequest reqSendEmail = new SendEmailRequest();
                reqSendEmail.EmailId = emailID;
                reqSendEmail.TrackingToken = "";
                reqSendEmail.IssueSend = true;
                SendEmailResponse res = (SendEmailResponse)service.Execute(reqSendEmail);
            }
            catch (Exception err)
            {
                // Throw error message
            }
        }
    }
}

By Ranjan Parhi
Senior Software Engineer @Team DynamicsCRM.
Mindfire Solutions

 
9 Comments

Posted by on November 20, 2013 in CRM WorkFlow

 

Tags: , , , , , , , , , , , , , , , ,

 
%d bloggers like this: