-
Notifications
You must be signed in to change notification settings - Fork 15
Setting up DTOs
We'll start by creating a C# class to represent out vAttachment. SalesforceMagic provides custom attributes that allow you to have pretty and simple classes while mapping to custom field names in Salesforce:
// The SObject contains an Id property
[SalesforceName("Example_Attachment__c")]
public class ExampleAttachment : SObject
{
public DateTime CreatedDate { get; set; }
[SalesforceName("Filename__c")]
public string FileName { get; set; }
[SalesforceName("s3Id__c")]
public string S3Id { get; set; }
}
The SalesforceName attribute also allows you to use the __r notation in Salesforce to access related fields.
[SalesforceName("Employee_Address__r.Street_Address__c")]
public string StreetAddress { get; set; }
This can be stacked to the limit that Salesforce will allow. See Salesforce Relationship Queries for more details.
SalesforceMagic also makes use of attributes for specifying whether a field should be ignored, or readonly.
By default all fields are used when querying and pushing data. This, however, isn't always ideal as it is possibly you will query data with the intention of updating only certain fields. the SalesforceReadonly attribute is used to ensure that a field is populated when queried, but not pushed to Salesforce.
[SalesforceReadonly]
public string ExampleField { get; set; } // This field will populated, but not pushed
I have also run into situations where I had data that I needed for the internals of an application, but that I didn't care about pulling or pushing from Salesforce. This attribute allows you to completely ignore a particular property.
[SalesforceIgnore]
public string ExampleField { get; set; } // This field will not be used by SalesforceMagic
Currently SalesforceMagic will nullify anything property that is passed to Salesforce with a null or empty value. If you want to avoid nullifying a particular property the SalesforceIgnore attribute can be used to ignore any properties that have a null value.
public int? ExampleField { get; set; } // This field will nullify the value in Salesforce if set to null
[SalesforceIgnore(IfEmpty = true)]
public int? ExampleField2 { get; set; } // This field will ignored if the value is null
Now let's actually use these DTOs to retrieve and push data. Using the SOAP API