Anyone been able to successfully script the messag...
# suitescript
c
Anyone been able to successfully script the message record? I’m trying to auto populate the cc but can’t get it working. It’s a sublist record but it doesn’t like it no matter what I do
b
checkout the suitescript supported records page on Message for an example about the
otherrecipientslist
c
Ya that’s what I was working with and was unsuccessful
b
What does your code look like
c
function beforeLoad(scriptContext) { var newRecord = scriptContext.newRecord; //get user email var currentUser = runtime.getCurrentUser(); var userEmail = currentUser.email; //set value on cc var messageRec = record.create({ type: record.Type.MESSAGE, }); messageRec.insertLine({ sublistId: 'otherrecipientslist', line: 0 }); messageRec.setSublistValue({ sublistId: 'otherrecipientslist', fieldId: 'email', value: 'john@smith.com', line: 0 }); messageRec.setSublistValue({ sublistId: 'otherrecipientslist', fieldId: 'cc', value: 'true', line: 0 }); } return { beforeLoad: beforeLoad }; });
I tried with the basic stuff to see if I could get it working. I then tried with beforesubmit in case it didn’t like what I was doing on beforeload and got a similar error
b
is there anymore to your script?
that code wont do anything by itself
c
What am I missing?
Sorry I’m still learning it all
I’m just trying to auto populate the cc field with an email when the user loads it
b
The N/record module is usually used to create or edit records.
The most basic use is to load or create a record
set fields on it
then save it
like in the examples
if you saved your messageRec, you would probably get further along, but will probably get some error messages about missing mandatory fields
that said, you wouldn't do that flow in a user event script
c
I tried doing a messageRec.saved(); and was getting the same error I tried to do it in a client script but user event was the only one that would allow me to deploy on the message
So I figured beforeload was best bet
b
you want to be specific about what you mean by error
for code, error could mean that the code throws an error with an error code and message
or it could mean that the code isnt doing what you want, which is what i suspect your before load user event is doing
keep in mind that it doesnt sound like you should be creating a message record in the first place
user event scripts are usually based on modifying the current record being loaded/saved
they have a
newRecord
key in their
scriptContext
that is used to modify the current record
you wouldnt save that
newRecord
c
So I tried to do just a set field value but since cc is a sublist from reading the help it made it sound like I had to create a message.
Copy code
{
  "type": "error.SuiteScriptError",
  "name": "SSS_INVALID_SUBLIST_OPERATION",
  "message": "You have attempted an invalid sublist or line item operation. You are either trying to access a field on a non-existent line or you are trying to add or remove lines from a static sublist.",
  "stack": [
    "anonymous(N/serverRecordService)",
    "beforeLoad(/SuiteScripts/CUE_autoPopulateCC.js:27)"
  ],
  "cause": {
    "type": "internal error",
    "code": "SSS_INVALID_SUBLIST_OPERATION",
    "details": "You have attempted an invalid sublist or line item operation. You are either trying to access a field on a non-existent line or you are trying to add or remove lines from a static sublist.",
    "userEvent": "beforeload",
    "stackTrace": [
      "anonymous(N/serverRecordService)",
      "beforeLoad(/SuiteScripts/CUE_autoPopulateCC.js:27)"
    ],
    "notifyOff": false
  },
  "id": "",
  "notifyOff": false,
  "userFacing": false
}
b
im assuming thats from the
Copy code
messageRec.insertLine({
  sublistId: "otherrecipientslist",
  line: 0,
});
thats used for inserting a line between existing lines
that will not work very well if there are no existing lines
c
That makes sense, removing it gives me a different error {"type":"error.SuiteScriptError","name":"UNEXPECTED_ERROR","message":null,"stack":["anonymous(N/serverRecordService)","beforeLoad(/SuiteScripts/CUE_autoPopulateCC.js:27)"],"cause":{"type":"internal error","code":"UNEXPECTED_ERROR","details":null,"userEvent":"beforeload","stackTrace":["anonymous(N/serverRecordService)","beforeLoad(/SuiteScripts/CUE_autoPopulateCC.js:27)"],"notifyOff":false},"id":"34e4e170-ca31-4609-964d-f1adbf8a9995-2d323032302e30362e3237","notifyOff":false,"userFacing":false}
b
what does your code look like now
all of the code will help
stack traces contain line numbers for the problem line
c
* @NApiVersion 2.x * @NScriptType UserEventScript * @NModuleScope SameAccount */ define(['N/email', 'N/record', 'N/runtime'], /** * @param {email} email * @param {record} record * @param {runtime} runtime */ function(email, record, runtime) { /** * Function definition to be triggered before record is loaded. * * @param {Object} scriptContext * @param {Record} scriptContext.newRecord - New record * @param {Record} scriptContext.oldRecord - Old record * @param {string} scriptContext.type - Trigger type * @Since 2015.2 */ function beforeLoad(scriptContext) { //set value on cc var messageRec = record.create({ type: record.Type.MESSAGE, }); messageRec.setSublistValue({ sublistId: 'otherrecipientslist', fieldId: 'email', value: 'test@fakeemail.com', line: 0 }); messageRec.setSublistValue({ sublistId: 'otherrecipientslist', fieldId: 'cc', value: 'true', line: 0 }); messageRec.save(); } return { beforeLoad : beforeLoad }; });
Line 27 is what the error is saying the problem is?
b
thats the line that causes the error
in this case, its more likely that you can't set that field because you havent set the other mandatory fields first
c
Saving the record wouldn’t allow me to further edit, right? I want to be able to set those mandatory fields manually and just have the cc automated
b
your code is trying to create a new message
completly unrelated to the current message that is being loaded
c
Makes sense
I’ll see if I can not create the message and just set the sublist value
The user will be clicking email to initiate the messag
You are a hero
It works
var messageRec = scriptContext.newRecord messageRec.setSublistValue({ sublistId: 'otherrecipientslist', fieldId: 'email', value: 'test@fakeemail.com', line: 0 }); messageRec.setSublistValue({ sublistId: 'otherrecipientslist', fieldId: 'cc', value: 'true', line: 0 }); }