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.