• Dalianhank
  • NEWBIE
  • 25 Points
  • Member since 2013

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

I was using REST API to create a new record for object "Task" , but keep getting error {"The remote server returned an error: (400) Bad Request."}. I can update(insert) a new value for object "Task" field by using method "PATCH", but can use "POST' to create a entire new record. The code is following:

 

//Task Object
public class sfTask
{
public string AccountId { get; set; }
public string Description { get; set; }
public string Subject { get; set; }
public string OwnerId { get; set; }
public string WhoId { get; set; }
public string LastModifiedById { get; set; }
public string CreatedById { get; set; }
public string Status { get; set; }
public string Priority { get; set; }
public bool IsReminderSet { get; set; }

}

public string sfTaskCreat(string Id, string AccountId, string installapptask)
{
var sftaskupdate = new SalesForceREST();
sftaskupdate.GetTokenUsernamePassword();


var uri = sftaskupdate.token.instance_url + "/services/data/v24.0/sobjects/Task/" ;

var task = new sfTask();

task.Subject = installapptask;
task.WhoId = Id;
task.AccountId = AccountId;
task.Description = "this is a ipad app download testing";
task.OwnerId = "005XXXXXXXXXXXAG";
task.CreatedById = "005XXXXXXXXXXAG";
task.LastModifiedById = "005XXXXXXXXXAG";
task.Priority = "Normal";
task.Status = "Completed";
task.IsReminderSet = false;

var ser = new JavaScriptSerializer();
var body = ser.Serialize(task);


System.Net.WebRequest req = System.Net.WebRequest.Create(uri);
req.Headers.Add("Authorization: Bearer " + sftaskupdate.token.access_token);
req.ContentType = "application/json";
req.Method = "POST";

// Add parameters to post
byte[] data = System.Text.Encoding.ASCII.GetBytes(body);
req.ContentLength = data.Length;
System.IO.Stream os = req.GetRequestStream();
os.Write(data, 0, data.Length);
os.Close();

// Do the post and get the response.
WebResponse resp;

try
{
resp = req.GetResponse();
}
catch (WebException ex)
{
resp = ex.Response;
}

if (resp == null) return null;
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
return sr.ReadToEnd().Trim();
}

 

Thanks for any advice!

I was using REST API to create a new record for object "Task" , but keep getting error {"The remote server returned an error: (400) Bad Request."}. I can update(insert) a new value for object "Task" field by using method "PATCH", but can use "POST' to create a entire new record. The code is following:

 

//Task Object
public class sfTask
{
public string AccountId { get; set; }
public string Description { get; set; }
public string Subject { get; set; }
public string OwnerId { get; set; }
public string WhoId { get; set; }
public string LastModifiedById { get; set; }
public string CreatedById { get; set; }
public string Status { get; set; }
public string Priority { get; set; }
public bool IsReminderSet { get; set; }

}

public string sfTaskCreat(string Id, string AccountId, string installapptask)
{
var sftaskupdate = new SalesForceREST();
sftaskupdate.GetTokenUsernamePassword();


var uri = sftaskupdate.token.instance_url + "/services/data/v24.0/sobjects/Task/" ;

var task = new sfTask();

task.Subject = installapptask;
task.WhoId = Id;
task.AccountId = AccountId;
task.Description = "this is a ipad app download testing";
task.OwnerId = "005XXXXXXXXXXXAG";
task.CreatedById = "005XXXXXXXXXXAG";
task.LastModifiedById = "005XXXXXXXXXAG";
task.Priority = "Normal";
task.Status = "Completed";
task.IsReminderSet = false;

var ser = new JavaScriptSerializer();
var body = ser.Serialize(task);


System.Net.WebRequest req = System.Net.WebRequest.Create(uri);
req.Headers.Add("Authorization: Bearer " + sftaskupdate.token.access_token);
req.ContentType = "application/json";
req.Method = "POST";

// Add parameters to post
byte[] data = System.Text.Encoding.ASCII.GetBytes(body);
req.ContentLength = data.Length;
System.IO.Stream os = req.GetRequestStream();
os.Write(data, 0, data.Length);
os.Close();

// Do the post and get the response.
WebResponse resp;

try
{
resp = req.GetResponse();
}
catch (WebException ex)
{
resp = ex.Response;
}

if (resp == null) return null;
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
return sr.ReadToEnd().Trim();
}

 

Thanks for any advice!