Clarity

 View Only

 Calling REST API in Gel Script to update task fields

Jump to Best Answer
Shavini Marapana's profile image
Shavini Marapana posted Feb 19, 2021 10:45 AM

Hi ,

I'm really new this Rest API developments. I want to update few task fields via Rest Api and I have below code. 

When I update task using Postman it correctly updating my task. But when I call this from gel script it is not working. Could you please check if there is any issue with the below code. I don't get any errors. it simply my process is not running at all. 

 <gel:script xmlns:core="jelly:core" xmlns:email="jelly:email" xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
                      xmlns:sql="jelly:sql" xmlns:util="jelly:util" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xog="http://www.niku.com/xog">
     
     <core:set var="basicAuth" value="Basic YWRtaW46VE1NiE=********************"/>
     
     <core:set var="restEndPoint" value="http://************************************/ppm/rest/v1/projects/5459986/tasks/6128252"/>
     <core:new className="java.net.URL" var="restURL">
     <core:arg type="java.lang.String" value="${restEndPoint}"/>
     </core:new>
     <core:invoke var="connection" method="openConnection" on="${restUrl}"/>
     
     
     <core:expr value='${connection.setRequestMethod("PATCH")}'/>
     <core:expr value='${connection.setRequestMethod("x-api-ppm-client", "ID001")}'/>
     <core:expr value='${connection.setRequestProperty("Authorization", basicAuth)}'/>
     <core:expr value='${connection.setRequestProperty("Content-type", "application/json")}'/>
     <core:expr value='${connection.setRequestProperty("Accept", "application/json"}'/>
     <core:expr value='${connection.setRequestProperty("Connection", "keep-alive")}'/>
     <core:expr value='${connection.setDoOutput("true")}'/>
      
     <core:set var="requestJSON" escapeText="false">
     
      {
             
      "p_taskRAG": "LSE_BLUE",
            
      "percentComplete": "1"
     }
     </core:set>
     
     <core:invoke var="outputStream" method="getOutputStream" on="${connection}"/>
     <core:new className="java.io.OutputStreamWriter" var="outputStreamWriter">
     <core:arg type="java.io.OutputStream" value="${outputStream}"/>
     </core:new>
     <core:expr value="${outputStreamWriter.write(requestJSON)}"/>
     <core:expr value="${outputStreamWriter.close()"/>
     
     <core:invoke var="restOutput" method="getInputStream" on="${connection}"/>
     <core:invoke var="restResponseCode" method="getResponseCode" on="${connection}"/>
     <core:choose>
     <core:when test="${restResponseCode == '200'}">
     <gel:log> Sucessfully updated task </gel:log>
     
     
     <core:invokeStatic className="org.apache.cxf.helpers.IOUtils" method="toString" var="projectOutputString">
     <core:arg type="java.io.InputStream" value = "{restOutput}"/>
     <core:arg value = "UTF-8"/>
     </core:invokeStatic>
     <core:expr value="${restOutput.close()}"/>
     <core:new className="org.json.JSONObject" var="projectJsonObject">
     <core:arg type="java.lang.String" value='${projectOutputString}'/>
     </core:new>
     <core:set var="InternalId" value="$projectJsonObject.get('_internalId')"/>
     <gel:log> ID: ${InternalId} </gel:log>
     </core:when>
     <core:otherwise>
     <gel:log>Failed to update task</gel:log>
     </core:otherwise>
     </core:choose>
                    </gel:script>
Suman Pramanik's profile image
Broadcom Employee Suman Pramanik Best Answer
This issue was also reported via Support and during debugging of the gel script it was found that connection method was not correctly getting called. We built a sample gel script to update task and it works fine.

Sharing the script as it may help others 

<gel:script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:wss="http://www.boomi.com/connector/wss"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xog="http://www.niku.com/xog"
xmlns:x="jelly:org.apache.commons.jelly.tags.xml.XMLTagLibrary"
xmlns:util="jelly:util" xmlns:sql="jelly:sql"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:jelly="jelly:core"
xmlns:nikuq="http://www.niku.com/xog/Query"
xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
xmlns:file="jelly:com.niku.union.gel.FileTagLibrary"
xmlns:core="jelly:core">

<!--***************************************Get the session from the system Start ***********************************-->
<gel:parameter var="gel_host_user" default="admin"/>
<gel:parameter var="gel_host_pass" default="xxxxx"/>
<gel:setDataSource dbId="niku"/>
<!--Set parameter here End -->
<!--***************************************Get the session from the system End ***********************************-->



<!-- Generate SessionID -->
<core:new className="com.niku.union.security.DefaultSecurityIdentifier" var="secId" />
<core:invokeStatic var="userSessionCtrl" className="com.niku.union.security.UserSessionControllerFactory" method="getInstance" />
<core:set var="secId" value="${userSessionCtrl.init(gel_host_user, secId)}"/>
<core:set var="sessionID" value="${secId.getSessionId()}"/>

<core:choose>
<core:when test="${sessionID == null}">
<gel:log level="ERROR">Cannot login to Clarity XOG. Check username.</gel:log>
</core:when>
<core:otherwise>
<gel:log>SessionID: ${sessionID}</gel:log>
</core:otherwise>
</core:choose>



<!--core:invoke method="setHeader" on="${patch}"> <core:arg type="java.lang.String" value="Cookie"/>
<core:arg type="java.lang.String" value="${v_Cookie}"/> </core:invoke-->



<core:new var="patch" className="org.apache.http.client.methods.HttpPatch">
<core:arg value="http://clarity/ppm/rest/v1/projects/5001083/tasks/5001170" type="java.lang.String"/>
</core:new>
<core:invoke on="${patch}" method="setHeader">
<core:arg value="Authorization" type="java.lang.String"/>
<core:arg value="Basic YWRtaW46YWRtaW4=" type="java.lang.String"/>
</core:invoke>

<core:invoke on="${patch}" method="setHeader">
<core:arg value="Content-Type" type="java.lang.String"/>
<core:arg value="application/json" type="java.lang.String"/>
</core:invoke>

<core:set var="createProject" escapeText="false">{"percentComplete": "1", "name": "Initiation Phase2" }</core:set>
<!-- <core:set var="createProject" escapeText="false">{"code": "${row.CODE}", "name": "${row.NAME}", "description":"${row.DESCRIPTION}" }</core:set> -->

<core:new var="entity" className="org.apache.http.entity.StringEntity">
<core:arg value="${createProject}" type="java.lang.String"/>
</core:new>
<gel:log>entity: ${entity}</gel:log>

<core:invoke on="${patch}" method="setEntity">
<core:arg value="${entity}" type="org.apache.http.entity.StringEntity"/>
</core:invoke>
<gel:log>patch: ${patch}</gel:log>

<core:invokeStatic var="httpclient" className="org.apache.http.impl.client.HttpClients" method="createDefault"/>
<core:invoke var="response" on="${httpclient}" method="execute">
<core:arg value="${patch}" type="org.apache.http.client.methods.HttpPatch"/>
</core:invoke>
<gel:log>response: ${response}</gel:log>

<core:invoke var="entityResponse" on="${response}" method="getEntity"/>
<core:invoke var="ResponseStatus" on="${response}" method="getStatusLine"/>
<gel:log>ResponseStatus getProtocolVersion: ${ResponseStatus.getProtocolVersion()}</gel:log>
<gel:log>ResponseStatus getReasonPhrase: ${ResponseStatus.getReasonPhrase()}</gel:log>
<gel:log>ResponseStatus getStatusCode: ${ResponseStatus.getStatusCode()}</gel:log>
<gel:log>entityResponse: ${entityResponse}</gel:log>
<core:set var="void" value="${response.close()}"/>

<gel:log>exception::${exception}</gel:log>
</gel:script>
Awadhesh Dammani's profile image
Awadhesh Dammani
Could you please help us to give more specific Input on Issue..
Instead of One Core set use Two:

<core:set value="${jsonObject.getJSONObject('QueryResult').get('Results').getJSONObject(0).get('ObjectID')}" var="clarityObjectID"/>