Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

how to stop calling service.create twice in cloning a record.

Posted on by 4,440

Hi 

I am cloning a record when I checked the audit history service. create is called twice,  I can see create twice in audit history. I am callin action and recordid as input and guid as output parameter to action. 

public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
if (context == null)
throw new ArgumentNullException("localContext");
tracingService.Trace("Read input entity reference");
var target = ((EntityReference)context.InputParameters["SourceRecord"]);

tracingService.Trace("Get input Entity to clone");

//return the entity of the entity reference passed in
var inputEntity = service.Retrieve(target.LogicalName, target.Id, new ColumnSet(true));

tracingService.Trace("Create output entity");
//create new entity to pass back
var outputEntity = new Entity(inputEntity.LogicalName);


//call to map input entity attirbutes to output entity attributes
outputEntity = CloneData(inputEntity, outputEntity);

tracingService.Trace("Create parent record");
//Call to create the new record
Guid clonedEntGuid = service.Create(outputEntity);


//query to get all related child records

var query = new QueryExpression("new_sam");
query.Criteria.AddCondition(new ConditionExpression("new_plrequest", ConditionOperator.Equal, inputEntity.Id));
query.ColumnSet = new ColumnSet(true);


tracingService.Trace("query child records");
var inputChildren = service.RetrieveMultiple(query);
if (!inputChildren.Entities.Any())
{
tracingService.Trace("No Child records, output parameter sent to action");
//Return the record guid as an output parameter.
context.OutputParameters["ClonedRecord"] = clonedEntGuid.ToString();
}
if(inputChildren.Entities.Any())
{
tracingService.Trace(string.Format("loop through child records count ... {0}", inputChildren.Entities.Count.ToString()));

//otherwise loop through children
foreach (var inputChild in inputChildren.Entities)
{
//create child record
var outputChild = new Entity(inputChild.LogicalName);

//call to clone input and output attributes
CloneData(inputChild, outputChild);

//relate parent and child record

outputChild.Attributes["new_plrequest"] = new EntityReference(outputEntity.LogicalName, clonedEntGuid);
//Create child record
service.Create(outputChild);
tracingService.Trace("child records created");

}

tracingService.Trace("Created child records and parent recordid sent as output parameter ");
//Return the entity reference as an output parameter.
context.OutputParameters["ClonedRecord"] = clonedEntGuid.ToString();
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(String.Format("{0} Change Request: {1} {2}",
context.MessageName,
ex.Message, ex.InnerException));
}
}

private Entity CloneData(Entity inputEntity, Entity outputEntity)
{
//loop through each key in the passed in entity and assign value to the new entity
foreach (string key in inputEntity.Attributes.Keys)
{
//Status Codes and State codes are ignored we want these to be set to default value
if (key == "statuscode" || key == "statecode") continue;

//switch statement to handle most entity types note child related entitites are ignored
switch (inputEntity.Attributes[key].GetType().ToString())
{
case "Microsoft.Xrm.Sdk.EntityReference":
outputEntity.Attributes[key] = inputEntity.GetAttributeValue<EntityReference>(key);
break;
case "Microsoft.Xrm.Sdk.OptionSetValue":
outputEntity.Attributes[key] = inputEntity.GetAttributeValue<OptionSetValue>(key);
break;
case "Microsoft.Xrm.Sdk.Money":
outputEntity.Attributes[key] = new Money(inputEntity.GetAttributeValue<Money>(key).Value);

break;
case "System.Guid":
//Don't set this, this is the Id record and a new one will be generated automatically
break;
default:
outputEntity.Attributes[key] = inputEntity.Attributes[key];
break;
}
}

return outputEntity;
}
}

*This post is locked for comments

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: how to stop calling service.create twice in cloning a record.

    Hi,

    Can you share a screenshot of audit history? It is strange to have 2 entries for Create.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans