Using ExecuteTransaction in Dynamics 365 C#

Leave a comment

ExecuteTransaction can be used in Dynamics 365 to execute requests as a transaction.

Here’s some sample code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Xrm.Tooling.Connector;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using System.ServiceModel;

namespace Carl.Dynamics365ExecuteTransaction
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var connectionString = @"AuthType = Office365; Url = https://yourorg.crm.dynamics.com/;Username=your@email.com;Password=yourpassword";
                CrmServiceClient conn = new CrmServiceClient(connectionString);

                IOrganizationService service;
                service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;

                var request = new ExecuteTransactionRequest()
                {
                    Requests = new OrganizationRequestCollection()
                };

                for (int i = 0; i < 5; i++)
                {
                    Entity contact = new Entity("contact");
                    contact["firstname"] = "Bob " + i.ToString();
                    contact["lastname"] = "Smith";

                    var createRequest = new CreateRequest()
                    {
                        Target = contact
                    };
                    request.Requests.Add(createRequest);
                }

                Console.WriteLine("Before execute");
                var response = (ExecuteTransactionResponse)service.Execute(request);
                Console.WriteLine("After execute");

                foreach (var r in response.Responses)
                {
                    var createResponse = (CreateResponse)r;
                    Console.WriteLine("Contact: " + createResponse.id);
                }

                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
        }
    }
}

 

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

Leave a Reply

Your email address will not be published. Required fields are marked *