1
votes

This MarkLogic XQuery post SOAP message to the CRM and gets a response back as XML, can you please help me to achieve following in the same query:

  1. I want to run this query every five minutes using the MarkLogic Task Server. Before it runs next time I have to parse the last response and do the following:

a) if in the response XML we see results/response/code[.=200], then pick the results/response/header/date and assign it to XQuery's $lastUpdateTime variable. Otherwise $lastUpdateTime should not get updated (to make sure transaction is successful).

b). I have one XML in a MarkLogic DataBase which contains id, first_name, last_name, phone, email tags. Every time SOAP responds brings me updated details from CRM i.e., first_name, last_name, phone, email (where ID is unique identifier). Now I have to loop through each and every response/item and when id matches with any of the records present in ML DB's XML, I have to update respective first_name, last_name, phone, email.

XQuery:

let $lastUpdateTime := xdmp:get-request-field("lastUpdateTime", "2012-07-23 09:26:03")

let $payload := xdmp:quote(
    <SOAP-ENV:Envelope>
      <SOAP-ENV:Body>
        ....
      </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
  )
return
  let $createLogin := xdmp:http-post("http://localhost/SugarCE-Full-6.5.0/service/v2/soap.php/login")  
  let $payload2 := xdmp:quote(
      <SOAP-ENV:Envelope>
        <SOAP-ENV:Body>
          <query>date_modified >= '{$lastUpdateTime} AND deleted = 0'</query>
        </SOAP-ENV:Body>
      </SOAP-ENV:Envelope>)
  return
    xdmp:http-post("http://localhost/SugarCE-Full-6.5.0/service/v2/soap.php/login")

response:

<?xml version="1.0" encoding="UTF-8"?>
<results warning="more than one root item">
  <response xmlns="xdmp:http">
    <code>200</code>
    <message>OK</message>
    <headers>
      <date>Thu, 26 Jul 2012 19:33:58 GMT</date>      
    </headers>
  </response>
  <SOAP-ENV:Envelope>
    <SOAP-ENV:Body>
      <entry_list>
        <item xsi:type="tns:entry_value">
        <item xsi:type="tns:name_value">
          <name xsi:type="xsd:string">id</name>
          <value xsi:type="xsd:string">90cd7230-9fdd-20ae-9ad2-500dabecdb70</value>
        </item>
        <item xsi:type="tns:name_value">
          <name xsi:type="xsd:string">first_name</name>
          <value xsi:type="xsd:string">Jhon</value>
        </item>
        <item xsi:type="tns:name_value">
          <name xsi:type="xsd:string">last_name</name>
          <value xsi:type="xsd:string">Carter</value>
        </item>
        <item xsi:type="tns:name_value">
          <name xsi:type="xsd:string">email</name>
          <value xsi:type="xsd:string">[email protected]</value>
        </item>
        <item xsi:type="tns:name_value">
          <name xsi:type="xsd:string">phone</name>
          <value xsi:type="xsd:string">99999999</value>
        </item>     
          </name_value_list>
        </item>
        <item xsi:type="tns:entry_value">
        <item xsi:type="tns:name_value">
          <name xsi:type="xsd:string">id</name>
          <value xsi:type="xsd:string">90cd7230-9fdd-20ae-9ad2-500dabecdb71</value>
        </item>
        <item xsi:type="tns:name_value">
          <name xsi:type="xsd:string">first_name</name>
          <value xsi:type="xsd:string">Jack</value>
        </item>
        <item xsi:type="tns:name_value">
          <name xsi:type="xsd:string">last_name</name>
          <value xsi:type="xsd:string">Sully</value>
        </item>
        <item xsi:type="tns:name_value">
          <name xsi:type="xsd:string">email</name>
          <value xsi:type="xsd:string">[email protected]</value>
        </item>
        <item xsi:type="tns:name_value">
          <name xsi:type="xsd:string">phone</name>
          <value xsi:type="xsd:string">8888888</value>
        </item>     
          </name_value_list>
        </item>    
        </item>
      </entry_list> 
      </SOAP-ENV:Body>
  </SOAP-ENV:Envelope>
</results>

Existing ML DB XML

<?xml version="1.0" encoding="UTF-8"?>
<Users>
    <user>
        <id>90cd7230-9fdd-20ae-9ad2-500dabecdb70</id>
        <firstName>Jhon</firstName>
        <lastName>Carter</lastName>
        <phone>999999999</phone>
        <email>[email protected]</email>
    </user>
    <user>
        <id>90cd7230-9fdd-20ae-9ad2-500dabecdb72</id>
        <firstName>Pankja</firstName>
        <lastName>Pankaj</lastName>
        <phone>88888</phone>
        <email>[email protected]</email>
    </user>
    <user>
        <id>90cd7230-9fdd-20ae-9ad2-500dabecdb78</id>
        <firstName>xyz</firstName>
        <lastName>abc</lastName>
        <phone>1111111</phone>
        <email>xyz@abc</email>
    </user>
</Users>

Any suggestion would be a great help. Thanks.

1

1 Answers

1
votes

First of all, get rid of the lastUpdateTime parameter. Store that value inside the users xml as an attribute of the root element for instance, and read it from there as soon as the script gets called. Extend your existing code to update that timestamp each time you update the users XML.

Next, store the script in the Database of your choice. Make sure it is a main xquery module, not a library module.

Finally, go to the Admin interface, open the Group of your interest, open Scheduled Tasks, and add a new task there. Modules database should point to where you stored your script. The task root refers to a path prefix within that modules database. You can leave it to / to keep things simple. Task database should point to where you stored your users XML.

HTH!