does anyone know how to pass a value from a Suitel...
# suitescript
u
does anyone know how to pass a value from a Suitelet back into a Client Script using SuiteScript2 without losing the values on a form? For context, I'm loading a Suitelet from Client Script's
afterSubmit()
, to load a record and get values, then finally send those values back to the Client Script before returning true and committing the save. I've successfully passed values from the CS to the SL, but I'm at a loss at returning the value back to the CS without the page just loading into a new page and losing all the input in the form. The intended result would be that after clicking Save on the form, the Suitelet is fired, a record is loaded and its values are retrieved and sent back to the Client Script, and it applies the values on the form before returning true for
afterSubmit()
to finish. Any and all input appreciated.
n
Sounds like you want to open the SuiteLet in a window using an opener and capture values stored when the window is closed. Kinda hard to visualise what you're doing to be honest.
u
Ah, allow me to clarify then. I'm trying to load a SALES_ORDER record from a FULFILLMENT record using a client script, but since Client Scripts obey role permissions, the users with Fulfillment permissions can't create nor edit Sales Orders, even through scripts. To work around this, I'm attempting to load a Suitelet through a Client Script, with the intent of the Suitelet to process some logic that involves loading, editing and retrieving values from the Sales Order, then returning some values back to the Client Script. The catch is that I need these to process when the user is fulfilling the Sales Order (i.e. they'll be in Create mode), so when they hit Save and trigger
afterSubmit()
, ideally the Suitelet will load and run in the background without unloading the form and losing all the user input. I've already managed to pass values from the Client Script to the Suitelet, but I'm having difficulty returning the values the latter generates to the former. I saw a thread mentioning this was possible in Suitescript 1, so I'm trying to figure out if it's feasible in Suitescript 2.x or not.
b
what does the code look like
text descriptions are far less useful than the actual code
m
After Submit sounds like a user event, not a client script?
u
Client Script:
Copy code
function onSaveRecord() {

    let _IF_record = currentRecord.get();
    let _IF_uuid = _dr_record.getValue({ 
fieldId:"custbody_if_uuid" 
});
    let _salesorder_reference = _IF_record.getValue({ fieldId:"createdfrom" });

    function AppendSalesOrderField(idk) {

        //spits out a string: the url of a suitescript
      var suiteletUrl = url.resolveScript({
          scriptId: 'customscript460',
          deploymentId: 'customdeploy1',
          returnExternalUrl: false,
          params: {
            tranid: _salesorder_reference,
            uuid: _IF_uuid
          }
      });


      https.get.promise({
        url: suiteletUrl
      }).then(function(response){
        window.onbeforeunload = null;
        location.href = suiteletUrl;
        SucceededAppending(response)
      }).catch(function(reason){
        log.error("F", reason);
        FailedAppending(reason)
      });
    }

    function SucceededAppending(wat) {
      alert("worked: " + wat);
      return false;
    }

    function FailedAppending(huh) {
      alert("failed: " + huh);
    }

    try {
     AppendSalesOrderField();
    } catch (e) {
      FailedAppending(e);
    }
  }

  exports.saveRecord = onSaveRecord;

  return exports;
});
Suitelet
Copy code
function onRequest(context) {

    if (context.request.method === 'GET') {
      let _id = parseInt(context.request.parameters.tranid, 10);
      let _uu = context.request.parameters.uuid;
      context.response.write("uuid: " + _uu + "\ntranid " + _id);
    } else {
      context.response.write("<script>window.close();</script>");
    }
  }

  exports.onRequest = onRequest;
  return exports;
I'm currently stumped at the suitelet here, as I'm unaware of any method of loading the suitelet without closing out the current page to render the result.
m
Remove the location.href line that is redirecting the browser to the suitelet page. You already called the suitelet and the result is in the response object
u
Thanks for that @michoel! The logic seems to be processing now, the only concern now are the values from the Suitelet don't seem to be carrying over. Is there a specific approach in the suitelet to make it send processed data back to the client script? I'm not too familiar with the
context.response
object, unless there's a limitation I'm unaware of?
m
Whatever you send from the suitelet (with
context.response.write
) should be available in the client script in the
response
object
u
I see. Presuming the SL is as I wrote on top, I should be getting a long string, but all I'm getting is "http.ClientResponse" when I run it through an alert to test. Running
Object.keys()
on the response object doesn't seem to return the values I'm writing from the suitelet neither. Am I supposed to encode the return value? The SL seems to be returning something because it's triggering the correct function on the CS.
b
use console.log to log the response object
you are logging the output of the response's toString method, which is not very useful'
alternatively you can lookup the ClientResponse Object Members
1
u
Thanks for this linked documentation @battk. I've managed to send string values from the SL to the CS using a
<http://https.post|https.post>()
instead of
https.get()
. For this very specific use-case, I feel that to be more than enough to get the job done. Thanks again!