Clone A Record

Clone A Case Record :

Cloning is one of the methods which can reduce the time spent on data entry, particularly if the same case record data being entered is similar.

Cloning or Record Copying allows you to create a duplicate record with the same values as the parent record with just one click.

Solution as follows:

Ribbon Button will be visible on the  case form, Once Record is Saved , then Button will be visibled. If he click on the Button open a dialog, with message “Do You want to clone a a case Record” If he click Ok , It will Cloning the record.

flow – Button – click- Js pass Id to – Action InputParameter- Plugin Trigger – Create Case record.

Ribbon workbench Button as follows :-

11.JPG

Add the Button Command As Follows :-

Action – web resource and Function Name -“CallAction

12.JPG

Display Rule As Follows : Form StateRule

13.JPG

Js Code For Ribbon WorkBench Action :-

 function CallAction() {
 debugger;
 var formtype = Xrm.Page.ui.getFormType();
 if(formtype!=1){
 debugger;
 Xrm.Utility.confirmDialog("Do You want copy the case Record?", 
 function () {
  var recordId = Xrm.Page.data.entity.getId().replace("{", "").replace("}", "");
 // action schemaname "new_a" and passing record id to the action parameter(first and second are parametrs) and 
 //under function BuildActionRequestXml seee the two parameters am using only one parameter and passing id to the parameter
 InvokeAction("new_CloneaCaseGuid", recordId);
 },
 function () {});
 
 } 
}
function InvokeAction(actionName,recordId) {
 debugger;
 var xmlHTTPRequest = new XMLHttpRequest();
 var results = true;
 xmlHTTPRequest.open("POST", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/web", false);
 xmlHTTPRequest.setRequestHeader("Accept", "application/xml, text/xml");
 xmlHTTPRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
 xmlHTTPRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");


 xmlHTTPRequest.onreadystatechange = function () {
 debugger;
 if (this.readyState == 4) {
 if (this.status == 200) {
 //if required write code here.
 //alert("success");
 }
 else {
 results = false;
 }
 }
 };
 xmlHTTPRequest.send(BuildActionRequestXml(actionName,recordId));
 
}


function BuildActionRequestXml(actionName,recordId) {
 debugger;
 var requestXml = "<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>" +
 "<s:Body>" +
 "<Execute xmlns='http://schemas.microsoft.com/xrm/2011/Contracts/Services' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>" +
 "<request xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts'>" +
 "<a:Parameters xmlns:b='http://schemas.datacontract.org/2004/07/System.Collections.Generic'>" +
 "<a:KeyValuePairOfstringanyType>" +
 "<b:key>Guid</b:key>" +
 "<b:value i:type='c:string' xmlns:c='http://www.w3.org/2001/XMLSchema'>" + recordId + "</b:value>" +
 "</a:KeyValuePairOfstringanyType>" +
 //"<a:KeyValuePairOfstringanyType>" +
 //"<b:key>second</b:key>" +
 //"<b:value i:type=\"c:string\" xmlns:c=\"http://www.w3.org/2001/XMLSchema\">" + + "</b:value>" +
 //"</a:KeyValuePairOfstringanyType>" +
 "</a:Parameters>" +
 "<a:RequestId i:nil='true' />" +
 "<a:RequestName>" + actionName + "</a:RequestName>" +
 "</request>" +
 "</Execute>" +
 "</s:Body>" +
 "</s:Envelope>";
 return requestXml;
}

 

Next Global Action Create Input Parameter as Guid with Type String :

Screenshot (116).png

Through Javascript , Button Onclick, we are passing the case record Id to the this InputParameter Guid. Once Action Called Then Plugin will Trigger to create the Case Record .

Plugin Step As Follows : Message Name = Action Name “new_CloneaCaseGuid14.JPG

Plugin Code To create the copy case Record as Follows:

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

namespace Clone_a_Record
{
 public class Class1 : IPlugin
 {
 public void Execute(IServiceProvider serviceProvider)
 {
 IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
 ITracingService tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
 IOrganizationServiceFactory servicefactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
 IOrganizationService service = servicefactory.CreateOrganizationService(context.UserId);
 tracing.Trace(context.InputParameters.Contains("Guid").ToString());
 if(context.InputParameters.Contains("Guid") && context.InputParameters["Guid"] is string)
 {
 tracing.Trace("IFLoopEnter");
 Guid CaseID = new Guid(context.InputParameters["Guid"].ToString());
 Entity cases = service.Retrieve("incident", CaseID, new ColumnSet(true));

 //Initialises new record from an existing record
 InitializeFromRequest copyCases = new InitializeFromRequest();
 copyCases.TargetEntityName = "incident";
 copyCases.EntityMoniker = cases.ToEntityReference();
 copyCases.TargetFieldType = TargetFieldType.All;
 InitializeFromResponse response = (InitializeFromResponse)service.Execute(copyCases);
 Entity newCase =(Entity) response.Results["Entity"];
 service.Create(newCase);
 tracing.Trace("Completed");

 }
 }
 }
}

15.JPG

It Will doesn’t clones the child Records.

16.JPG
That’s It It will Clones the Record;

Enjoy 🙂

 

 

Advertisements

Calling Actions from Plugin in CRM 2016

Calling Actions from Plugin in CRM 2016:

Interesting features Updated  in CRM  is Actions. Actions are the Custom messages which can be created for any entity or globally to perform business operations like create/update/Assign etc. Looking on  first time we get a feeling that it is very similar to Workflow but hold on it’s very different from workflow  or Plugins.

Can be associated with single entity or globally.
Actions support Arguments – Input and Output Arguments.
Can be only called through web service calls Like  JavaScript/Plugins/Web through API calls.
Always runs under the context of the calling user.

 Input Arguments:

These are the parameters which need to be passed during action call. These can be Optional/Mandatory. For Mandatory argument it is necessary to provide some value while calling this action.

Output Arguments:

These are the parameters which can be returned when the action is called. These can be Optional/Mandatory.

1

 

Example:

Let me create a simple Action on Enquiry entity which has one Input Argument: ProjectName [string]

Flow: Settings > Processes > select Category as Actions > In primary entity > Global.                    Input Parameter- InputData.

new_gb” is the name of the Action which should be referred while calling this through API.

 

Calling Actions from Plugins

Call this Action from our Plugin. I have a Plugin which fires on Post Create of account  am passing the Input Argument Inputdata Parameter before calling this Action. Once this is called It creates a new contact record.

Flow – Account Created – Plugin Triggered(Create Message-post operation)-Plugin triggered then action will be called passing account name to input data- Action step will create the contact record.

// Calling the Action - new_EnquiryCreateProject
OrganizationRequest req = new OrganizationRequest("new_gb");
req["inputdata"] = account.Attributes["name"].toString(); or "Accountname";
for Global Action No need of Target.
//req["Target"] = new EntityReference("contact", contact.Id);

//execute the request
OrganizationResponse response = service.Execute(req);