Showing posts with label update record in Model driven app. Show all posts
Showing posts with label update record in Model driven app. Show all posts

Thursday, 10 August 2023

update a record in dataverse using console application

  public static void DataverseConnector()

        {

            try

            {

                string conn = @"AuthType=OAuth;Username=user@org.onmicrosoft.com;Password=password;Url=https://orgapp.crm.dynamics.com;TokenCacheStorePath=c:\MyTokenCache2;LoginPrompt=Auto";

                using (ServiceClient serviceClient = new ServiceClient(conn))

                {

                    if (serviceClient.IsReady)

                    {

                        //Use Entity class with entity logical name

                        var account = new Entity("account");

                        account.Id = new Guid("a17f700a-6d37-ee11-bdf4-000d3a0aab51");

                        // set attribute values

                        // string primary name

                        account["name"] = "Contoso2";

                        // Boolean (Two option)

                        account["creditonhold"] = false;

                        // DateTime

                        account["lastonholdtime"] = new DateTime(2017, 1, 1);

                        // Double

                        account["address1_latitude"] = 47.642311;

                        account["address1_longitude"] = -122.136841;

                        // Int

                        account["numberofemployees"] = 500;

                        // Money

                        account["revenue"] = new Money(new decimal(5000000.00));

                        // Picklist (Option set)

                        account["accountcategorycode"] = new OptionSetValue(1); //Preferred customer

                        //Create the account

                       serviceClient.Update(account);

                      //  Console.WriteLine("Record ID is {0}.", accountid);

                    }

                    else

                    {

                        Console.WriteLine("A web service connection was not established.");

                    }

                }


                // Pause the console so it does not close.

                Console.WriteLine("Press any key to exit.");

                Console.ReadLine();

            }

            catch (Exception ex)

            {

                Console.Write(ex.Message);

            }

            

        }