How can I run a function/method only after the sta...
# suitescript
a
How can I run a function/method only after the status of the purchase order is "Approved" ?
Copy code
/**
    *@NApiVersion 2.0
    *@NScriptType UserEventScript
    *@NModuleScope Public
*/

define(['N/record', 'N/runtime', 'N/email'], function(record, runtime, email) {

    function afterSubmit(context) {
        
        var status = context.newRecord.getValue({
            fieldId: 'approvalstatus',
        });
        var currentRecord = context.newRecord.id; // get record id

        email.send({
                author: runtime.getCurrentUser().id,
                body: 'This record has following information ' + currentRecord + status,
                recipients: 'test',
                subject: 'Test',
        })
        
    }   

    return {
        afterSubmit: afterSubmit,
    }
});
b
a
I tried, but unfortunately I cannot access the field id 'approvalstatus'. Do you see the issue here?
Copy code
var status = context.newRecord.getValue({
            fieldId: 'approvalstatus',
        });
b
depends on what you mean you cant access the field
saying something doesnt work is too meaningless
there are lots of ways something can fail
there are lots of ways that your expectations about what should happen could be wrong
a
When I log it, it shows me an empty value. When I wrapped it up in a condition, it never ran the codeblock
Copy code
if(status == 'Approved'){
  // do something
}
b
will kinda depend on how the purchase order is approved
a
via workflow actions in the View mode
b
first is if someone actually edited the purchase order and changed the status
in which case you should be able to get the approval status
though your code wouldnt actually work since the internal id of the statues are not the text of the option
a
Is it possible to script something, if the approval status is triggered by a workflow button in the view mode?
b
buttons tend to mean that it actually wasnt a record edit, this is especially true if scriptContext.type is approve
in which case you should lookup the order's status
a
Alright, will try. Thanks a lot!
Copy code
/**
    *@NApiVersion 2.0
    *@NScriptType UserEventScript
    *@NModuleScope Public
*/

define(['N/record', 'N/runtime', 'N/email', 'N/search'], function(record, runtime, email, search) {

    function afterSubmit(context) {

        var id = context.newRecord.id; // get record id

        var fieldLookUp = search.lookupFields({
            type: search.Type.PURCHASE_ORDER,
            id: id.toString(),
            columns: 'entity'
        });

        email.send({
                author: runtime.getCurrentUser().id,
                body: 'This record has following information ' + id + fieldLookUp.entity + ' MARK ' + fieldLookUp,
                recipients: '<mailto:artur.kasakow@aimsport.com|artur.kasakow@aimsport.com>',
                subject: 'Test',
        })
        
    }   

    return {
        afterSubmit: afterSubmit,
    }
});
Somehow I cannot access the object and return a valid output. The email I receive is always the object itself: This record has following information 12795[object Object] MARK [object Object]. It should output the entity that I defined in the columns, no? I tried to access the list element with the index 0 as well, but the output was undefined. What am I doing wrong here?
b
rules of concatenation means that all your variables will be converted to strings before being concated
Copy code
fieldLookUp.entity
is an object, and converting an object to a string ends up using Object.prototype.toString()
which outputs [object Object] for Objects
same thing for fieldLookUp
a
Understood, worked out with JSON.stringify Thanks again!