Hey Guys, guess I need some support :) I added a ...
# suitescript
s
Hey Guys, guess I need some support :) I added a Button in a UserEventScript to our Sales Order and tried to get the Text for example the Shipping State but it don’t works at all i always got the message “Cannot read property ‘newRecord’ of undefined” Guess the problem Is, that the button is created in the beforeLoad function ? Here is my example:  UserEventScript.js to Add the Button:
Copy code
define(['N/currentRecord', 'N/record'], function(currentRecord, record){

    var exports= {};

    function beforeLoad(scriptContext) {

      scriptContext.form.addButton({
          id:"custpage.button",
            label:"Press Me",
            functionName:"myMail"
          }
        )
       // path to Button function:
      scriptContext.form.clientScriptModulePath="SuiteScripts/mybuttonfunc.js";

    }


    exports.beforeLoad = beforeLoad;
    return {exports};

  });
ClientScript.js :
Copy code
define(['N/record'], function(record) {

  var exports = {};

  function pageInit() {}

  function myMail(scriptContext){
    var shippingS = scriptContext.newRecord.getText({fieldId: 'shipstatus'})
  // var shippingS = scriptContext.newRecord.getValue({fieldId: 'shipstatus'});
  }

  exports.pageInit = pageInit;
  exports.myMail = myMail;
  return exports;

});
b
Buttons dont get a scriptContext
w
scriptContext
....
dammit.
b
Tomas can say the rest
w
I think you need to load the module N/currentRecord and then run let rec = currentRecord.get() to get the current record. then run rec.getValue...
But it also depends on if you are running this in view or edit.
s
ok thy ill give it a try! the button only shows up in viewmode
w
ok, then I think you need to use search.lookupFields();
@battk, feel free to correct me 🙂
Copy code
search.lookupFields({type: 'salesorder', id:currentRecord.get().id, columns:['shipstatus']})
b
you can also get the fields in the beforeLoad, and pass them in as parameters
Copy code
scriptContext.form.addButton({
  id: "custpage.button",
  label: "Press Me",
  functionName: "myMail('im a string')",
});
👍 1
m
@battk I used to do that with some regularity but I believe it's not actually documented anywhere that passing parameters in
functionName
works. Do you know otherwise and/or do you see it as fairly low-risk undocumented code? It definitely feels like a better solution to me (better for performance, if nothing else) than constantly using
currentRecord.get()
in client functions.
b
I've really struggled to get buttons from UE scripts to do anything useful. I tried to set a check box to true and nothing would happen, very frustrating, I always just end up going back to workflow button and then workflow action script, but would be very interested in what you find
w
@b if the button is pressed in a view-context, you need to refresh the page to see the new value... or hack/update the html.
s
You can also just write your whole function inline into the button like
Copy code
`(function myMail(){
//add all your stuff here
})()`
If using 2.1, the tick string notation makes this much easier to read
👍 1
🤯 1
b
I would refresh the page, wouldn't work, would love to see a working example
m
@b Are you using
N/currentRecord
to set values in view? If so, that's probably the issue - you'll need to use
N/record
to submitFields or a combination of load/setValue/save instead.
b
Yes, here is what my function had:
function iBeenClicked (){
var soRec = currentRecord.get();
var soRecObj = r.load({
type: r.Type.SALES_ORDER,
id: soRec.id
});
soRecObj.setValue({
fieldId: 'custbody_gvo_rerelease',
value: true
});
soRecObj.save();
window.location.reload();
}
m
@b Huh, I don't immediately see any issues in there (assuming
custbody_gvo_rerelease
is a checkbox field that stores value and that
currentRecord
and
r
are the
N/currentRecord
and
N/record
modules). It's not throwing an error, right? Have you tried putting a
console.log
or
alert
in there to make sure the function is getting triggered at all?
b
I just created a workflow button and workflow action script, everytime I try it justs ends up being a big headache and time suck
s
record.submitFields() should be faster since you have no real reason to be loading the record. Also, clicking a button to set a single field value and reload the page seems like strange use case to me, why not just open the record in EDIT mode in the first place and click the checkbox?
b
@MTNathan i see it as low risk. the functionName is essentially implemented in the onclick of the button. Thats basically eval, you can do whatever you want
👍 1
there are minor differences in how the code is evaled depending on if you add a client script file in the user event, but eval pretty much allows you to do what you want
n
window.nlapiGetFieldText('shipstatus'); window.nlapiGetFieldValue('shipstatus'); save code lines