0

My user registration form has a custom field for Gender. How can I make SCA write the customer's gender to the NS database?

Should I use Commerce API or SuiteScript API?

I guess I should edit the model Account/SuiteScript/Account.Model.js but should I use the Commerce API (Customer object or the Profile/User object) or directly write to the database using record.setFieldValue()?

In Account/SuiteScript/Account.Model::register() function:

//@method register
//@param {UserData} user_data
//@param {Account.Model.Attributes} user_data
register: function (user_data)
{
    var customer = session.getCustomer();

    ....default user registration code

    var user = Profile.get();



    // Should I do it like this?
    customer.updateProfile({
        "customfields": {
            "custentity_dev_gender": user_data.gender
        }
    })

    // Should I edit the user object??

    // Should I interact directly with the customer record. Does Commerce API allow this??
    var record = nlapiLoadRecord('customer', user.id);
    record.setFieldValue('custentity_dev_gender', user_data.gender);
    var internalId = nlapiSubmitRecord(record);



    return {
        touchpoints: session.getSiteSettings(['touchpoints']).touchpoints
    ,   user: user
    ,   cart: LiveOrder.get()
    ,   address: Address.list()
    ,   creditcard: CreditCard.list()
    };
}
sazr
  • 24,984
  • 66
  • 194
  • 362

1 Answers1

1

Its Very Simple actually no need to write record.setFieldValue()

  1. Very first step is to create custom entity field from Customization--> Entity Field-->New
  2. Apply the entity field to customer give edit access to field and Save.
  3. Copy the entity id from there, in this case say 'custentity_gender_field'
  4. The you need to override below two files, if you don't know ask separate question I'll reply there.

i.Modules/suitecommerce/LoginRegister@2.2.0/Templates/login_register_register.tpl ii.Modules/suitecommerce/Profile@2.3.0/SuiteScript/Profile.Model.js

  1. Go to first file write below code somewhere where you need to put gender

<div class="login-register-register-form-controls-group" data validation="control-group">
 <label class="login-register-register-form-label for="custentity_gender_field">{{translate 'Gender: '}}
  <small class="login-register-register-form-optional">{{translate '(optional)'}}</small>
 </label>
 <div class="login-register-register-form-controls" data-validation="control">
  <input type="text" name="custentity_gender_field" id="custentity_gender_field" class="login-register-register-form-input">
 </div>
</div>
  1. Then Go to Second File i.e. Profile.Model.js

You can see in get function various filed values, put your custom field value there

this.fields = this.fields || ['isperson', 'email', 'internalid', 'name', 'overduebalance', 'phoneinfo', 'companyname', 'firstname', 'lastname', 'middlename', 'custentity_gender_field', 'emailsubscribe', 'campaignsubscriptions', 'paymentterms', 'creditlimit', 'balance', 'creditholdoverride'];
  1. Deploy your code and test under user roles--> Manage User

I missed one more thing here, you need to make changes in

Account@2.2.0/SuiteScript/Account.Model.js as well in this model find

register: function (user_data)
{

}

and your custom field in

ModelsInit.session.registerCustomer(
{ 
    custentity_gender_field:user_data.custentity_gender_field
});

That's it...

Anup Chaudhary
  • 320
  • 1
  • 10