Exemple #1
0
        public async Task <string> PostAccountAsync(dynamic accountData)
        {
            SalesforceAccessToken token = await GetAccessTokenAsync();

            string result = "[]";

            if (token != null)
            {
                using (HttpClient queryClient = new HttpClient())
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                    string restQuery = token.serviceUrl + "/services/data/v39.0/sobjects/Account/";

                    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, restQuery);
                    //add token to header
                    request.Headers.Add("Authorization", "Bearer " + token.oauthToken);
                    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    HttpContent httpContent = new StringContent(accountData.ToString(), Encoding.UTF8);
                    httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    request.Content = httpContent;

                    //call endpoint async
                    HttpResponseMessage response = await queryClient.SendAsync(request);

                    result = await response.Content.ReadAsStringAsync();
                }
            }

            return(result);
        }
Exemple #2
0
        public async Task <string> GetAccountAsync(string accountName)
        {
            SalesforceAccessToken token = await GetAccessTokenAsync();

            string result = "[]";

            if (token != null)
            {
                using (HttpClient queryClient = new HttpClient())
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                    //QUERY: Retrieve records of type "account"
                    //string restQuery = serviceUrl + "/services/data/v25.0/sobjects/Account";
                    //QUERY: retrieve a specific account
                    //string restQuery = serviceUrl + "/services/data/v25.0/sobjects/Account/001E000000N1H1O";
                    //QUERY: Perform a SELECT operation
                    string restQuery = token.serviceUrl + "/services/data/v25.0/query?q=SELECT+name+from+Account";

                    if (!String.IsNullOrWhiteSpace(accountName))
                    {
                        restQuery += "+where+name+=+'" + accountName + "'";
                    }

                    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, restQuery);

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

                    //return JSON to the caller
                    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    //call endpoint async
                    HttpResponseMessage response = await queryClient.SendAsync(request);

                    result = await response.Content.ReadAsStringAsync();

                    dynamic obj = JsonConvert.DeserializeObject(result);;

                    if (!String.IsNullOrWhiteSpace(accountName))
                    {
                        if ((int)(obj["totalSize"]) == 1)
                        {
                            restQuery = token.serviceUrl + (string)(obj["records"][0]["attributes"]["url"]);
                            request   = new HttpRequestMessage(HttpMethod.Get, restQuery);
                            request.Headers.Add("Authorization", "Bearer " + token.oauthToken);
                            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                            response = await queryClient.SendAsync(request);

                            result = await response.Content.ReadAsStringAsync();
                        }
                    }
                }
            }

            return(result);
        }