Bud
Bud

Reputation: 781

SuiteScript Create File In Client Side Script

I'm trying to create a file in the File Cabinet and write to it in a Client Script. Checking the API reference, I see that all the File objects are Server-side only.

Does that mean you can't create and write to a file in a Client script? I tried to use the code in my Client script anyway, but got the error:

Fail to evaluate script: {"type":"error.SuiteScriptModuleLoaderError","name":"{stack=[Ljava.lang.Object;@59c89ae9, toJSON=org.mozilla.javascript.InterpretedFunction@5a4dd71f, name=MODULE_DOES_NOT_EXIST, toString=org.mozilla.javascript.InterpretedFunction@1818dc3c, id=, message=Module does not exist: N/file.js, TYPE=error.SuiteScriptModuleLoaderError}","message":"","stack":[]}

When I tried to save it in NetSuite as the script file. Does the above mean that the N/File object can't be loaded in a Client script?

Can I write to a file in a Client script?

Upvotes: 1

Views: 1728

Answers (2)

Martha
Martha

Reputation: 764

Create a Client Script - this Script will contain the function to call the Suitelet and pass along information from the current record/session if needed.

  function pageInit{
    //required but can be empty
  }
  function CallforSuitelet(){
    var record = currentRecord.get();
    var recId = record.id;
    var recType = record.type
    var suiteletURL = url.resolveScript({
      scriptId:'customscriptcase3783737_suitelet',// script ID of your Suitelet
      deploymentId: 'customdeploycase3783737_suitelet_dep',//deployment ID of your Suitelet
      params: {
       'recId':recId,
       'recType':recType
      }
     });
      document.location=suiteletURL;
  }

  return {
    CallforSuitelet : CallforSuitelet,
    pageInit : pageInit
  }

Create a Suitelet - this script will create the file

function onRequest(context) {
  var requestparam = context.request.parameters;
  var recId = requestparam.recId; //the same name of the fields specified in url.resolveScript parameters from Client Script
  var recType = requestparam.recType;

  var objRecord = record.load({
    type: record.Type.___,//insert record type
    id: recId
  });
  var content = 'Insert Content Here';
  var xml = "<?xml version=\"1.0\"?>\n<!DOCTYPE pdf PUBLIC \"-//big.faceless.org//report\" \"report-1.1.dtd\">\n";
    xml += "<pdf>\n<body font-size=\"12\">\n<h3>Sample PDF</h3>\n";
    xml += "<p></p>";
    xml += content;
    xml += "</body>\n</pdf>";

    context.response.renderPdf({xmlString: xml});
  }
  return {
    onRequest: onRequest
  }

Upvotes: 4

Simon Delicata
Simon Delicata

Reputation: 411

As you've already discovered, server-only modules can't be called from client-side scripts directly, but this can be done via a Suitelet. You will need to decide how the Suitelet does it's work. An example of the principal at work can be found here and here

Upvotes: 0

Related Questions