Custom User Registration: How to write gender to customer record - netsuite

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()
};
}

Its Very Simple actually no need to write record.setFieldValue()
Very first step is to create custom entity field from Customization--> Entity Field-->New
Apply the entity field to customer give edit access to field and Save.
Copy the entity id from there, in this case say 'custentity_gender_field'
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
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>
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'];
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...

Related

Netsuite email template - how to get child values from custentity field?

guys!
I'm writing an email template for the invoices. On the invoice form I have a "Project" (internalId: Job) field -> on the "Project" form I have a "custom entity" field with employee-type. I can get an employee's name with ${transaction.job.custentity5}. But I can't get access to related fields such as email, phone number and etc. The code ${transaction.job.custentity5.email} gives me nothing. The code ${transaction.job.custentity5.mobilephone} gives me a strange error like "field job.mobilephone not found" (netsuite hides custentity5 in this objects chain), but I see this field in employee's profile.
How do i get child values from custentity field?
Unfortunately, you can't go into such a deep level with the standard data provided.
You can however fetch the data with a search.lookupFields during a beforeLoad and set it as default value on a custom field of the form.
/**
* #NApiVersion 2.x
* #NScriptType UserEventScript
*/
define(['N/ui/serverWidget', 'N/search'], function(serverWidget, search) {
function beforeLoad(context) {
var invoice = context.newRecord;
var form = context.form;
var type = context.type;
var UserEventType = context.UserEventType;
// only execute during printing...
if (type != UserEventType.PRINT) return
var jobID = invoice.getValue({fieldId: 'job'});
// return when no job/project is set on the invoice...
if (!jobID) return
var job = search.lookupFields({
type: search.Type.JOB,
id: jobID,
columns: ["custentity5"]
})
// return when no employee is set on the project...
if (!(job.custentity5 && job.custentity5[0] && job.custentity5[0].value)) return
var employee = search.lookupFields({
type: search.Type.JOB,
id: job.custentity5[0].value,
columns: ["email", "phone"]
})
var field = form.addField({
id : 'custpage_custom_data_employee',
label: 'Employee',
type : serverWidget.FieldType.LONGTEXT
});
field.defaultValue = JSON.stringify(employee);
}
return {
beforeLoad: beforeLoad
};
})
You can access the data within the template through:
<#if record.custpage_custom_data_employee?has_content>
<#assign employee = record.custpage_custom_data_employee?eval />
</#if>
Netsuite doesn't drill down that far for you. Generally you get one step away from the primary record so you see a name using ${transaction.job.custentity5} because you'd see a name if you opened the associated job record.
How you can show the details from custentity5 depends on a bunch of factors like who is sending this email? Is is special enough to have its own button? Is the email being sent batch etc.
Options:
A before load user event script can check whether the record is being printed or emailed the standard way. That script can load and populate custom fields on the main record so you may be able to reference ${transaction.custpage_ent5.mobilephone} where you'd added the job's custom entity to the transaction
fully script your email using n/Render. You'll likely need to script everything but you can look up and add arbitrary records and datastructures to your renderer. This would be triggered by a custom button or batched script.

custom fields for address are not being saved?

I added some custom fields for address for in a twig template which extends from
#Storefront/storefront/component/address/address-form.html.twig
when i submit the form from the /checkout/register page, the custom fields are not saved.
In the Shopware\Core\Checkout\Customer\SalesChannel\RegisterRoute.php file, when data mapping is done for address, only the fields below are mapped:
private function mapAddressData(DataBag $addressData): array
{
$mappedData = $addressData->only(
'firstName',
'lastName',
'salutationId',
'street',
'zipcode',
'city',
'company',
'department',
'countryStateId',
'countryId',
'additionalAddressLine1',
'additionalAddressLine2',
'phoneNumber'
);
if (isset($mappedData['countryStateId']) && $mappedData['countryStateId'] === '') {
$mappedData['countryStateId'] = null;
}
return $mappedData;
}
How can i save custom fields for an address when developing an app? is it possible ?
There's this repository (not mine) with an example on how to add additional inputs to the storefront and how to map them to the payload when persisting the address. Basically you subscribe to the corresponding CustomerEvents, take the user inputs and enrich the payload.

NetSuite SuiteScript 2.0 get Company Name

Is there any way to get the current company name when on a transaction entry page? Either some session object, global object, or some sort. Kind of like how you can get the user name from the runtime module? I see country name, but cannot seem to get the company name. Using a client script for this.
How I get the user name:
runtime.getCurrentUser().name
thank you
Here is the working code now, but trying to figure a way to get this on a client script. Possible to pass this value?
var companyInfo = config.load({
type: config.Type.COMPANY_INFORMATION
});
var compname = companyInfo.getValue({
fieldId: 'companyname'
});
log.debug(compname);
scriptContext.form.addButton({
id: "custpage_mybutton",
label: "View Documents",
functionName: "onButtonClick"
});
scriptContext.form.clientScriptModulePath = "SuiteScripts/dl_cs_vendorbill.js";
In server side scripts you can do this:
require(['N/config'], config=>{
const info = config.load({type:config.Type.COMPANY_INFORMATION});
log.debug({title:'Current company Info', details:info.getValue({fieldId:'companyname'}));
});
For a client script you could use that code in a paired user event script.
In the before phase of the UE you'd do the lookup and then add a hidden field to the form with the company name as a value. Your client script can then look that up like any normal getValue

Angular Firebase Firestore add new fields to user collection

I'm new to Angular and I'm having a problem that I think is simple ...
I am making an application for discount coupons, I have a registration with Firebase, and I have in my code so that when registering the information is saved in FireStore, in a collection called "users", where I keep "email", "id "and" Roles "(that way I have different roles),
the case is that I want to add more fields within this collection, example: "telefono", I found a way to add information, but only type (boolean), and I do not know how to pass it to (string), since the phone will have a format + 99-9999 + 999999 and not (true or false), I show you my code.
apiservice.ts
export class ApiService{
constructor(
private afs: AngularFirestore
) { }
estadoChange: boolean;
}
updateEstado(key){
this.afs.doc('users/' + key).update({
estado : this.estadoChange = !this.estadoChange
})
}
private-component.ts
actualizarEstado(key) {
this.afs.updateEstado(key);
}
private-component.html
<input type="text" (change)="actualizarEstado(user.id)">
With this I add a new field to the collection of the user trying to add the information,
But I want to add it as a string, and do not boolean, I hope you can help me, greetings!

How to retrieve model data for use inside of another model?

I'm working with an Ember app that ties into a Rails backend through a Sails.js API, and ran into an issue I can't find any similar examples for.
My Messages model has a sender_id column, which corresponds with the id column in my Users model. There is also a Profile model (belongs to User through user_id) that contains the username column, which is what I'd like to be able to access through the Messages model as 'senderName' (i.e., message.senderName). My models and routes are all hooked up, so I have access to all the data I need and everything displays correctly in the browser aside from my senderName function.
The plan was to look up the Profile object through its user_id (using sender_id) inside of Messages, then pull the username field from there. I've been able to receive Promises back from my Profile query, but from there I'm not sure how/if I can access the actual username field. Is there a way to do this, or am I trying to fit a square peg into a round hole?
I also tried accessing username by looking up the User object first (this.store.find('user'), etc.) and using my Rails associations for user.profile.username, but that didn't work either.
Models:
App.Message = DS.Model.extend({
sender_id : DS.attr('number'),
content : DS.attr('string'),
senderName: function() {
var id = this.get('sender_id');
var profile = this.store.find('profile', {user_id: id}).then(function() {
console.log(profile);
// What next? profile.username doesn't work
})
}.property('sender_id')
});
App.Profile = DS.Model.extend({
user: DS.belongsTo('user', { async: true }),
username : DS.attr('string'),
user_id : DS.attr('number')
});
App.User = DS.Model.extend({
profile: DS.belongsTo('profile', { async: true }),
email: DS.attr('string'),
name: DS.attr('string')
});
Template:
<script type="text/x-handlebars" data-template-name="messages/index">
<div class="small-12 column">
{{#each message in model}}
<p>From: {{message.senderName}}</p>
<p>Content: {{message.content}}</p>
{{/each}}
</div>
</script>
Here's an example of the promises I'm receiving back in the browser console:
Promise {_id: 93, _label: undefined, _state: undefined, _result: undefined, _subscribers: Array[0]…}
Thanks for your help!
If I'm getting the question correct, you are having trouble accessing the fields in the model? If so, try:
profile.get('username')
You're really overcomplicating this :-)
Message also belongs to sender, why did you define sender_id as a number?
If you delete senderName and change the sender_id property to:
sender: DS.belongsTo('user', { async: true }),
You can use message.get('sender.profile.username'). I think your main issue here is that you're trying to do an async operation in the computed property, which is a no-no. You can alias the senderName if you like, via senderName: Ember.computed.alias('sender.profile.username') if you end up using it a lot.

Resources