4N335
4N335

Reputation: 281

How to generate a custom record from employee record, through UE in suitescript 2.0?

I need to generate a custom record in backend once a new employee record is created, I just wanted to know if record.transform(options) is the correct choice for it. I tried doing this, but nothing is happening so far and I dont know what to put in toType prop of record.transform obj because my record is not standard. Kindly suggest a solution from your experience. thanks

my code:

define(['N/record', 'N/ui/serverWidget', '../library/global_constants.js'], function (record, ui, globalConstants) {


  function afterSubmit(context) {
    if (type == ctx.UserEventType.CREATE || type == ctx.UserEventType.EDIT) {
      createPayrollRec(context);
    }
  }

  function createPayrollRec(ctx) {
    try {
      var rec = ctx.newRecord;
      var form = ctx.form;

      var hireDate = rec.getValue({ fieldId: 'hiredate' });
      var childRecLen = rec.getLineCount({ sublistId: 'here_will_come_payroll_child' });

      if (!!hireDate && childRecLen > 0) {// if hireDate field is populated and chidlRec has lines

        var tranfRec = rec.transform({
          fromType: rec.Type.EMPLOYEE,
          fromId: req.parameters.id,
          toType: 'customrecord_mxp_payroll',// this is my custom rec id
          isDynamic: true,
        });
        log.debug('tranfRec', tranfRec);

        tranfRec.save({
          ignoreMandatoryFields: true
        });
        log.debug('Payroll Record Created!');

      }
    } catch (error) {
      log.error('Error: on afterSubmit', error)
    }
  }

  return {
    afterSubmit: afterSubmit
  }

});

Upvotes: 0

Views: 1205

Answers (1)

Charl
Charl

Reputation: 812

record.transform is used for specific records that are based on other records, such as generating a Sales Order, based on an Estimate record.

You will need to create your custom record:

var custRec= record.create({
       type: 'customrecord_mxp_payroll',
});

Then you will need to populate the fields on your record with the needed values. I assume you will use some of the values on Employee record? Do something like this then:

var nsrecord = record.load({
        type: record.Type.EMPLOYEE,
        id: req.parameters.id
    });
var empValue = nsrecord.getValue('fieldname');
custRec.setValue('fieldname', empValue);
custRec.save();

I hope this helps.

Upvotes: 1

Related Questions