Access REST APIs in Oracle Fusion Using Cloud Groovy

 The article gives the sample scripts with the use of Oracle Fusion Cloud REST APIs in Groovy. it includes all the crud operations and some advanced methods to access the REST APIs.


Register REST API in Webservices under the application composer tool. Refer below article to register the REST API.

  • Always prefer not to use the web services, instead consider using Groovy functions for CRUD operations as a web service call is more expensive than a Groovy call in terms of performance.
  • Test the web service thoroughly in POSTMAN/any other client with samples and then use it in Groovy.
  • Use try-catch when calling REST calls to handle errors.

GET Operation:

Simple GET Call 1: 

To get or Read a specific record use the below code. we don't need to pass content type etc.

URL to Register in Webservices:

https://abcd-dev1.fa.ap2.oraclecloud.com:443/crmRestApi/resources/latest/opportunities/##OptyNumber##

 def OptyNumber = OptyNumber  
 def x = adf.webServices.GET_Opportunity.GET(OptyNumber)  
 setAttribute('A_Rez_c',x)  
 def saleStageId= x.items?.first().SalesStageId

GET Call Method 2:

Using the below method we can pass the parameters to using dynamicQueryParams. Here we are not passing key value to the URL.

URL to Register in Webservices:

https://abcd-dev1.fa.ap2.oraclecloud.com:443/crmRestApi/resources/latest/contacts

 def partyId =''  
 def conn = adf.webServices.ContactREST  
 def queryParams 
 queryParams=['finder':'PrimaryKey;PartyId=300000000660001','fields':'PartyId,PartyNumber','onlyData':'true']  
 conn.dynamicQueryParams = queryParams  
 def contacts = conn.GET()  
 def output = contacts.items?.first().PartyId  
 throw new oracle.jbo.ValidationException(output.toString()) 

GET Call Method 3:

URL to Register in Webservices:

https://<myHost>/crmRestApi/resources/latest/##ObjectName##/##ObjectKey## 
 def ObjService = adf.webServices.getRecord   
 def objectName = 'accounts'  
 def objectID = '288666'      //Pass the record identifier as dynamic if required  
 def queryParms = [:]   
 queryParms.fields='PartyId,OrganizationName' // Provide required fields only. skipping queryParms all fields will come in the response.  
 queryParms.onlyData='true'   
 ObjService.dynamicQueryParams = queryParms   
 def response = ObjService.GET(objectName, objectID)   
 def output = response.items?.first().PartyId

GETCall with more than one result 4:

URL to Register in Webservices:

https://<myHost>/crmRestApi/resources/latest/##ObjectName##/##key##/child/##childName##  

def ObjService = adf.webServices.WSAccessChildRecords //create  
 String objectName="deals"  
 String objectID = DealId  
 String ChildName = "DealAttachments"  
 def headers = [:]   
 headers."Content-Type" = "application/vnd.oracle.adf.resourceitem+json"  
 ObjService.requestHTTPHeaders=headers  
 def dealattachments=ObjService.GET(objectName.toString(),objectID.toString(),ChildName.toString())  
 def rec  
 def dealAttach=[]   
 for (item in dealattachments.items) // loopthrough with the results  
 {   
  dealAttach.add(item.FileName)  
 }   


POST Operation:

URL to Register in Webservices:

https://abcd-dev1.fa.ap2.oraclecloud.com:443/crmRestApi/resources/latest/contacts

 def partyId =''  
 def conn = adf.webServices.ContactREST  
 def POST_param =  
 [  
  FirstName :"XXXXXX",  
  LastName :"XXXXXX",  
 ]  
 def httpHeaders=['Content-Type':'application/vnd.oracle.adf.resourceitem+json']  
 conn.requestHTTPHeaders=httpHeaders  
 def newOpty = conn.POST(POST_param)  
 def output = newOpty.PartyId  
 println("PartyId of the New contact is: " + output )  

PATCH Operation:

URL to Register in Webservices:

https://<myHost>/crmRestApi/resources/latest/##ObjectName##/##ObjectKey## 
 def​ opty ​=​ adf​.​webServices​.​PATCH_Opportunity  
 try{  
  ​// Create new Opportunity object by passing Oportunity name  
  ​// Set Content-Type request header  
  ​def​ ​OptyNumber​ ​=​ ​OptyNumber  
  ​def​ ​Name​ ​=[​Name​:​'PATCH_NEW_NAME']  
  ​def​ httpHeaders​=[​'Content-Type'​:​'application/vnd.oracle.adf.resourceitem+json']  
  opty​.​requestHTTPHeaders​=​httpHeaders  
  ​def​ newOpty ​=​ opty​.​PATCH​(​OptyNumber​,​ ​Name)  
 }​catch​(​Exception​ e​){  
  println​(​"Headers:"​+​opty​.​responseHTTPHeaders)  
  println​(​"Status:"​+​opty​.​statusCode)  
  println​(​"Error:"​+​e)  
 }  

DELETE Operation:

URL to Register in Webservices:

https://<myHost>/crmRestApi/resources/latest/opportunities/##ObjectKey## 
 def​ opty ​=​ adf​.​webServices​.​OpportunityWS
 try
 {  
  ​def​ ​OptyNumber​ ​=​ ​OptyNumber  
  ​def​ response​=​ opty​.​DELETE​(​OptyNumber​)
 }
​catch​(​Exception​ e​)
{  
  println​(​"Headers:"​+​opty​.​responseHTTPHeaders)  
  println​(​"Status:"​+​opty​.​statusCode)  
  println​(​"Error:"​+​e)  
 }  


External API Call :

External API methods, Authorization, body & response payloads might be different for each web service.

 def qtricsWS=adf.webServices.QualtricsSurvey  
 qtricsWS.requestHTTPHeaders['Content-Type']='application/json'  
qtricsWS.requestHTTPHeaders['x-api-token']='59223240-d470-11e9-9193-353535'
qtricsWS.requestHTTPHeaders['Accept-Language']='es-ES'
def Qtricsres = qtricsWS
.POST(strMap);


Get Count, links, and access other attributes  from the response:

Sample Response in Postman:

 {  
   "items": [  
     {  
       "AttachedDocumentId": 300000528626077,  
       "LastUpdateDate": "2023-09-15T09:50:12.448+00:00",  
       "DatatypeCode": "FILE",  
       "FileName": "rejected-Customer Contacts.csv",  
       "links": [  
         {  
           "rel": "self",  
           "href": "https://abcd-dev1.fa.ap2.oraclecloud.com:443/crmRestApi/resources/11.13.18.05/deals/300000361874315/child/DealAttachments/00020000000EACED00057708000110D950F0F19D0000000EACED00057708000110D950F0F19C",  
           "name": "DealAttachments",  
           "kind": "item"  
         },  
         {  
           "rel": "canonical",  
           "href": "https://abcd-dev1.fa.ap2.oraclecloud.com:443/crmRestApi/resources/11.13.18.05/deals/300000361874315/child/DealAttachments/00020000000EACED00057708000110D950F0F19D0000000EACED00057708000110D950F0F19C",  
           "name": "DealAttachments",  
           "kind": "item"  
         }  
       ]  
     }  
   ],  
   "count": 3,  
   "hasMore": false,  
   "limit": 25,  
   "offset": 0,  
   "links": [  
     {  
       "rel": "self",  
       "href": "https://abcd-dev1.fa.ap2.oraclecloud.com:443/crmRestApi/resources/11.13.18.05/deals/300000361874315/child/DealAttachments",  
       "name": "DealAttachments",  
       "kind": "collection"  
     }  
   ]  
 }  


Groovy:

  def OptyNumber = OptyNumber   
  def x = adf.webServices.GET_Opportunity.GET(OptyNumber)   
 def docid= x.items?.first()?.AttachedDocumentId  
  def selflink= x.items?.links?.first()?.href  
  def count= x.count  
  def hasmore= x.hasMore  

Comments

Popular posts from this blog

Register REST API in Webservices under the application composer tool.

OIC to perform the Integration