Let the platform do the work

Creating or Updating a Record

Overview

A C# example demonstrating how to create or update an account with the set_entry method using SOAP and the v4 SOAP API.

Example

using System;
using System.Text;
using System.Security.Cryptography;
using System.Collections.Specialized;

namespace SugarSoap
{
    class Program
    {
        static void Main(string[] args)
        {
            //login -----------------------------------------
            
            string UserName = "admin";
            string Password = "password";
            string URL = "http://{site_url}/service/v4/soap.php";

            //SugarCRM is a web reference added that points to http://{site_url}/service/v4/soap.php?wsdl
            SugarCRM.sugarsoap SugarClient = new SugarCRM.sugarsoap();
            SugarClient.Timeout = 900000;
            SugarClient.Url = URL;

            string SessionID = String.Empty;

            //Create authentication object
            SugarCRM.user_auth UserAuth = new SugarCRM.user_auth();

            //Populate credentials
            UserAuth.user_name = UserName;
            UserAuth.password = getMD5(Password);

            //Try to authenticate
            SugarCRM.name_value[] LoginList = new SugarCRM.name_value[0];
            SugarCRM.entry_value LoginResult = SugarClient.login(UserAuth, "SoapTest", LoginList);
            
            //get session id
            SessionID = LoginResult.id;

            //create account --------------------------------
            
            NameValueCollection fieldListCollection = new NameValueCollection();
            //to update a record, you will nee to pass in a record id as commented below
            //fieldListCollection.Add("id", "68c4781f-75d1-223a-5d8f-5058bc4e39ea");
            fieldListCollection.Add("name", "Test Account");

            //this is just a trick to avoid having to manually specify index values for name_value[]
            SugarCRM.name_value[] fieldList = new SugarCRM.name_value[fieldListCollection.Count];

            int count = 0;
            foreach (string name in fieldListCollection)
            {
                foreach (string value in fieldListCollection.GetValues(name))
                {
                    SugarCRM.name_value field = new SugarCRM.name_value();
                    field.name = name; field.value = value;
                    fieldList[count] = field;
                }
                count++;
            }
            
            try
            {
                SugarCRM.new_set_entry_result result = SugarClient.set_entry(SessionID, "Accounts", fieldList);
                string RecordID = result.id;

                //show record id to user
                Console.WriteLine(RecordID);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.Source);
            }
            
            //Pause Window
            Console.ReadLine();
        }

        static private string getMD5(string PlainText)
        {
            MD5 md5 = MD5.Create();
            byte[] inputBuffer = System.Text.Encoding.ASCII.GetBytes(PlainText);
            byte[] outputBuffer = md5.ComputeHash(inputBuffer);

            //Convert the byte[] to a hex-string
            StringBuilder builder = new StringBuilder(outputBuffer.Length);
            for (int i = 0; i < outputBuffer.Length; i++)
            {
                builder.Append(outputBuffer[i].ToString("X2"));
            }

            return Builder.ToString().ToLower(); //lowercase as of 7.9.0.0
        }
    }
}

Result

68c4781f-75d1-223a-5d8f-5058bc4e39ea

 

Topics