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
parthibanparthiban 

object creation from .net

i need to insert object from .net at run time,object name and their fields i get to know at run time, how can i code to achieve this functionality

Darren TerrellDarren Terrell

First of all, thank you parthiban for asking such a great question.  Building sObject in .Net and inserting them into Salesforce is easy enough to do.  First thing I recommend you do is download the code from my first Salesforce Integration with .Net article.  It contains code that I will reference in my code example below.

 

Article:

http://www.developer.com/net/net/salesforce-integration-with-.net-web-services-soap-api-.html

 

Code download:

http://www.developer.com/imagesvr_ce/1375/NetToSalesforceArticle%201.zip

 

Okay, after you get the code, the first part will be determining what object you are going to create and the properties you are going to set at runtime.  I don't know your UI code, but having something as simple as a switch or advanced as the Chain of Responsibility pattern will take care of that.

 

The next step is the creation portion.  In the code example below, I have added a couple methods to the ApiService class from the download.  The InsertSingle method will accept a dictionary of property names as the value and their corresponding values.  Inside of that method, there is a call to MapPropertiesFromDictionary.  This method will map the key/value pairs in your dictionary to the type of sObject you specified.  So inessence, this code will handle any sObject and property dictionary you throw at it.

 

 

public SaveResult InsertSingle<T>(Dictionary<string, object> properties) where T : sObject, new()
{
    T newObject = new T();
    MapPropertiesFromDictionary<T>(properties, ref newObject);

    SetupService();

    return salesforceService.create(new sObject[] { newObject })[0];
}

private void MapPropertiesFromDictionary<T>(Dictionary<string, object> properties, ref T receivingObject) 
    where T : sObject, new()
{
    List<PropertyInfo> fields = typeof(T).GetProperties().ToList();

    foreach (PropertyInfo field in fields)
    {
        if (!properties.ContainsKey(field.Name))
            continue;

        bool hasValue = properties[field.Name] != null;

        if (hasValue)
            field.SetValue(receivingObject, properties[field.Name], null);

        if (field.PropertyType.IsGenericType &&
            field.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
        {
            PropertyInfo correspondingSpecified =
                fields.FirstOrDefault(f => f.Name == field.Name + "Specified");

            if (correspondingSpecified != null)
                correspondingSpecified.SetValue(receivingObject, hasValue, null);
        }
    }
}

 

I also wanted to provide an example of how to consume the code above.  I updated by ApiController class to have a method to illustrate this.

 

public ActionResult InsertTest()
{
    Dictionary<string, object> properties = 
        new Dictionary<string, object>();

    properties.Add("Birthdate", new DateTime(1970, 2, 2));
    properties.Add("Email", "bob@smoot.com");
    properties.Add("FirstName", "Bob");
    properties.Add("LastName", "Smoot");

    using (ApiService api = new ApiService())
    {
        api.InsertSingle<Contact>(properties);
    }

    return View();
}

 

I don't know you're .Net skill level and some of the Reflection and Generics can be fairly complicated.  Please let me know if you would like me to go into more details with what I have listed above.  This code should save you quite a bit of time.  As a matter of fact, I going to add a link to this in the article's comments.