https://netsuiteprofessionals.com logo
#suitescript
Title
# suitescript
a

Artur_K

04/27/2022, 6:49 AM
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

battk

04/27/2022, 6:50 AM
a

Artur_K

04/27/2022, 6:52 AM
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

battk

04/27/2022, 6:54 AM
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

Artur_K

04/27/2022, 6:56 AM
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

battk

04/27/2022, 6:57 AM
will kinda depend on how the purchase order is approved
a

Artur_K

04/27/2022, 6:57 AM
via workflow actions in the View mode
b

battk

04/27/2022, 6:57 AM
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

Artur_K

04/27/2022, 6:59 AM
Is it possible to script something, if the approval status is triggered by a workflow button in the view mode?
b

battk

04/27/2022, 7:00 AM
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

Artur_K

04/27/2022, 7:02 AM
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

battk

04/27/2022, 8:03 AM
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

Artur_K

04/27/2022, 8:19 AM
Understood, worked out with JSON.stringify Thanks again!