Dynamics 365 Retrieve and Update using C#

2 Comments

In this post we will look at how to retrieve a record using Retrieve Multiple, then update the record.

The example is, we are working with a customer and we need to find their record in an app using their first name, last name and email address. We then want to update their record with a new phone number.

Create a new Console App:

Add XrmTooling:

Add the code:

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Tooling.Connector;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

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

                ConditionExpression condition1 = new ConditionExpression();
                condition1.AttributeName = "lastname";
                condition1.Operator = ConditionOperator.Equal;
                condition1.Values.Add("Smith");

                ConditionExpression condition2 = new ConditionExpression();
                condition2.AttributeName = "firstname";
                condition2.Operator = ConditionOperator.Equal;
                condition2.Values.Add("Bob");

                ConditionExpression condition3 = new ConditionExpression();
                condition3.AttributeName = "emailaddress1";
                condition3.Operator = ConditionOperator.Equal;
                condition3.Values.Add("test@test.com");

                FilterExpression filter1 = new FilterExpression();
                filter1.Conditions.Add(condition1);
                filter1.Conditions.Add(condition2);
                filter1.Conditions.Add(condition3);

                QueryExpression query = new QueryExpression("contact");
                query.ColumnSet.AddColumns("firstname", "lastname", "emailaddress1");
                query.Criteria.AddFilter(filter1);

                EntityCollection result = service.RetrieveMultiple(query);
                foreach (var c in result.Entities)
                {
                    Console.WriteLine("Found contact: " + c.Attributes["firstname"] + " " + c.Attributes["lastname"]);

                    // Update with new phone number
                    Entity contact = new Entity("contact");
                    contact = service.Retrieve(contact.LogicalName, c.Id, new ColumnSet(true));
                    contact["telephone1"] = "111-222-1111";
                    service.Update(contact);
                    Console.WriteLine("Updated contact");
                }
            }
            catch(Exception ex)
            {
                // Handle exception
            }
        }
    }
}

After we run this, the record will be retrieved and updated:

 

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

 

2 Responses to Dynamics 365 Retrieve and Update using C#

  1. Hi,
    Thanks for your very useful article, I’ve tried to customize it to get an entity and update an attribute but it returns an error “object reference not set to an instance of an object” at EntityCollection result = service.RetrieveMultiple(query)

    The change I have is just renaming the entity from contact to incident (Case).

    Would you please offer any idea?

Leave a Reply

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