custom fields for address are not being saved? - shopware

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.

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.

Changing the REPRESENTS SUBSIDIARY field in Netsuite

I wish to change the subsidiary which is being represented by a certain entity, as I have used a dummy subsidiary before.
Is there a way to do this? I cannot change the field via the UI and I cannot find the field in the import functionality
You can find the "subsidiary" field in the UI by completing the following: go to a entity record in edit mode, un-layer all tabs (or add "&unlayered=T" to the url bar), use the browser Find feature (normally Ctrl + F) and search for subsidiary. If it does not come up this way identify the form used and open that for editing, un-layer all tabs, and use the find feature for subsidiary, elect to show or change the display settings then return to the record in edit mode to change.
OR via suitescript, if you have the field id (if native/standard NetSuite field id = subsidiary) you can run the following code in the browser console
require(['N/record'], function(record){
var Ids = [
//enter all entity internal ids here
];
for (var i=0; i<Ids.length; i++){
record.submitFields({
type: record.Type.______, //ref Suite Answer Id: 45161
id: Ids[i],
values: {
subsidiary: 1 //the internal id for the subsidiary you want to set
},
options: {
enableSourcing: false, //default is true
ignoreMandatoryFields : true //default is false
}
});
console.log('done', 'updated id: '+Ids[i]);
}
console.log('done', '# of records updated: '+i);
});
OR you can try Mass Updates (ref Suite Answer 8429), or CSV Imports (ref Suite Answer 10022).

Customized model values / output ngx-formly

I am trying to use ngx-formly to render forms. (using the FormlyFormConfig Object)
The form is rendering properly and i am getting model values as json object .
But is it possible that instead of setting the form fields values to model values i can set the values to the FormlyFormConfig "defaultValue" field ?
if not , is it possible to set additional attributes to the model.
This is because my form is tied to a field ids which are coming from backend and wated to set the values to same same formfieldconfig so that the backend knows which field the values belong to .
Here is the sample
As I correctly understood your question, you can add a default value to the field.
For example:
test.component.ts:
public form = new FormGroup({});
public model = {};
public options: FormlyFormOptions = {};
public fields: FormlyFieldConfig[];
this.fields = [{
key: 'name',
type: 'input',
defaultValue: name, // you can add default value
templateOptions: {
label: 'Name',
placeholder: 'Name',
},
}];
test.component.html:
<formly-form
[form]="form"
[fields]="fields"
[model]="model"
[options]="options"
></formly-form>

Custom User Registration: How to write gender to customer record

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...

How to update an index with new variables in Elasticsearch?

I have an index 'user' which has a mapping with field of "first", "last", and "email". The fields with names get indexed at one point, and then the field with the email gets indexed at a separate point. I want these indices to have the same id though, corresponding to one user_id parameter. So something like this:
function indexName(client, id, name) {
return client.update({
index: 'user',
type: 'text',
id: id,
body: {
first: name.first
last: name.last
}
})
}
function indexEmail(client, id, email) {
return client.update({
index: 'user',
type: 'text',
id: id,
body: {
email: email
}
})
}
When running:
indexName(client, "Jon", "Snow").then(indexEmail(client, "jonsnow#gmail.com"))
I get an error message saying that the document has not been created yet. How do I account for a document with a variable number of fields? And how do I create the index if it has not been created and then subsequently update it as I go?
The function you are using, client.update, updates part of a document. What you actually needs is to first create the document using the client.create function.
To create and index, you need the indices.create function.
About the variable number of fields in a document type, it is not a problem because Elastic Search support dynamic mapping. However, it would be advisable to provide a mapping when creating the index, and try to stick to it. Elastic Search default mapping can create you problems later on, e.g. analyzing uuids or email addresses which then become difficult (or impossible) to search and match.

Resources