Thursday, 19 December 2013

Update Data In Oracle CRM



Hello Friends, I am back again with another part of integration of Oracle CRM. In my previous blog of this topic we have discussed, how to query/fetch data from Oracle CRM using exposed web services. Now this is the time to update data back in Oracle CRM using web service.

Let’s take example of forecast/opportunity again. We have already fetched opportunity data from Oracle CRM and suppose we have processed it and now we want to update some data (suppose processed reference number – i.e. URN (unique reference number)) back in Oracle CRM for same forecast/opportunity record.

Assuming web service references are already added in project and data fetched from Oracle CRM for forecast/opportunity using opportunity ID. Data already processed and finally we have generated URN number and associated Opportunity ID in session and this URN need to be updated in Oracle CRM

As we discussed earlier, there is specific process/flow of action while calling method of web service to communicate with Oracle CRM. So the flow/steps will be same here as well 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 – to update data in Oracle CRM repository: - 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 code snippet as below to get better understanding of web service call to update generated URN number in Oracle CRM for specific Forecast/Opportunity record using id.
Step 1: - Extract generated session id (for web service communication) and processed data (URN number) from session object.

string opportunityID = Convert.ToString(Session["opportunityID"]);    
string sessionID = Convert.ToString(Session["SessionID"]);
      
Step 2: - Create an object for Forecast/Opportunity to call method and update data and assign service URL with generated login session id.

OpportunitySvc.Opportunity opportunityObj = new OpportunitySvc.Opportunity();
opportunityObj.Url = ConfigurationManager.AppSettings["WebServiceURL"] +   ";jsessionid=" + sessionID;

Step 3: - Create an array of Opportunity Data object for single element to hold data which we are planning to update in Oracle CRM. Now assign opportunity ID and generated URN in object properties.

OpportunitySvc.OpportunityData[] oppupdata = new OpportunitySvc.OpportunityData[1];
oppupdata[0] = new OpportunitySvc.OpportunityData();
oppupdata[0].Id = opportunityID;
oppupdata[0].stURN_FOR_CREDIT_CARD = URN.ToString();

Step 4: - Create data object for List of opportunity data and add your prepared opportunity data object array in it which we need to update in Oracle CRM.

OpportunitySvc.ListOfOpportunityData lopd = new OpportunitySvc.ListOfOpportunityData();

lopd.Opportunity = oppupdata;

Step 5: - Create an input object for input data of opportunity update call to update processed data (URN number) in specific fields/properties/objects data of specific forecast/opportunity record.

OpportunityUpdate_Input uinput = new OpportunityUpdate_Input();
uinput.ListOfOpportunity = lopd;
Step 6: - Create an output object to receive output of opportunity update call after updating processed data (URN number) in specific fields/properties/objects data of specific forecast/opportunity record.

OpportunityUpdate_Output uoutput = opportunityObj.OpportunityUpdate(uinput);

Step 7: - Validate returned output object and verify for valid information in it to decide whether update call was successful or failed and display/process success or failure code accordingly.

if (uoutput != null && uoutput.ListOfOpportunity != null && uoutput.ListOfOpportunity.Opportunity != null && uoutput.ListOfOpportunity.Opportunity.Length > 0 && uoutput.ListOfOpportunity.Opportunity[0] != null)
{
                        //URN saved successfully.
litMessage.Text = "Thanks for the payment. Please note your URN for any communication.";
ClearSession();
}
else
{
litMessage.Text = "Sorry!! Your payment processed successfully but failed to update reference number in repository. Please try by refreshing this page again.";
}

You can also verify that data updated in Oracle CRM or not by visiting admin tool of Oracle CRM and verify specific forecast/opportunity record there for updated value.
              
      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 to avoid any 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;
 }

Highlight on used objects for Oracle CRM web service integration to update data.
      1.       Data objects are suffixed with “Data”.
      2.       Input objects are suffixed with “_Input”.
      3.       Output objects are suffixed with “_Output”.
      4.       Update Input objects are suffixed with “Update_Input”.
      5.       Update Output objects are suffixed with “Update_Output”.
      6.       Update Methods are suffixed with “Update” to update associated entity data in Oracle CRM 

So, here we have simply updated a value in Oracle CRM using update object of respective entity (i.e. Forecast/opportunity in this example)

Please do share your thoughts and feed backs to improve. Till than enjoy integration.

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.