ahawks122's Profile
Rookie
3
Points

Questions
0

Answers
1

  • Rookie Asked on October 29, 2021 in SuiteCloud Development Framework.

    I was looking for the same and here is an example of how I tested my Sales Order User Event script. Hopefully this helps

     

    File src/FileCabinet/SuiteScripts/SalesOrder-UserEvent.js

    
    /**
    
    * @NApiVersion 2.1
    
    * @NScriptType UserEventScript
    
    */
    
    define([],
    
    () => {
    /**
    
    * Defines the function definition that is executed before record is submitted.
    
    * @param {Object} scriptContext
    
    * @param {Record} scriptContext.newRecord - New record
    
    * @param {Record} scriptContext.oldRecord - Old record
    
    * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
    
    * @since 2015.2
    
    */
    
    const beforeSubmit = (scriptContext) => {
    
    if(scriptContext.type === scriptContext.UserEventType.CREATE){
    
    scriptContext.newRecord.setValue({fieldId: 'custbody_customfield', value: Math.random()});
    
    }
    
    }
    return {beforeSubmit}
    });
    

    File __tests__/sales-order-test.js

    import record from 'N/record'; import Record from 'N/record/instance'; import SalesOrderUserEvent from 'SuiteScripts/SalesOrder-UserEvent'; jest.mock('N/record'); jest.mock('N/record/instance'); beforeEach(() => { jest.clearAllMocks(); }); describe('Sales Order Test', () => { it('should add a random number to the sales order on create', () => { // given const salesOrderId = 1352; record.load.mockReturnValue(Record); Record.save.mockReturnValue(salesOrderId); const scriptContext = { type: 'create', newRecord: Record, UserEventType: { CREATE: 'create', EDIT: 'edit', } }; // when SalesOrderUserEvent.beforeSubmit(scriptContext); // then expect(Record.setValue).toHaveBeenCalledWith({fieldId: 'custbody_customfield', value: expect.anything()}); }); it('should not add a random number to the sales order on edit', () => { // given const salesOrderId = 1352; record.load.mockReturnValue(Record); Record.save.mockReturnValue(salesOrderId); const scriptContext = { type: 'edit', newRecord: Record, UserEventType: { CREATE: 'create', EDIT: 'edit', } }; // when SalesOrderUserEvent.beforeSubmit(scriptContext); // then expect(Record.setValue).not.toHaveBeenCalled(); }); });

    • 1692 views
    • 1 answers
    • 0 votes