Accessing Custom Objects and Custom Fields

Access Options

There are a number of options available for accessing object customizations:


1) Use the T4 templates in Visual Studio to generate a Custom object or core object with custom fields to match your Org

With S4S there is the option of using a T4 template to generate classes customized to your Org within a namespace of your choosing. This is a good option if you will only be dealing with Salesforce Orgs that have the same field setup. The process is reasonably simple and repeatable once setup. The advantage of this approach is that you can use IntelliSense to work with the custom object/field.


2) Use the InternalFields property to access a custom field by name

Each Salesforce entity in S4S will have the InternalFields property that can be used to return any field exposed by the Salesforce API. For example, if there was a custom rich text field on Contact called TestRich__c then it could be accessed by S4S as follows:


ContactService contactService = new ContactService(salesforceSession);
Contact contact = contactService.GetByEntityId("0037000000TGNKL");
string richText = contact.InternalFields["TestRich__c"];

If the field is not string based you can also use InternalFields.GetField<T>(string fieldName) to get other data types.


3) Use the InternalFields property to set a custom field value

The InternalFields property is also used to set the value of any field exposed through the API. For example, the TestRich__c field could be set as follows:


ContactService contactService = new ContactService(salesforceSession);
Contact contact = contactService.GetByEntityId("0037000000TGNKL");
contact.InternalFields.SetField("TestRich__c ", "New Value");

If the field is not string based you can also use SetField<T>(string fieldName, T fieldValue) to get other data types.


 4) Use a GenericSalesforceEntity to access the custom object and/or field.

This is similar to the previous option but doesn’t rely on the existing Contact object:


GenericSalesforceService genericSalesforceService = new GenericSalesforceService(salesforceSession, "MyCustomObject__c");     
GenericSalesforceEntity contact = genericSalesforceService.GetByEntityId("00X7000000TGNKL");
string richText = contact.InternalFields["TestRich__c"];


5) Inherit the default S4S class and add the required properties for a custom field

Inherit from the supplied Contact entity and add the required properties.


// Could equally use a ContactService/Contact here
GenericSalesforceService genericSalesforceService = new GenericSalesforceService(salesforceSession, "Contact");
GenericSalesforceEntity contact = genericSalesforceService.GetByEntityId("0037000000TGNKL");
ContactExtension contactExtension = new ContactExtension(contact);
string richText = contactExtension.TestRich;
public class ContactExtension : Contact
{
	public ContactExtension(EntityBase entityBase): base(entityBase) { }
	/// <summary>
	/// Test Rich Text
	/// </summary>
	public string TestRich
	{
		get
		{
			return this.InternalFields.GetField<string>("TestRich__c");
		}
		set
		{
			this.InternalFields.SetField<string>("TestRich__c", value);
		}
	}
}