Thursday, August 8, 2013

Work with Salesforce sObject for easy transform

How to work with Salesforce object for easy transform?


Salesforce WSDL are designed to maximize flexibility by using generic sObject abstract type to represent any objects. (complex types). But CRUD operations on generic arrays of objects that are not strongly-typed, which is undesirable from an OO perspective. Here I show how Fusion Middleware can identify the context of the generic objects & cast them to strongly-typed (concrete) objects.


Step 1: Add the Salesforce object, to the latest version of  the Salesforce enterprise wsdl, that we would like to expose and utilize in XSL transformation designer. The objects are created in your custom namespace (noted as YOURCOMPANY below). Be sure to name the element with the EXACT name of the type.



 <schema elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:YOURCOMPANY.enterprise.soap.sforce.com" xmlns:ens="urn:sobject.enterprise.soap.sforce.com"> <import namespace="urn:sobject.enterprise.soap.sforce.com"/> <element name="Opportunity" type="ens:Opportunity"></element> </schema>
           
       
    

Step 2: Within the scope that you will be working with such object, create a variable of element type of what you created in Step 1.


<variable name="opp" element="ns10:Opportunity"/>

in this case, xmlns:ns10="urn:YOURCOMPANY.enterprise.soap.sforce.com"



Step 3: TransformToObj.xsl can be reused in each composite to map the custom object back to SObject.



 
?>
               xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/"
               xmlns:xp20="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.Xpath20"
               xmlns:mhdr="http://www.oracle.com/XSL/Transform/java/oracle.tip.mediator.service.common.functions.MediatorExtnFunction"
               xmlns:bpel="http://docs.oasis-open.org/wsbpel/2.0/process/executable"
               xmlns:oraext="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.ExtFunc"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:dvm="http://www.oracle.com/XSL/Transform/java/oracle.tip.dvm.LookupValue"
               xmlns:hwf="http://xmlns.oracle.com/bpel/workflow/xpath"
               xmlns:wns="urn:YOURCOMPANY.enterprise.soap.sforce.com"
               xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
               xmlns:med="http://schemas.oracle.com/mediator/xpath"
               xmlns:ids="http://xmlns.oracle.com/bpel/services/IdentityService/xpath"
               xmlns:bpm="http://xmlns.oracle.com/bpmn20/extensions"
               xmlns:xdk="http://schemas.oracle.com/bpel/extension/xpath/function/xdk"
               xmlns:xref="http://www.oracle.com/XSL/Transform/java/oracle.tip.xref.xpath.XRefXPathFunctions"
               xmlns:fns="urn:fault.enterprise.soap.sforce.com"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
               xmlns:tns="urn:enterprise.soap.sforce.com"
               xmlns:ora="http://schemas.oracle.com/xpath/extension"
               xmlns:socket="http://www.oracle.com/XSL/Transform/java/oracle.tip.adapter.socket.ProtocolTranslator"
               xmlns:ens="urn:sobject.enterprise.soap.sforce.com"
               xmlns:ldap="http://schemas.oracle.com/xpath/extension/ldap"
               exclude-result-prefixes="xsi xsl wns fns xsd soap tns ens bpws xp20 mhdr bpel oraext dvm hwf med ids bpm xdk xref ora socket ldap">
<!--<xsl:output method="xml" indent="yes"/>--> <xsl:template match="/"> <tns:update> <tns:sObjects> <xsl:attribute name="xsi:type" namespace="http://www.w3.org/2001/XMLSchema-instance"> <xsl:value-of select="local-name(/child::node())"/> </xsl:attribute> <xsl:for-each select="/child::node()/child::*"> <xsl:copy-of select="."/> </xsl:for-each> </tns:sObjects> </tns:update> </xsl:template> </xsl:stylesheet>
 
 
   
     
       
         
       
       
         
       
     
   
 



Wednesday, August 7, 2013

PythonWebAppInterfaceSalesforceRESTAPI

This implementation is a simple Python Web application that authenticates the user to Force.com via Oauth 2.0, then creates, reads, updates, and deletes Force.com records via the new API. It interfaces  with the Force.com REST API.

Need to install:
  • Gunicorn Web Server – Django has default HTTP server, but Gunicorn is more production-ready embedded webserver.
  • Django Rest Framework – Provides endpoint for client to interface. Provides Web browseable API, out of the box Authentication policies including Oauth2.
  • Python-Requests library – Get content, add in caching
  • Python-Logging module
  • OpenSSH-server – authentication
  • Virtualenv - Create a new virtual env using virtualenv to make sure package configuration is keep nicely isolated from any other projects.
  • A Force.com account on EE with password never expired option
  • Configure Remote Access in Salesforce – Generates clientId, clientSecret to use in python request.

Salesforce Object Design and Restful API

  • Partner_Locator__c and Partner_Locator_Country__c has a master to detail relationship.
  • Expose Apex classes as REST Web Services via annotations @RestResource
  • Apex Rest supports two formats for representations of resources: JSON and XML


Web Service Authorization with Oauth

  • Oauth allows Partner Locator web app as the client operate on behalf of a user, without revealing that user’s credentials, such as username and password, to the client. The client first sends the user to an authorization server (login.salesforce.com), which authenticates the user, obtains the user’s authorization, and issues an access token which the client can use in interacting with a resource server (instance url returned in response).

Coding and Configuration Specifics:


  • requests.post : Requests is an Apache2 Licensed HTTP library. Requests makes integration with web services seamless. There is no need to manually add query strings to URLs, or to form-encode POST data. Keep-alive and HTTP connection pooling are 100% automatic.
  • @api_view(['GET']) : Use Django Response class @api_view function for views that return Response objects to ensure that the view can perform content negotiation and select the appropriate renderer for the response, before it is returned from the view.
  • @cache_page(60) : caching the output of individual views. Django.views.decorators.cache defines a cache_page decorator that will automatically cache the view’s response.
  • rest_framework.renderers.JSONPRenderer : A list or tuple of parser classes, that determines the default set of renderers that may be used when returning a Response object. Django REST framework provides JSONP support to get around cross-domain browser access issue.
  • from django.conf.urls import patterns, url : URLConf is a simple mapping between URL patterns (simple regular expressions) to Python functions (views).