Monday, 11 November 2013

Oracle - CRM Web Service Integration



Web Definition: -Oracle CRM is a customer relationship management application developed by Oracle Corporation. Oracle CRM includes Oracle and Peoplesoft products but leads with Siebel CRM and CRM on Demand.”

In other words Oracle CRM is highly customizable customer relationship management system which exposes a variety of features and customization option to modify and customize system as per your industry need and to manage at the scale where it fits to you or your business needs.

It provided various interfaces and integration points to connect and communicate with system and allows other platforms and frameworks to associate/integrate with system.

Here we are going to discuss in general procedure and steps flow to integrate and communicate with exposed web services of Oracle CRM.

Following are the steps to integrate a web service of Oracle CRM in .Net web application. (For ex integration of Forecast web service)
  
1. Login to admin interface
      2. Select and download web service WSDL.
      3.  Select and download required schema files.
      4. Add reference in web application.
      5. Communication with Oracle CRM using service reference.

1.       Login to admin interface.


There are two environments provided by Oracle CRM as below.

Stage Environment – for development

Production Environment – for live

To get access to above environments you need to have valid credentials. Use your admin credentials to login into above environments to proceed to get web service WSDL. Click on Admin tab in top right menu after login. 

2.       Select and download web service WSDL.


Find “Integration” section and click on “Web Service Administration” tab. This will navigate you to Web Services Administration page. Here you are provided with the option to select service type/version and associated WSDL to download.

(Note: Prefer to use Web Services V2.0 to get latest methods and features to integrate however Web Services V1.0 is also sufficient with some less option.

For example we are using Web Services V2.0 for Forecast service. Forecast is also termed as Opportunity in other term in Oracle CRM.)

Select Web Services V2.0 in select service dropdown and click on Go.
Select desired web service (for example Forecast) in WSDL object list and download WSDL (custom/generic) as per your requirement.

(Note: Custom WSDL available for download with some custom object and properties added into it while Generic WSDL contains generic methods and objects to be used.)

3.       Select and download required schema files.


Schema file are XSD files which exposes available types and properties used in associated WSDL objects. When downloading WSDL then download schema as well (if available) for selected WSDL object (i.e. Forecast).

You can search in schema and can ensure availability of property/type if you want to access specific property of object. 

4.       Add reference in web application.


Create a web application and select project in solution explorer. Right click and select Add Web Reference.
Enter path of WSDL of Forecast/Opportunity web service in URL and go. Now name this web reference (i.e. OpportunitySvc) and add reference.

(Note: Please keep schema file in same folder while adding web reference.)

We are good to go to access service to communicate with Oracle CRM as the configuration and required web references are already added. 


5.       Communication with Oracle CRM using service reference.


As we are already done with reference adding process now we need to implement code to call service method to communicate with Oracle CRM. There is specific process/flow of action while calling method of web service to communicate with Oracle CRM as below.

a) Login and create session: - We need to call login command of integration service to login  into    service and generate session id to be used while calling web service method to uniquely identify request and avoid unauthorized access to data. Below is code snippet to call login command and fetch generated session id for subsequent service calls.

HttpWebRequest request = null;
HttpWebResponse response = null;
//WebHeaderCollection webHeaderCollection = null;
string sessionID = string.Empty;
string baseURL = string.Empty;
//Login Credentials
baseURL = ConfigurationManager.AppSettings["WebServiceURL"];
/* WebServiceURL for stage:-  "https://secure-ausomxhna.crmondemand.com/Services/Integration"; */

//Creating web request
request = (HttpWebRequest)HttpWebRequest.Create(new Uri(baseURL + "?command=login"));

request.Method = "GET";
request.Headers.Set("UserName", ConfigurationManager.AppSettings["username"]);
request.Headers.Set("Password", ConfigurationManager.AppSettings["password"]);
try
{
//Getting the response
       response = (HttpWebResponse)request.GetResponse();
       Stream sr = response.GetResponseStream();
       //Retrieve session ID
       char[] separator = { ';' };
       String[] headers = response.Headers["Set-Cookie"].Split(separator);
       for (int i = 0; i <= headers.Length - 1; i++)
       {
              if (headers[i].StartsWith("JSESSIONID"))
              {
                     separator[0] = '=';
                     sessionID = headers[i].Split(separator)[1];
                     break;
              }
       }
       Session["SessionID"] = sessionID;
       sr.Close();
       response.Close();
}
catch (WebException we)
{
       // Log exception
}
  
b)      Call actual web service method: - During calling to web service we need to pass generated session id (generated from login step (a)) with every request to Web service URL.
Refer steps with code snippet as below to get better understanding of web service call to fetch Forecast/Opportunity data using id from Oracle CRM.
Step 1: - Create an object for Forecast/Opportunity to call method and fetch data and provide service URL with generated login session id.

string sessionID = Convert.ToString(Session["SessionID"]);
Opportunity opportunityObj = new Opportunity();
opportunityObj.Url = ConfigurationManager.AppSettings["WebServiceURL"] + ";jsessionid=" + sessionID;

Step 2: - Create an input object for Opportunity query to fetch specific fields/properties/objects data as provided in this input object.

OpportunityQueryPage_Input input = new OpportunityQueryPage_Input();

Step 3: - Create a query type object to specify query/condition fetch Opportunity data for matching query/condition as specified in it. Prepare condition and add as value in this object.
For example condition added below to fetch opportunity records where record id is equal to specified opportunity id.

OpportunitySvc.queryType qtid = new OpportunitySvc.queryType();
//Set opportunity id for query.
qtid.Value = "= '" + opportunityID + "'";

Step 4: - Create a query object (suffixed with ‘Query’) for opportunity to specify objects and properties to fetch data for. This object communicates to the service about data which need to be return in output.

OpportunityQuery opp = new OpportunityQuery();

opp.Id = qtid; //Opportunity ID
opp.AccountId = new OpportunitySvc.queryType(); //Account ID
opp.CreatedDate = new OpportunitySvc.queryType();
opp.CloseDate = new OpportunitySvc.queryType();
opp.OwnerId = new OpportunitySvc.queryType();
    
(Note: Initialize all those properties to queryType which are supposed to be fetched with data for records in output object because this initialization informs service about fields/properties/objects to return data for.)

Step 5: - Create query object for List of opportunity and add your prepared opportunity query object in it because we need to inform service that we need data for this object and specified properties (in step 4) of each opportunity element of this list.

ListOfOpportunityQuery listobj = new ListOfOpportunityQuery();
listobj.Opportunity = opp;
input.ListOfOpportunity = listobj;

(Note: you can review output object first (in our example output object of opportunity query), to know about available objects and properties to decide that which objects/properties we need to query to get desired data. We need to access opportunity object which is available in list form in output object which triggers us to initialize/create query object of list of opportunity.)

Step 6: - Call query method of service object by passing query input object which will return respective output object with data as queried in provided input object.
                 
//Call service to get output.
OpportunityQueryPage_Output output = opportunityObj.OpportunityQueryPage(input);

Step 7: - Validate returned object for desired data availability and process returned data as per business requirement.

if (output.ListOfOpportunity != null && output.ListOfOpportunity.Opportunity.Length > 0)
{
       strAccountID = output.ListOfOpportunity.Opportunity[0].AccountId;
      
       //Use data as per business requirement. 
}
               
      c)       Logoff and finish session: - Logoff command need to be called when we are done with our communication with Oracle CRM to finish session and any avoid unauthorized access to data using earlier generated session. Below is code snippet to call logoff command to finish session for service calls.

try
{
string baseURL1 = ConfigurationManager.AppSettings["WebServiceURL"]; //"https://secure-ausomxhna.crmondemand.com/Services/Integration";
       string logoffUrlString = baseURL1 + "?command=logoff";
       HttpWebRequest req = (HttpWebRequest)WebRequest.Create(logoffUrlString);

       // make the HTTP call
       HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

       string sessionID = string.Empty;
       Session["SessionID"] = null;

 }
 catch (Exception ex)
 {
       //Log exception;
 }


Points to keep in mind during Oracle CRM web service integration.

  
1.       Query objects are suffixed with “Query”.
2.       Query Input objects are suffixed with “QueryPage_Input”.
3.       Query Output objects are suffixed with “QueryPage _Output”.
4.       Query methods to fetch results/output on the basis of query input object are suffixed with “QueryPage” (for example refer step 6). 
5.       All properties and object must be initialized to queryType to fetch data for that specific field/property/object from Oracle CRM. For example as specified above in step 4 and 5.
5.       If some property is not initialized to queryType in that case service is not going to return data for that field/property/object.
6.       Oracle CRM provides highly customizable options and this customization also reflects in available web services. That means which fields of an object should be available in web service (WSDL) to query or access this could be managed from Oracle CRM admin tool. How could it be done? that is a different chapter. 

So friends, now you are good to go to explore more and try at your end to integrate/access web service in your application to fetch data from Oracle CRM. 

Share your feedback if this blog helps you in anyways and suggestions are most welcome to improve as well.