function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
somasundaram s 7somasundaram s 7 

Create and update a record using Rest API

hi guys,

i will try to create and update a record in SFDC via API. I have acheived to create a Record .But I can't able to update  the Record via API . 

Here is my sample code :
        public void UpdatedRecordforAccount()
        {
            //0017F00000b9EnPQAU
            HttpClient client = new HttpClient();
            string requestMessage = "{\"Name\":\"Express Logistics and Transport Sample\"}";


            HttpContent content = new StringContent(requestMessage, Encoding.UTF8, "application/json");            
            string uri = InstanceUrl + API_ENDPOINT + "sobjects/Account/0017F00000b9EnPQAU" + "?_HttpMethod=PATCH";

            //create request message associated with POST verb
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri);

            //add token to header
            request.Headers.Add("Authorization", "Bearer " + AuthToken);

            //return xml to the caller
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            request.Content = content;
            var response = client.SendAsync(request).Result;
            Console.WriteLine(response.Content.ReadAsStringAsync().Result);
            Console.ReadLine();
        }

I have already tried these two method (http put,post) and _Patch concept. If any have a solution .please share to me 
VamsiVamsi
Hi,

Inorder to update a record get the record based on the ID in context and update the fields requried and then initate update on it.
somasundaram s 7somasundaram s 7
Hi ,

I have done .here is the my sample code .


        public void UpdatedRecordforAccount()
        {         
            #region searching code
            string uri = InstanceUrl+ API_ENDPOINT + "sobjects/User/0057F000002uhKvQAI" + "?_HttpMethod=PATCH";

            HttpClient client = new HttpClient();
            string requestMessage = "{\"username\":\"saravanan@....com\" ,\"email\":\"saravanan@....com\",\"lastname\":\"SaravananKDF\",\"alias\":\"SRAKDF\",\"timezonesidkey\":\"America/Los_Angeles\",\"EmailEncodingKey\":\"ISO-8859-1\",\"ProfileId\":\"00e7F000001oCBZQA2\",\"LanguageLocaleKey\":\"en_US\",\"LocaleSidKey\":\"en_US\"}";
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,uri);
            request.Headers.Add("Authorization", "Bearer " + AuthToken);
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpContent content = new StringContent(requestMessage, Encoding.UTF8, "application/json");
            request.Content = content;
            var response = client.SendAsync(request).Result;
            Console.WriteLine(response.Content.ReadAsStringAsync().Result);
            Console.ReadLine();
}

Thanks ,
Somu