Saturday, October 6, 2018

Automatically create an XML file from saved search using script

 

The script pulls some field values from an existing saved search 'customsearch35'.
To create the XML document, set the XML header, data header and then get the value for each row from the saved search.

Sample Code Snippet:

 var results = nlapiSearchRecord('customer','customsearch35',null,null);

 // Set XML header
 xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
 xml += '<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">';

 // Header
 xml += '<row>';
 xml += ' <Col0> Date Created </Col0>';
 xml += ' <Col1> Name </Col1>';
 xml += ' <Col2> Phone </Col2>';
 xml += ' <Col3> Fax </Col3>';
 xml += ' <Col4> Email address </Col4>';
 xml += '</row>';

 // Contents
 for(x=0;x<results.length;x++)
 {
   xml += '<row>';
   xml += ' <Col0>' + results[x].getValue('createddate') + '</Col0>';
   xml += ' <Col1>' + results[x].getValue('companyname') + '</Col1>';
   xml += ' <Col2>' + results[x].getValue('phone') + '</Col2>';
   xml += ' <Col3>' + results[x].getValue('fax') + '</Col3>';
   xml += ' <Col4>' + results[x].getValue('email') + '</Col4>';
   xml += '</row>';

 }

 // Close document
 xml += '</document>';

// Create File
var file = nlapiCreateFile('searchresults.xml', 'XMLDOC', xml);

// You can either write result on the same page
response.setContentType(file.getType());
response.write(file.getValue()); 

// Or send the result via email
var newEmail = nlapiSendEmail(1234, 'jdoe@testemail.com', 'Sample email and attachment','Please see the attached file', null, null, null, file);


 

1 comment:

  1. Hello!

    Can you add this to a button in an item fullfilment?

    ReplyDelete