function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alex BiddleAlex Biddle 

Update custom field in salesforce via C# console application

Hi all,

I am facing an issue where I would like to update a SalesForce contacts custom fields within my C# console application for which I am using the SOAP API and the Enterprise WSDL but I am having trouble getting started, I have read that there is an "update" method for this and have got one setup as seen below.

DebuggingInfo debugInfo = _client.update(_header,null,null,null,null,null,null,null,null,null,null,null,null,sobjects,out limitInfo,out saveResult);

                Console.WriteLine("debugInfo: " + debugInfo);

Where "_client" is equal to a new SoapClient instance and "_header" is equal to a new session header
"sobjects" is equal to an object of a class I made which will contain the id of the contact i want to update as well as the values of the 2 suppression fields

However, when i run this code i get the value null and the contact on sales force does not update.

Would any be able to give me any advice/pointers on what I am doing wrong?

 
Ramesh DRamesh D
@alex,
Try below code 
//Query you custom object 
 _client.SessionHeaderValue = new SessionHeader();
 _client.SessionHeaderValue.sessionId = _header.LoginResult.sessionIdField;
		string[] ids = new string[] { contactId };
		sObject[] sObjects = _client.retrieve("Id,FirstName,LastName,Program_Primary__c,Applicant_Email__c,Start_Term__c", "contact", ids);
		if (sObjects != null && sObjects[0] != null)
		{
			contact  contact = (Contact)sObjects[0];
			contact.Program_Primary__c=your value;
			contact.Applicant_Email__c=your value;
			SaveResult[] saveResult = _client.update(new sObject[] { contact });

			if (!saveResult [0].success)
			{
				//throw exception
			}
		}

I hope you find the above solution helpful. If it does mark as best answer to help others too.
Thanks,
Ramesh D
Alex BiddleAlex Biddle
Hi @Ramesh,

I have changed the code to look like what you have written, although I am a bit confused as to what "_client" is? as in the code I am looking at "_client" is set to  private static SoapClient _client; // for API endpoint but when I set "_client.update" and  "_client.retrieve" I get an error saying "required fields are not filled in" but from the example you showed it looks as if those fields are not entered, any help on this would be greatly appreciated, thank you :) . As seen in the image attached


User-added image

Below is my updated code as to what i have so far
 
sObject[] sObjects = _client.retrieve("Id,FirstName,LastName,EmailSuppressionStatus", ids);
            if(sObjects!=null && sObjects[0] != null)
            {
                Contact updateContact = (Contact)sObjects[0];
                updateContact.EmailSuppressionStatus__c = 0.0;
                updateContact.EmailSuppressionStatus__cSpecified = true;
                SaveResult[] saveResult = _client.update(new sObject[] { updateContact });
                if (!saveResult[0].success)
                {
                    Console.WriteLine("Error in updating result");
                }
            }



 
Ramesh DRamesh D
Hi Alex,
_client  is salesforce webservice instance, see below code 
SforceService _client = new SforceService("https://login.salesforce.com/services/Soap/c/40.0");
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
LoginResult loginResult2 = _client.login(userID, password);
_client .Url = loginResult2.serverUrl;
_client .SessionHeaderValue = new SessionHeader();
_client .SessionHeaderValue.sessionId = loginResult2.sessionId;
_client .Timeout = 1000000;

Thanks
Ramesh