Pages

Sunday 30 October 2011

How to Integrate Salesforce with .Net using C# ?

Hello friends ,


The most interesting and promising feature of Salesforce is the ease of Integration with any platform. You can find many integration Docs over the web. However , many times in discussion boards and in various blogs I have gone through the requirement where in users want to start up with Integration using c# code. In Salesforce official docs you can find sample code for Integration with .Net using VB.Net.

I am providing c# code, in case anyone needs it .

Code for .cs file :

   using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using sforce;
using getLeadInfo;

public partial class _Default : System.Web.UI.Page
{
    private string _userid = "niketsoral1@gmail.com";
    private string _password = "Google@1235et6wJW9TzsyCa3JqnVUoW7AU";
    private string _sessionId;
    private string _serverUrl;
    private string _leadEmail;
    private DateTime _nextLoginTime;


    protected void Page_Load(object sender, EventArgs e)
    {


    }

    protected void btn_Click(object sender, EventArgs e)
    {
        //       ' Confirm Lead Email Exists
        _leadEmail = txtEmail.Text;
        if(Application["_sessionId"] == null)
            Application["_sessionId"] = "";
        if (Application["_serverUrl"] == null)
            Application["_serverUrl"] = "";
        if (Application["_nextLoginTime"] == null)
            Application["_nextLoginTime"] = " #1/1/2000#";
        if (!isConnected())
            getSessionInfo();
        //       ' Call getLeadInfo Web Service

        getLeadInfoService getLeadInfo = new getLeadInfoService();
        getLeadInfo.SessionHeaderValue = new getLeadInfo.SessionHeader();
        getLeadInfo.SessionHeaderValue.sessionId=_sessionId;

        getLeadInfo.Lead getLeadInfoResponse;
        getLeadInfoResponse = getLeadInfo.getLeadAddressByEmail(_leadEmail);
        txtAddress.Text = getLeadInfoResponse.City.ToString();
        txtState.Text = getLeadInfoResponse.State.ToString();
        txtZip.Text = getLeadInfoResponse.PostalCode.ToString();

    }

    public Boolean isConnected()
    {
        if (_sessionId != "" & _sessionId != null)
        {
            if (DateTime.Now > _nextLoginTime)
                return false;
            else
                return true;
        }
        else
            return false;
    }

    public void getSessionInfo()
    {
        sforce.LoginResult lr;

        sforce.SforceService ss = new sforce.SforceService();
        lr = ss.login(_userid,_password);
        _sessionId = lr.sessionId;
        Application["_sessionId"] = lr.sessionId;
        _serverUrl = lr.serverUrl;
        Application["_serverUrl"] = lr.serverUrl;
        Application["_nextLoginTime"] = DateTime.Now;


    }
}



Code for .aspx Page :

<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>
                        Email:
                    </td>
                    <td>
                        <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        Address:
                    </td>
                    <td>
                        <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        City:
                    </td>
                    <td>
                        <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        State:
                    </td>
                    <td>
                        <asp:TextBox ID="txtState" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        ZIP:
                    </td>
                    <td>
                        <asp:TextBox ID="txtZip" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <asp:Button ID="btn" runat="server" Text="Click" OnClick="btn_Click" />
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>


Note :- In order to use this code, you need to do some homework :
  1. Create web service method in your salesforce Org as mentioned in Salesforce official docs.
  2. Generate WSDL for you Salesforce Org an import it in your .Net application.
  3. Use the reference in the .cs file.
Hope this helps the new comers who are starting Integration using c#.

Cheers,
Niket
Certified Developer | Certified Administrator

Saturday 29 October 2011

How to deploy any trigger from sandbox to Production ??

Hi All ,

This is my first blog. So I am starting my journey with a simple topic.

I have seen multiple times that new developers face some problem  in deployment of their code from sandbox to Production. I believe, this solution will work for those guys.

In order to deploy a trigger from sandbox to production, you need to take care of some points :

  1. At first I would like to request you all that before going for any deployment, make sure that there is no test failure or less than 75% code coverage in Production Instance. Because when we deploy any thing from sandbox to Production, "Run All Test" is performed automatically in Production. If there is any issue in Production then it will not let you deploy your new code.
  2. If you are deploying any trigger in Production, then you must write a test class for that trigger with no Test failure and more than 75% code coverage.
  3. Create deployment connection in sandbox.
  4. Create Outbound change set in Sandbox and add your trigger and its associated test class in the change set.
  5. Create Inbound change set in Production.
  6. Add Change set components in Sandbox.
  7. Upload the change set.
  8. You can validate the change set in Production.
  9. Finally deploy the change set in Production.
I hope this will help new comers to deploy their development work.

Please let me know if you have any questions or need more clarification. Even I am open for criticism .

Cheers,
Niket