- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2023 03:39 AM
Hi All,
I have a requirement to generate an XML file with specific format and place it in MID Server. Requirement is as follows:
1. Query sys_user table to get those records where Active=false and Updated_on=Today.
2. Based on the result set, get the employee number of each record and create the XML string in below format:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<Import>
<Pers id="12345" operation="update">
<Status>Disabled</Status>
<Card id="1" operation="delete"/>
<Card id="2" operation="delete"/>
<Card id="3" operation="delete"/>
<Card id="4" operation="delete"/>
<Card id="5" operation="delete"/>
</Pers>
<Pers id="23456" operation="update">
<Status>Disabled</Status>
<Card id="1" operation="delete"/>
<Card id="2" operation="delete"/>
<Card id="3" operation="delete"/>
<Card id="4" operation="delete"/>
<Card id="5" operation="delete"/>
</Pers>
<Pers id="34567" operation="update">
<Status>Disabled</Status>
<Card id="1" operation="delete"/>
<Card id="2" operation="delete"/>
<Card id="3" operation="delete"/>
<Card id="4" operation="delete"/>
<Card id="5" operation="delete"/>
</Pers>
3. Upon generation of XML string, write it to file(.xml extension) and then push it to a specified location(file path) in MID Server.
Note : I can make use of Export Set however, i cannot export the data in the format that is mentioned above.
Any pointers on the best possible way to achieve this requirement would be really helpful.
Thanks in advance!!
~Kumar.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2023 03:17 PM - edited 05-18-2023 11:45 PM
Note: The following example will place the file in next to the start.bat / start.sh of your mid server installation.
You can put use any filepath, but you need to make sure, that your mid server user has access to the file.
Also note that you might want to change the mid server (checkout the line where eccGr.agent = "mid.server.<name of the mid server record>"
var doc = new XMLDocument2();
var importNode = doc.createElement('Import');
createPers(doc, importNode, '12345', 'update', 'Disabled', [{ id: '1', operation: 'delete' }, { id: '2', operation: 'delete' }, { id: '2', operation: 'delete' }]);
createPers(doc, importNode, '23456', 'update', 'Disabled', [{ id: '1', operation: 'delete' }, { id: '2', operation: 'delete' }, { id: '2', operation: 'delete' }]);
createPers(doc, importNode, '34567', 'update', 'Disabled', [{ id: '1', operation: 'delete' }, { id: '2', operation: 'delete' }, { id: '2', operation: 'delete' }]);
var midServerName = 'test';
sendToMid(midServerName, 'helloWorld.xml', doc.toString());
function sendToMid(midServerName, fileName, fileContent) {
var saveScript = function () {
var fileWriter = new Packages.java.io.FileWriter(probe.getParameter("file_name"));
try {
fileWriter.write(probe.getParameter("file_content"));
} catch (e) {
return e.message;
} finally {
fileWriter.close();
}
};
var params = { 'skip_sensor': true, 'file_name': fileName, 'file_content': fileContent, 'script': '(' + saveScript.toString() + ')();' };
var payload = new XMLDocument2();
payload.createElement("parameters");
for (var param in params) {
var el = payload.createElement("parameter");
el.setAttribute("name", param);
el.setAttribute("value", params[param]);
}
var eccGr = new GlideRecord("ecc_queue");
eccGr.newRecord();
eccGr.topic = "JavascriptProbe";
eccGr.queue = "output";
eccGr.state = "ready";
eccGr.name = "SaveXMLOnMID";
eccGr.source = "";
eccGr.agent = "mid.server." + midServerName;
eccGr.payload = payload.toString();
return eccGr.insert();
}
function createPers(doc, root, id, operation, status, cards) {
doc.setCurrentElement(root);
var pers = doc.createElement('Pers');
pers.setAttribute('id', id);
pers.setAttribute('operation', operation);
doc.setCurrentElement(pers);
doc.createElementWithTextValue('Status', status);
cards.forEach(function (card) {
createCard(doc, pers, card.id, card.operation);
});
}
function createCard(doc, pers, id, op) {
doc.setCurrentElement(pers);
var node = doc.createElement('Card');
node.setAttribute('id', id);
node.setAttribute('operation', op);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2023 01:44 AM
The thing is that the service account which executes the service needs to have access to the folder. You can try to use an absolute or a relative path, but I cannot guarantee that it will work due to access rights.
I've tried the script with e.g.
sendToMid(midServerName, 'exported/helloWorld.xml', doc.toString());
on my linux mid server and it worked with +rw access on the exported folder.