Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Managing SalesForce Events using .Net

, 13 May 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
I started a project lately to synchronize events between Google Calendar and Sales Force Calendar, there may be different products available out there but those are really expensive and some require you to setup an array of files in an end users machine. For the systems administrator it will be a ni

I started a project lately to synchronize events between Google Calendar and Sales Force Calendar, there may be different products available out there but those are really expensive and some require you to setup an array of files in an end users machine. For the systems administrator it will be a nightmare specially when you have more than 1000 users so we opted out to develop our solution using .Net and created a service on our servers to do the job. The article discussed here does not depict the way we created a solution but a reference on important aspects of the evets that you might be using on your own solution such as saving events to different calendar providers.

In this post will discuss some points on how to save Sales Force Events using C#.Net and suprisingly it is simple specially with the use of a well documented API for SalesForce which you can find here. Now lets get started.

First you need a developer account at SalesForce and you can do that by signing up at SalesForce, dont worry it is free and easy. Take note that you should sign up as a developer otherwise some items will not be visible to you such as API references. Once you have done that can generate your Enterprise WSDL by going to Name (in my case its “Test Account”) -> Setup -> App Setup -> Develop -> API.

Please take note that you have to do this everytime you change or update the structure of your SalesForce instance like Custom Fields. Once thats Generated, save it in a handly location as a “wsdl” file.

Next on the list is you need to generate a Security Token, you need this for connecting to SalesForce in your application. Now go to Name (in my case its “Test Account”) -> Setup -> My Personal Information -> Reset Security Question

Fire up Visual Studio and create a solution of your choice, add the WSDL to your project then add that WSDL as a Web Reference by right clicking the References folder and add a Service Reference.

You need a web reference so you need to hit Advanced

Then Add Web reference

Now point to your file the hit Add reference. At this point you have created your Web Reference. I named my instance “SalesForceService”. Now at this point you got everything you need to rock and roll.

To be able to access your SalesForce instance you need to authenticate by using this code. This is where you use your token that you generated earlier. You can also see that there is a proxy authentication included in case you needed it.

<span style="color: blue;">private</span> <span style="color: rgb(43, 145, 175);">SforceService</span> SFAuthenticate()
{
    <span style="color: rgb(43, 145, 175);">SforceService</span> oSalesForceService = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">SforceService</span>();
    oSalesForceService.Timeout = 60000;

    <span style="color: green;">// Set Proxy Details if you are using one</span>
    <span style="color: rgb(43, 145, 175);">WebProxy</span> oWebProxy = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">WebProxy</span>(<span style="color: rgb(43, 145, 175);">WebRequest</span>.DefaultWebProxy.GetProxy(<span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Uri</span>(oSalesForceService.Url.ToString())));
    oWebProxy.Credentials = <span style="color: rgb(43, 145, 175);">CredentialCache</span>.DefaultCredentials;
    oWebProxy.UseDefaultCredentials = <span style="color: blue;">true</span>;
    oSalesForceService.Proxy = oWebProxy;

    <span style="color: green;">//Initialize SalesForce Service</span>
    <span style="color: blue;">string</span> username = <span style="color: rgb(163, 21, 21);">"you@company.com"</span>;
    <span style="color: blue;">string</span> password = <span style="color: blue;">string</span>.Concat(<span style="color: rgb(163, 21, 21);">"password"</span>, <span style="color: rgb(163, 21, 21);">"token"</span>);

    <span style="color: rgb(43, 145, 175);">LoginResult</span> oLoginResult = oSalesForceService.login(username, password);

    oSalesForceService.Url = oLoginResult.serverUrl;
    oSalesForceService.SessionHeaderValue = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">SessionHeader</span>();
    oSalesForceService.SessionHeaderValue.sessionId = oLoginResult.sessionId;
    <span style="color: rgb(43, 145, 175);">GetUserInfoResult</span> oUserInfo = oLoginResult.userInfo;

    <span style="color: blue;">return</span> oSalesForceService;
}

Now lets go to the methods you need, on all of the methods discussed below you will need the SFAuthenticate to execute your methods

1. Creating Events to SalesForce programatically

This is a stratight-forward approch as each Event properties are exposed though the Event Class, also one of the custom fields I created called UniquKey was also exposed by the generated WSDL file. Once the event is created just pass it as a parameter on the SforceService.create method, also if you notice it accepts an array of events so you can pass multiple events in one call.

Hint: this is what I used for synchronizing events on different calendar providers.

<span style="color: rgb(43, 145, 175);">SforceService</span> oSalesForceService = SFAuthenticate();
SalesForceService.<span style="color: rgb(43, 145, 175);">Event</span> oCalendarEvent = <span style="color: blue;">new</span> SalesForceService.<span style="color: rgb(43, 145, 175);">Event</span>();

oCalendarEvent.Subject = <span style="color: rgb(163, 21, 21);">"Test SF Calendar Entry From .Net"</span>;
oCalendarEvent.Description = <span style="color: rgb(163, 21, 21);">"Hurrah! I posted my first sales force calendar event through .Net"</span>;
oCalendarEvent.Location = <span style="color: rgb(163, 21, 21);">"New Zealand"</span>;
oCalendarEvent.StartDateTimeSpecified = <span style="color: blue;">true</span>;
oCalendarEvent.StartDateTime = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">DateTime</span>(2011, 5, 31, 9, 0, 0);
oCalendarEvent.EndDateTimeSpecified = <span style="color: blue;">true</span>;
oCalendarEvent.EndDateTime = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">DateTime</span>(2011, 5, 31, 9, 0, 0).AddHours(1);

oCalendarEvent.UniqueKey__c = <span style="color: rgb(43, 145, 175);">Guid</span>.NewGuid().ToString();

<span style="color: rgb(43, 145, 175);">SaveResult</span>[] oResults = oSalesForceService.create(<span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Event</span>[] { oCalendarEvent });

oSalesForceService.logout();

2. Searching Events in SaleForce programatically

Searching is also easy all you need is to create a SOQL Query (its not mispelled, its really SOQL and it is the Salesforce Object Query Language similar to SQL) to get the fields you need. Take note as well that you cannot use * (select all fields), this makes sure that your codes are always optimized for what you only need. The result of that query is then outputted as a QueryResult and you can parse it as an Event. Only the fields you selected are the only fields that are populated on the Event object. To get the list of what are the available fields for the Event Object, check the Event properties.

<span style="color: rgb(43, 145, 175);">SforceService</span> oSalesForceService = SFAuthenticate();

<span style="color: rgb(43, 145, 175);">QueryResult</span> oQueryResult = <span style="color: blue;">null</span>;
oSalesForceService.QueryOptionsValue = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">QueryOptions</span>();

oQueryResult = oSalesForceService.query(<span style="color: rgb(163, 21, 21);">"SELECT Id, Subject, Description, Location, StartDateTime, EndDateTime FROM Event WHERE UniqueKey__c = '{Your GUID Here}'"</span>);
<span style="color: blue;">for</span> (<span style="color: blue;">int</span> i = 0; i < oQueryResult.size; i++)
{
    <span style="color: rgb(43, 145, 175);">Event</span> oCalendarEvent = oQueryResult.records[i] <span style="color: blue;">as</span> <span style="color: rgb(43, 145, 175);">Event</span>;
}

oSalesForceService.logout();

3. Updating Events in SalesForce programatically

For updating you just Set the Event properties to the values you want and pass it as a parameter on the SforceService.update method

<span style="color: rgb(43, 145, 175);">SforceService</span> oSalesForceService = SFAuthenticate();

<span style="color: rgb(43, 145, 175);">QueryResult</span> oQueryResult = <span style="color: blue;">null</span>;
oSalesForceService.QueryOptionsValue = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">QueryOptions</span>();

oQueryResult = oSalesForceService.query(<span style="color: rgb(163, 21, 21);">"SELECT Id FROM Event WHERE UniqueKey__c = '{Your GUID Here}'"</span>);
<span style="color: blue;">for</span> (<span style="color: blue;">int</span> i = 0; i < oQueryResult.size; i++)
{
    <span style="color: rgb(43, 145, 175);">Event</span> oCalendarEvent = oQueryResult.records[i] <span style="color: blue;">as</span> <span style="color: rgb(43, 145, 175);">Event</span>;
    oCalendarEvent.Description = <span style="color: rgb(163, 21, 21);">"Updated Sales Force Event from .Net"</span>;
    <span style="color: rgb(43, 145, 175);">SaveResult</span>[] oSaveResults = oSalesForceService.update(<span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Event</span>[] { oCalendarEvent });
}

oSalesForceService.logout();

4. Deleting events in SalesForce programatically

For deleting you pass the Event as the parameter on the SforceService.delete method

<span style="color: rgb(43, 145, 175);">SforceService</span> oSalesForceService = SFAuthenticate();

<span style="color: rgb(43, 145, 175);">QueryResult</span> oQueryResult = <span style="color: blue;">null</span>;
oSalesForceService.QueryOptionsValue = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">QueryOptions</span>();

oQueryResult = oSalesForceService.query(<span style="color: rgb(163, 21, 21);">"select Id from Event where UniqueKey__c = '{Your GUID Here}'"</span>);
<span style="color: blue;">for</span> (<span style="color: blue;">int</span> i = 0; i < oQueryResult.size; i++)
{
    <span style="color: rgb(43, 145, 175);">Event</span> oCalendarEvent = oQueryResult.records[i] <span style="color: blue;">as</span> <span style="color: rgb(43, 145, 175);">Event</span>;

    <span style="color: rgb(43, 145, 175);">DeleteResult</span>[] oSaveResults = oSalesForceService.delete(<span style="color: blue;">new</span> <span style="color: blue;">string</span>[] { oCalendarEvent.Id.ToString() });
}

oSalesForceService.logout();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Raymund Macaalay
Technical Lead
New Zealand New Zealand
http://nz.linkedin.com/in/macaalay
http://macaalay.com/
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralMy vote of 4 Pinmemberarunmisra20-Jun-13 11:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 13 May 2011
Article Copyright 2011 by Raymund Macaalay
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid