简体   繁体   中英

How to manipulate a xml using suitescript?

I have a problem with SuiteScript 2.0:

In my quote record, I have a button to create a SOLICITACAO DE OF(Fabrication request), and this document has its own number, different from the quote number, I was thinking about use a xml to hold the number, and every time I need to generate a new document, I generate a new number, so I have this code running in an User Event Script:

function getOfNumber(){
  var xmlFileContents = file.load({id:1474}).getContents();
  var xmlDocument = xml.Parser.fromString({text:xmlFileContents});
  var solicitacaoOfNode = xml.XPath.select({
    node : xmlDocument,
    xpath : '//SOLICITACAO_OF'
  });
  var currentOfNumber = solicitacaoOfNode[0].firstChild.nextSibling.textContent;
  log.debug({title:"Xml Content",details:currentOfNumber});
}

When I run this code, it returns me the current number, wich I will use as document number for the document that I'm creating in another function. Until here ok, but now, I need to add 1 to this number and save it in the xml for the next time this function runs in another record.

But I have no idea how to edit this xml, can someone help me? I was looking at Help Center on the modules, I found a lot of N/xml functions that I don't know how to use..

This is my xml file:

<?xml version="1.0"?>
<SOLICITACAO_OF>
    <Current_number>8866</Current_number>
</SOLICITACAO_OF>

I am not sure if there is a way to update an existing file on file cabinet, the approach that I would follow is:

  1. Define a specific folder to store this xml file. Create a parameter for this is a good practice.
  2. Load the file (single one) from this folder.
  3. Follow your current logic getOfNumber()
  4. Your xml content is straightforward, you could easily generate it again as string with the new Current_number.

  currentOfNumber = parseInt(currentOfNumber)++; var xmlString = '<?xml version="1.0"?>'; xmlString += '<SOLICITACAO_OF>'; xmlString += '<Current_number>'+ currentOfNumber+'</Current_number>'; xmlString += '</SOLICITACAO_OF>'; 

  1. Then create a function to delete your current xml file from file cabinet
  2. Finally create the new xml file with the xmlString as a content.

Since the new xml file id will be dynamically (delete/create) the folder id parameter will be handy to know which file to load.

Hopefully it makes sense to you. good luck!

It sounds like you are just trying to have a consistent source for a sequence number. The xml approach seems like overkill.

You could easily just load a text file, extract the number and rewrite the file. (see N/file in the help for this)

However you could also define a custom record that has an integer field. You would read the integer value and record it and write back an updated value. Pretty simple to do and has better performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM