How to delete referential relationship between two entities in C# console application
*This post is locked for comments
How to delete referential relationship between two entities in C# console application
*This post is locked for comments
Hi,
i want to delete record which have referential relationship. To delete parent and child record from CRM entity
Hi DMat,
Try using below code.
#region Execute Multiple with Results
ExecuteTransactionRequest TransactionRequestDelete = null;
// Create an ExecuteTransactionRequest object.
TransactionRequestDelete = new ExecuteTransactionRequest()
{
// Create an empty organization request collection.
Requests = new OrganizationRequestCollection(),
//ReturnResponses = true
};
#endregion
EntityReference connection = new EntityReference() { LogicalName = "LogicalName", Id = Guid };
DeleteRequest deleteRequest = new DeleteRequest { Target = connection };
TransactionRequestDelete.Requests.Add(deleteRequest);
try
{
// Execute all the requests in the request collection using a single web method call.
var responseForCreateRecords = (ExecuteTransactionResponse)service.Execute(TransactionRequestDelete);
}
catch (FaultException<OrganizationServiceFault> ex)
{
if (ex.Detail.ErrorDetails.Contains("MaxBatchSize"))
{
int maxBatchSize = Convert.ToInt32(ex.Detail.ErrorDetails["MaxBatchSize"]);
if (maxBatchSize < TransactionRequestDelete.Requests.Count)
{
// Here you could reduce the size of your request collection and re-submit the ExecuteTransaction request.
// For this sample, that only issues a few requests per batch, we will just print out some info. However,
// this code will never be executed because the default max batch size is 1000.
Console.WriteLine("The input request collection contains %0 requests, which exceeds the maximum allowed (%1)",
TransactionRequestDelete.Requests.Count, maxBatchSize);
}
}
Console.WriteLine("\t\t\t\t\t\t =================================");
Console.WriteLine("\t\t\t\t\t\t\t Exception : " + "Create request failed for the project{0} and the reason being: {1}",
((ExecuteTransactionFault)(ex.Detail)).FaultedRequestIndex + 1, ex.Detail.Message);
throw;
}
Thanks,
Shahbaaz
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,280 Super User 2024 Season 2
Martin Dráb 230,214 Most Valuable Professional
nmaenpaa 101,156