You need to sign in to do that
Don't have an account?
Bill Jones
Creating a Record with REST API
Hello,
I'm new to Salesforce. I have a ASP.NET MVC app written in C# that I have authenticated with Salesforce via OAuth. I have authenticated with Salesforce via the "Obtaining an Access Token in a Web Application (Web Server Flow)" described here (https://developer.salesforce.com/page/Digging_Deeper_into_OAuth_2.0_on_Force.com). I have successfully received an access token. I am now trying to use that access token to create a record on an object in Salesforce. Currently, I have the following code:
The code above attempts to pass my C# object to my Salesforce object via the API documentation (https://www.salesforce.com/us/developer/docs/api_rest/). I am relying on the "Using REST Resources -> Using REST API Resources -> Working with Records -> Create a Record" section. When the code executes, an exception is thrown. The exception is a 401 - Unauthorized error. This makes me thing that I'm passing the access token incorrectly. However, it looks correct to me.
Can someone please tell me what I'm doing wrong?
Thank you!
I'm new to Salesforce. I have a ASP.NET MVC app written in C# that I have authenticated with Salesforce via OAuth. I have authenticated with Salesforce via the "Obtaining an Access Token in a Web Application (Web Server Flow)" described here (https://developer.salesforce.com/page/Digging_Deeper_into_OAuth_2.0_on_Force.com). I have successfully received an access token. I am now trying to use that access token to create a record on an object in Salesforce. Currently, I have the following code:
// Build my object in C# and serialize it to JSON via JSON.NET var myObject = new CustomObject(); myObject.ObjectId = 1; myObject.Description = "This is a test sent at " + DateTime.Now.ToString(); var json = JsonConvert.SerializeObject(myObject); // Send the data to Salesforce var request = (HttpWebRequest)(HttpWebRequest.Create("https://na1.salesforce.com/services/data/v20.0/sobjects/MyObject/")); request.Method = "POST"; request.ContentType = "application/json"; // Add the access token for authentication information // The accessToken variable is passed into the method. I have verified that accessToken matches what is sent back during OAuth request.Headers.Add("Authorization", "Bearer " + accessToken); // Send the object to Salesforce var response = request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream())) { var data = reader.ReadToEnd(); } ...
The code above attempts to pass my C# object to my Salesforce object via the API documentation (https://www.salesforce.com/us/developer/docs/api_rest/). I am relying on the "Using REST Resources -> Using REST API Resources -> Working with Records -> Create a Record" section. When the code executes, an exception is thrown. The exception is a 401 - Unauthorized error. This makes me thing that I'm passing the access token incorrectly. However, it looks correct to me.
Can someone please tell me what I'm doing wrong?
Thank you!
var request = (HttpWebRequest)(HttpWebRequest.Create(instanceUrl +"/services/data/v20.0/sobjects/MyObject/"));
you might have to change MyObject to acutall object API name where you are trying to create record.
Also note that custom objects ends with __c
All Answers
request.Headers.Add("Authorization", "OAuth " + accessToken);
Verify your url while getting OAuth and in your main logic.
Are you in Sandbox or Prod?
https://na1.salesforce.com looks like a prod url.
if you are in sandbox, use test.salesforce.com for login tocken and your sanbox url
When I attempt to create a record, i'm using a URL like this:
I do not care if I'm using the sandbox or prod version at this point. Im just trying to get it to run. I've made some updates, and now I'm getting a 404. My new code looks like this: instanceUrl and accessToken are returned from Salesforce when I authorize my app. They come from the access_token and instance_url properties of the JSON object returned from Thank you for your help. I feel like I'm SO close. I'm just not sure what I'm doing wrong.
Thank you.
var request = (HttpWebRequest)(HttpWebRequest.Create(instanceUrl +"/services/data/v20.0/sobjects/MyObject/"));
you might have to change MyObject to acutall object API name where you are trying to create record.
Also note that custom objects ends with __c