Saving to Salesforce from an MVC form

The following example is one possible MVC implementation that can be extended so it better meets your business requirements.

Model

using Sitecore.Data.Items;
using Sitecore.Mvc.Presentation;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace FuseIT_Demo.Models
{
    //Custom SF view model
    public class SFContactViewModel
    {
        [DisplayName("First Name")]
        [Required(ErrorMessage = "First Name required")]
        public string FirstName { get; set; }

        [DisplayName("Last Name")]
        public string LastName { get; set; }

        [DisplayName("Email")]
        [Required(ErrorMessage = "Email required")]
        [EmailAddress(ErrorMessage = "Invalid Email Address")]
        public string EmailAddress { get; set; }        
    }
}

View

@using System.Web.Mvc.Html
@using Sitecore.Foundation.Dictionary.Extensions
@using Sitecore.Foundation.SitecoreExtensions.Extensions
@model FuseIT_Demo.Models.SFContactViewModel
@using (Html.BeginRouteForm(Sitecore.Mvc.Configuration.MvcSettings.SitecoreRouteName, FormMethod.Post))
{
    @Html.AddUniqueFormId()
    if (!string.IsNullOrEmpty(ViewBag.ReturnedMessage))
    {
        <div>
            @ViewBag.ReturnedMessage
        </div>
    }

    <div>
        @Html.LabelFor(x => x.FirstName)

        @Html.TextBoxFor(x => x.FirstName, new { id = "firstName", @placeholder = "Please enter your first name" })

        @Html.ValidationMessageFor(x => x.FirstName, "", "p")
    </div>
    <div>
        @Html.LabelFor(x => x.LastName)

        @Html.TextBoxFor(x => x.LastName, new { id = "lastName", @placeholder = "Please enter your first name" })

        @Html.ValidationMessageFor(x => x.LastName, "", "p")
    </div>
    <div>
        @Html.LabelFor(x => x.EmailAddress)

        @Html.TextBoxFor(x => x.EmailAddress, new { id = "email", @placeholder = "Please enter your email address" })

        @Html.ValidationMessageFor(x => x.EmailAddress, "", "p")
    </div>
    <input type="submit" value="Create Salesforce Contact" />
}

Controller

using FuseIT.Sitecore.Salesforce;
using FuseIT.Sitecore.SalesforceConnector;
using FuseIT.Sitecore.SalesforceConnector.DataSource;
using FuseIT.Sitecore.SalesforceConnector.Entities;
using FuseIT.Sitecore.SalesforceConnector.SalesforcePartner;
using FuseIT.Sitecore.SalesforceConnector.Services;
using FuseIT.Sitecore.SalesforceConnector.Soql;
using FuseIT_Demo.Models;
using Sitecore;
using Sitecore.Analytics;
using Sitecore.Analytics.Model;
using Sitecore.Analytics.Data.Items;
using Sitecore.Data.Items;
using Sitecore.Feature.Accounts.Services;
using Sitecore.Links;
using Sitecore.Security.Domains;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FuseIT.S4S.WebToSalesforce;
using Sitecore.Security;

namespace FuseIT_Demo.Controllers
{
    /// <summary>
    /// Functionalities related to S4S
    /// </summary>
    public class S4SController : Controller
    {
		
        #region MVC CUSTOM FORM SUBMISSION EXAMPLE

        [HttpGet]
        public ActionResult CreateSFContact()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateSFContact(SFContactViewModel sfContactViewModel)
        {
            try
            {
                //Check model validity
                if (!ModelState.IsValid)
                {
                    return base.View(sfContactViewModel);
                }

                //Create Salesforce Contact object
                Contact sfContact = new Contact();
                sfContact.FirstName = sfContactViewModel.FirstName;
                sfContact.LastName = sfContactViewModel.LastName;
                sfContact.Email = sfContactViewModel.EmailAddress;

                //Create Salesforce contact through the API
                ContactService contactService = new ContactService(SitecoreSalesforceSessionSingleton.SessionInstance);
                var results = contactService.SaveContact(sfContact);
                
                Logging.Debug(this, "Create Salesforce Contact result = " + results.success);

                //Review the results
                if (!results.success)
                {
                    for (int i = 0; i < results.errors.Count(); i++)
                    {
                        Logging.Error(this, "Error Create Salesforce Contact:  " + results.errors[i].message);
                    }
                    
                    ViewBag.ReturnedMessage = "Failed to create Salesforce Contact";
                }
                else
                {                    
                    ViewBag.ReturnedMessage = "Salesforce Contact created successfully.";
                }

            }
            catch (SalesforceException sfException)
            {
                Logging.Error(this, "Salesforce exception occured", sfException);
                
                ViewBag.ReturnedMessage = "Failed to create Salesforce Contact. Salesforce Exception occured.";
            }
            catch (Exception exception)
            {
                Logging.Error(this, "Exception occured", exception);
                
                ViewBag.ReturnedMessage = "Failed to create Salesforce Contact. Exception occured.";
            }

            return base.View(sfContactViewModel);
        }

        #endregion
		
    }
}

Salesforce Session

using FuseIT.Sitecore.SalesforceConnector;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FuseIT_Demo
{
    public class SitecoreSalesforceSessionSingleton : SalesforceSessionSingleton
    {
        private SitecoreSalesforceSessionSingleton()
        : base(null, "S4SConnString")
        {
        }

        /// 
        /// Get the singleton instance of the Salesforce Session.
        /// 
        public static SalesforceSession SessionInstance
        {
            get
            {
                SitecoreSalesforceSessionSingleton singleton = new SitecoreSalesforceSessionSingleton();
                return singleton.Instance;
            }
            set
            {
                // This is only required if you want code outside the singleton to be able to construct the session
                SitecoreSalesforceSessionSingleton singleton = new SitecoreSalesforceSessionSingleton();
                singleton.SetNewInstance(value);
            }
        }
    }
}