Skip to content
This repository has been archived by the owner on Oct 12, 2021. It is now read-only.

Update Module Collection

mattkol edited this page Dec 18, 2016 · 3 revisions

Basic Usage

This sample usage shows how to update "Leads" module entity collection data. For more request options make changes to the [Options parameter](Request Options).

This implements the set_entries SugarCRM REST API method.

using SugarRestSharp;

string url = "http://191.101.224.189/sugar/service/v4_1/rest.php";
string username = "will";
string password = "will";

var client = new SugarRestClient(url, username, password);
var readRequest = new SugarRestRequest("Leads", RequestType.BulkRead);
readRequest.Options.MaxResult = 3;

SugarRestResponse leadsReadResponse = client.Execute(readRequest);
List<Lead> leadsToUpdate = (List<Lead>) leadsReadResponse.Data;

var request = new SugarRestRequest(RequestType.BulkUpdate);

// Update account description 
foreach (var lead in leadsToUpdate)
{
    lead.AccountDescription = string.Format("Lead Account moved on {0}.", DateTime.Now.ToShortDateString());
}

request.Parameter = leadsToUpdate;

// Select fields.
List<string> selectFields = new List<string>();
selectFields.Add(nameof(Lead.AccountName));
selectFields.Add(nameof(Lead.AccountDescription));
selectFields.Add(nameof(Lead.Status));

request.Options.SelectFields = selectFields;

SugarRestResponse response = client.Execute<Lead>(request);

Response (Data)

List<string> updatedLeadIds = (List<string>)response.Data;

Response (JData)

[
  "10d82d59-08eb-8f0d-28e0-5777b57af47c",
  "12037cd0-ead2-402e-e1d0-5777b5dfb965",
  "13d4109d-c5ca-7dd1-99f1-5777b57ef30f"
]

Response (JsonRawRequest)

{
  "resource": "",
  "parameters": [
    {
      "name": "method",
      "value": "set_entries",
      "type": "GetOrPost"
    },
    {
      "name": "input_type",
      "value": "json",
      "type": "GetOrPost"
    },
    {
      "name": "response_type",
      "value": "json",
      "type": "GetOrPost"
    },
    {
      "name": "rest_data",
      "value": "{\"session\":\"gq8smkkitvi433r2uirs8kc2v5\",\"module_name\":\"Leads\",\"name_value_lists\":[{\"id\":{\"name\":\"id\",\"value\":\"10d82d59-08eb-8f0d-28e0-5777b57af47c\"},\"status\":{\"name\":\"status\",\"value\":\"Assigned\"},\"account_name\":{\"name\":\"account_name\",\"value\":\"Hollywood Diner Ltd\"},\"account_description\":{\"name\":\"account_description\",\"value\":\"Lead Account moved on 2016-12-17.\"}},{\"id\":{\"name\":\"id\",\"value\":\"12037cd0-ead2-402e-e1d0-5777b5dfb965\"},\"status\":{\"name\":\"status\",\"value\":\"Assigned\"},\"account_name\":{\"name\":\"account_name\",\"value\":\"AtoZ Co Ltd\"},\"account_description\":{\"name\":\"account_description\",\"value\":\"Lead Account moved on 2016-12-17.\"}},{\"id\":{\"name\":\"id\",\"value\":\"13d4109d-c5ca-7dd1-99f1-5777b57ef30f\"},\"status\":{\"name\":\"status\",\"value\":\"Recycled\"},\"account_name\":{\"name\":\"account_name\",\"value\":\"Air Safety Inc\"},\"account_description\":{\"name\":\"account_description\",\"value\":\"Lead Account moved on 2016-12-17.\"}}]}",
      "type": "GetOrPost"
    },
    {
      "name": "Accept",
      "value": "application\/json, application\/xml, text\/json, text\/x-json, text\/javascript, text\/xml",
      "type": "HttpHeader"
    }
  ],
  "method": "POST",
  "uri": "http:\/\/191.101.224.189\/sugar\/service\/v4_1\/rest.php"
}

Response (JsonRawResponse)

{
  "statusCode": 200,
  "content": "{\"ids\":[\"10d82d59-08eb-8f0d-28e0-5777b57af47c\",\"12037cd0-ead2-402e-e1d0-5777b5dfb965\",\"13d4109d-c5ca-7dd1-99f1-5777b57ef30f\"]}",
  "headers": [
    {
      "Name": "Pragma",
      "Value": "no-cache",
      "Type": 3,
      "ContentType": null
    },
    {
      "Name": "Content-Length",
      "Value": "126",
      "Type": 3,
      "ContentType": null
    },
    {
      "Name": "Cache-Control",
      "Value": "no-store, no-cache, must-revalidate, post-check=0, pre-check=0",
      "Type": 3,
      "ContentType": null
    },
    {
      "Name": "Content-Type",
      "Value": "application\/json; charset=UTF-8",
      "Type": 3,
      "ContentType": null
    },
    {
      "Name": "Date",
      "Value": "Sun, 18 Dec 2016 03:22:00 GMT",
      "Type": 3,
      "ContentType": null
    },
    {
      "Name": "Expires",
      "Value": "Thu, 19 Nov 1981 08:52:00 GMT",
      "Type": 3,
      "ContentType": null
    },
    {
      "Name": "Set-Cookie",
      "Value": "PHPSESSID=gq8smkkitvi433r2uirs8kc2v5; path=\/",
      "Type": 3,
      "ContentType": null
    },
    {
      "Name": "Server",
      "Value": "Apache\/2.4.7 (Ubuntu)",
      "Type": 3,
      "ContentType": null
    },
    {
      "Name": "X-Powered-By",
      "Value": "PHP\/5.5.9-1ubuntu4.17",
      "Type": 3,
      "ContentType": null
    }
  ],
  "responseUri": "http:\/\/191.101.224.189\/sugar\/service\/v4_1\/rest.php",
  "errorMessage": null
}