AnsweredAssumed Answered

Execute XOG Write via Javascript Function

Question asked by AlistairMiller on Aug 30, 2017
Latest reply on Sep 5, 2017 by AlistairMiller

OK folks, I'm stuck and hoping someone can share their knowledge.  I'm launching a javascript function when an event occurs on a web page. The function will update a Task status (using XOG).

 

Everything is happy until I try to initialize, aim and fire my XMLHTTPRequest, and I wonder if someone can point out what I've missed.  Here's the function:

function ExecuteTaskUpdate(taskID, taskStatus)
{

//Obtain Server name from URL
var url = window.document.URL;
var ServerName = url.substring(7,url.indexOf("niku/")-1);
var xog_url = "https://" + ServerName + "/niku/xog";

// Retrieve Project ID from URL
var n = url.length;
var n = (n-7);
var projID = url.substring(n, (n+7));

// Get User's SessionID from Cookie
UserSessionID=window.clarity.session.sessionId;

//Query the DB for the set of Tasks
var TaskQueryCode="fon_proj_phase_task";
var TaskQueryFilterField="task_int_id";
var TaskQueryFilterValue= taskID;

//Query Callback is not being used
var TaskQueryCallback="";

//Execute the query using the logged user's sessionId
var Result=ExecuteClarityQueryAsUser(ServerName, TaskQueryCode, TaskQueryFilterField, TaskQueryFilterValue, TaskQueryCallback);
var returnedString = String(Result.responseText);

// Creates the XML object with the TextResponse from the Query
var xmlobject = crossBrowserXMLLoad(Result.responseText);

//Get records returned
var taskitems = xmlobject.getElementsByTagName("Record");
totaltasks = taskitems.length;

for (var i = 0 ; i < totaltasks ; i++)
{

var task = taskitems[i];
var projectID = task.getElementsByTagName("project_id")[0].firstChild.nodeValue;
var projectName = task.getElementsByTagName("project_name")[0].firstChild.nodeValue;
var taskCode = task.getElementsByTagName("task_id")[0].firstChild.nodeValue;
var taskName = task.getElementsByTagName("task_name")[0].firstChild.nodeValue;
var taskStart = task.getElementsByTagName("start_date")[0].firstChild.nodeValue;
var taskFinish = task.getElementsByTagName("finish_date")[0].firstChild.nodeValue;
var taskOutline = task.getElementsByTagName("outline_level")[0].firstChild.nodeValue;

//Build up the XOG Write File
if (taskStatus == 0)
{
var xw = "?<xml version=\"1.0\" encoding=\"utf-8\"?>" +
'<soapenv:Envelope ' +
' xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"' +
' xmlns:core="jelly:core"' +
' xmlns:file="jelly:com.niku.union.gel.FileTagLibrary" ' +
' xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary" ' +
' xmlns:soap="jelly:com.niku.union.gel.SOAPTagLibrary" ' +
' xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ' +
' xmlns:sql="jelly:sql" ' +
' xmlns:util="jelly:util" ' +
' xmlns:xog="http://www.niku.com/xog" ' +
' xmlns:xsd="http://www.w3.org/2001/XMLSchem\" ' +
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +
' <soapenv:Header> ' +
'<xog:Auth>' +
' <xog:SessionID>' + UserSessionID + '</xog:SessionID>' +
'</xog:Auth>' +
'</soapenv:Header>' +
'<soapenv:Body>' +
'<NikuDataBus xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../xsd/nikuxog_project.xsd">' +
'<Header action="write" externalSource="NIKU" objectType="project" version="15.2.0.213"/>' +
'<Projects>' +
'<Project name="' + projectName + '" projectID="' + projectID + '">' +
'<Tasks>' +
'<Task finish="' + taskFinish + '" internalTaskID="' + taskID + '" name="' + taskName + '" outlineLevel="' + taskOutline + '" percComp="' + "0" + '" start="' + taskStart + '" status="' + "0" + '" taskID="' + taskCode + '"/>' +
'</Tasks>' +
'</Project>' +
'</Projects>' +
'</NikuDataBus>' +
'</soapenv:Body>' +
'<soapenv:Envelope>'
} else if (taskStatus == 1)
{
var xw = "?<xml version=\"1.0\" encoding=\"utf-8\"?>" +
'<soapenv:Envelope ' +
' xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"' +
' xmlns:core="jelly:core"' +
' xmlns:file="jelly:com.niku.union.gel.FileTagLibrary" ' +
' xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary" ' +
' xmlns:soap="jelly:com.niku.union.gel.SOAPTagLibrary" ' +
' xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ' +
' xmlns:sql="jelly:sql" ' +
' xmlns:util="jelly:util" ' +
' xmlns:xog="http://www.niku.com/xog" ' +
' xmlns:xsd="http://www.w3.org/2001/XMLSchem\" ' +
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +
' <soapenv:Header> ' +
'<xog:Auth>' +
' <xog:SessionID>' + UserSessionID + '</xog:SessionID>' +
'</xog:Auth>' +
'</soapenv:Header>' +
'<soapenv:Body>' +
'<NikuDataBus xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../xsd/nikuxog_project.xsd">' +
'<Header action="write" externalSource="NIKU" objectType="project" version="15.2.0.213"/>' +
'<Projects>' +
'<Project name="' + projectName + '" projectID="' + projectID + '">' +
'<Tasks>' +
'<Task finish="' + taskFinish + '" internalTaskID="' + taskID + '" name="' + taskName + '" outlineLevel="' + taskOutline + '" percComp="' + "0.99" + '" start="' + taskStart + '" status="' + "1" + '" taskID="' + taskCode + '"/>' +
'</Tasks>' +
'</Project>' +
'</Projects>' +
'</NikuDataBus>' +
'</soapenv:Body>' +
'<soapenv:Envelope>'
} else if (taskStatus == 2)
{
var xw = "?<xml version=\"1.0\" encoding=\"utf-8\"?>" +
'<soapenv:Envelope ' +
' xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"' +
' xmlns:core="jelly:core"' +
' xmlns:file="jelly:com.niku.union.gel.FileTagLibrary" ' +
' xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary" ' +
' xmlns:soap="jelly:com.niku.union.gel.SOAPTagLibrary" ' +
' xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ' +
' xmlns:sql="jelly:sql" ' +
' xmlns:util="jelly:util" ' +
' xmlns:xog="http://www.niku.com/xog" ' +
' xmlns:xsd="http://www.w3.org/2001/XMLSchem\" ' +
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +
' <soapenv:Header> ' +
'<xog:Auth>' +
' <xog:SessionID>' + UserSessionID + '</xog:SessionID>' +
'</xog:Auth>' +
'</soapenv:Header>' +
'<soapenv:Body>' +
'<NikuDataBus xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../xsd/nikuxog_project.xsd">' +
'<Header action="write" externalSource="NIKU" objectType="project" version="15.2.0.213"/>' +
'<Projects>' +
'<Project name="' + projectName + '" projectID="' + projectID + '">' +
'<Tasks>' +
'<Task finish="' + taskFinish + '" internalTaskID="' + taskID + '" name="' + taskName + '" outlineLevel="' + taskOutline + '" percComp="' + "1" + '" start="' + taskStart + '" status="' + "2" + '" taskID="' + taskCode + '"/>' +
'</Tasks>' +
'</Project>' +
'</Projects>' +
'</NikuDataBus>' +
'</soapenv:Body>' +
'<soapenv:Envelope>'
}
//This is where things go horribly wrong...


//launch a new XMLHTTP request
var xmlhttp;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", xog_url, true);
var xmlDoc = xmlhttp.responseXML;

//Initialize, aim and fire
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//Did we strike our target?
var xmlDoc = xmlhttp.responseXML;

console.log("XML String: " + xmlhttp.responseText);
try {
var xogOutput = xmlDoc.getElementsByTagName("XOGOutput")[0];
xogOutput.parentNode.removeChild(xogOutput);
} catch (err) {
//Did we miss altogether?
console.log(err.message);
}
//What is the Response to our http request?
console.log(xmlhttp.responseText);
}
}

//Right, send it all away...
xmlhttp.setRequestHeader("Content-Type", "text/xml");
xmlhttp.send(xw);
}

}

I can build execute the query ("fon_proj_phase_task") and get back all of the information I need.  I can build my XOG write request, which runs happily and successfully in the IT-ROI XOG and Query Bridge. So, I'm missing something simple and vital in the XMLHTTPRequest.

The error in the browser Console is "Failed to load resource: the server responded with a status of 400()"

Any ideas will be very gratefully received...

 

Edit: version 15.2, ondemand

Outcomes