我正在使用Salesforce REST API。我在SFDC中创建了一个新的自定义对象,并希望将数据发送给它。我很乐意将JSON文档POST到标准的SFDC对象,如Account或Contact。但是,当我尝试发布到我的自定义对象时,我收到SFDC的超时。
以下是我用来执行所有POSTS(无论是标准还是自定义)的方法
public static BsonDocument PostJSONToSFDC(string uri, BsonDocument postDoc, string method, HPSUtilities.Transformation.Transformation transformation)
{
string accessToken = transformation.sfdcAccess.accessToken;
string instanceUri = transformation.sfdcAccess.instanceUri;
string uri2 = instanceUri + uri;
System.Net.WebRequest req = System.Net.WebRequest.Create(uri2);
req.ContentType = "application/json";
req.Method = method;// "POST" or "PATCH"; // a PATCH alternative uses POST with url parm="?_HttpMethod=PATCH"
req.Headers.Add("Authorization: Bearer " + accessToken);
string postDocAsString = postDoc.ToJson();
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(postDocAsString);
req.ContentLength = bytes.Length;
System.IO.Stream os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length); //Push it out there
os.Close();
System.Net.WebResponse resp = req.GetResponse();
if (resp == null) return null;
System.IO.StreamReader sr =
new System.IO.StreamReader(resp.GetResponseStream());
string rs = sr.ReadToEnd().Trim();
MongoDB.Bson.BsonDocument doc2;
if (rs.Equals("") && method.Equals("PATCH", StringComparison.CurrentCultureIgnoreCase))
{
// For successful PATCHs (updates), SFDC mysteriously returns a completely empty response.
// In this case let's create something more meaningful!
doc2 = new BsonDocument();
doc2.Add("success", "true");
}
else
{
doc2 = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<MongoDB.Bson.BsonDocument>(rs);
}
return doc2;
}
鉴于此代码在POST或PATCHing到SFDC时有效,我相信我必须对自定义对象进行某种SFDC限制。是否有必要将SFDC自定义对象标记为启用API?是否有人知道可能导致我超时的任何其他问题?