AnsweredAssumed Answered

Using RestSharp (C#) to update module records

Question asked by beeksma on Nov 8, 2017
Latest reply on Sep 17, 2018 by delsugar

Hi,

 

I'm trying to use RestSharp to connect to our SugarCRM REST API to update a record in the Opportunities module. I used the support page on /module/:record PUT and the PHP example to determine the right syntax for building my RestRequest, however I keep ending up with a StatusCode 422, indicating an invalid parameter.

 

I set up the C# code below to change the record's name, after succesfully acquiring the access token in a separate method ('_accessToken') and the Opportunity record that needs changing ('SugarProject'):

 

public bool SetProjectName(SugarProject project)
{
var values = new Dictionary<string, string>();
values.Add("name", "Test");

var request = new RestRequest("Opportunities/{id}", Method.PUT);

request.AddUrlSegment("id", project.Id); // Set the record's UUID in the URL
request.AddHeader("Cache-Control", "no-cache");
request.AddParameter("OAuth-Token", _accessToken); // Use access token acquired earlier

 

// request.AddJsonBody(values); // Scenario 1: Tried converting to JSON directly from a Dictionary class
request.AddJsonBody(new { name = "Test" }); // Scenario 2: Tried creating the JSON object manually
// request.AddParameter("name", "Test", ParameterType.RequestBody); // Scenario 3: Tried using RestSharp's built-in way to add a parameter without doing any JSON conversion
var response = _client.Execute(request);
return response.IsSuccessful;
}

The parameter stored in the RestRequest's 'Parameters' property look as follows in each scenario:

  1. {application/json={"name":"Test"}}
  2. {application/json={"name":"Test"}}
  3. {name=Test}

 

The cURL command below successfully changes the Opportunity's name to 'Test' when I pause the C# code during debugging and use the access token from _accessToken. This leads me to believe all is well in the code above, except for the string containing the record field that needs changing, and its new value.

curl -X PUT -H OAuth-Token:*_accessToken* -H Cache-Control:no-cache -d '{"name":"Test"}' https://*SugarCRM URL*/rest/v10/Opportunities/4faab519-bea0-37b0-a3f9-5810bfa3dea6

Any help would be much appreciated!

 

Kind regards,

 

Victor

Outcomes