Reputation: 166
There is a need to create a custom structure in NetSuite. The structure must be as closer as possible to existing Department record type. All standard classifications are occupied, we need more classifications.
I created a custom record type with two fields: name and parent. Parent points to the record of the same type. The question is how to create a nicely formatted name like: "Parent_of_the_parent : the_parent : child". We need to specify all parent names in record name. What is the best way to achieve this with NetSuite customization capabilities?
Upvotes: 0
Views: 1352
Reputation: 140
I like the previous solution by Suite Resources if there is only 3 levels to the classifications.
If you absolutely NEED many levels to your classifications (like departments or class), you can create a custom record (with inline editing disabled) and use a script to update all of the sub-classifications that change if you edit a classification.
For example, if you create a custom record with an ID = 'customrecord_classification' and you have fields (custrecord_classificationname [type=FreeFormText], custrecord_classificationparent [type=ListRecord] referring to the new custom record type, and custrecord_classificationfullname [type=FreeFormText]), then you can use/modify the following script I wrote for you below. I tested it and it works great, but if you want to use it some error handling, logging, etc. should be added.
function beforeSubmit(type) {
// Make sure you turn off inline editing on the custom record type so we don't have to handle xedit events
// Get the context the record was changed and only run when the change was made in the UI
var context = nlapiGetContext();
if (context.getExecutionContext() == 'userinterface') {
// If creating a new classification record
if (type == 'create' ) {
// Get the current record fields: internal ID, custrecord_classificationname, custrecord_classificationparent
var classificationID = nlapiGetRecordId();
var classificationName = nlapiGetFieldValue('custrecord_classificationname');
var parentID = nlapiGetFieldValue('custrecord_classificationparent');
// Check if the classification has a parent specified
var hasParent = parentID.length == 0 ? false : true;
var classificationFullName;
// If the classification has a parent specified lookup the parent's full name and append the new classification name to it
if (hasParent == true) {
var parentFullName = nlapiLookupField('customrecord_classification', parentID, 'custrecord_classificationfullname');
classificationFullName = parentFullName + ' : ' + classificationName;
}
// If the classification does not have a parent then the full name is equal to the name
else {
classificationFullName = classificationName;
}
// set the classification full name on the current record
nlapiSetFieldValue('custrecord_classificationfullname', classificationFullName);
}
// If editing an existing classification record
else if (type = 'edit') {
var classificationID = nlapiGetRecordId();
// get the recored before the record was edited to grab the old value for the full classification name
var previousClassificationRecord = nlapiGetOldRecord();
var oldClassificationFullName = previousClassificationRecord.getFieldValue('custrecord_classificationfullname');
var classificationName = nlapiGetFieldValue('custrecord_classificationname');
var parentID = nlapiGetFieldValue('custrecord_classificationparent');
var hasParent = parentID.length == 0 ? false : true;
var classificationFullName;
if (hasParent == true) {
var parentFullName = nlapiLookupField('customrecord_classification', parentID, 'custrecord_classificationfullname');
classificationFullName = parentFullName + ' : ' + classificationName;
}
else {
classificationFullName = classificationName;
}
nlapiSetFieldValue('custrecord_classificationfullname', classificationFullName);
var filters = new Array();
var columns = new Array();
// Filter for a saved search for all classifications that have a full classification name beginning with the old classification name
filters[0] = new nlobjSearchFilter( 'custrecord_classificationfullname', null, 'startswith', oldClassificationFullName );
// Make sure the current record is not one of the records returned
filters[1] = new nlobjSearchFilter( 'internalid', null, 'noneof', classificationID );
columns[0] = new nlobjSearchColumn( 'custrecord_classificationfullname' );
var affectedClassifications = nlapiSearchRecord( 'customrecord_classification', null, filters, columns );
// loop through all of the classifications that need to be updated
for ( var i = 0; affectedClassifications != null && i < affectedClassifications.length; i++ ) {
var subClassificationToFix = affectedClassifications[i];
var subClassificationToFixID = subClassificationToFix.getId();
// load the sub-classification record to fix, correct it's value then re-submit it
var subClassificationToFixRecord = nlapiLoadRecord('customrecord_classification', subClassificationToFixID);
var subClassificationToFixOldFullName = subClassificationToFixRecord.getFieldValue('custrecord_classificationfullname');
var subClassificationToFixNewFullName = subClassificationToFixOldFullName.replace(oldClassificationFullName, classificationFullName);
subClassificationToFixRecord.setFieldValue('custrecord_classificationfullname', subClassificationToFixNewFullName);
var id = nlapiSubmitRecord(subClassificationToFixRecord, false);
}
}
}
}
Upvotes: 1
Reputation: 1164
It's only 3 levels deep? Or more?
If 3, create a field on the parent that references the grandparent (store value is unchecked, use the Sourcing and Filtering tab).
On the child, you can do the same thing - reference the parent and reference to the grandparent.
Upvotes: 0
Reputation: 140
You'd probably need a name (full name with parent), parent, and name (no hierarchy).
You could then fill out the Name (no hierarchy) value (Ex. Socks), specify the parent (Ex. Clothing).
You could then populate the full name field by loading the parent record and concatenating the name (no hierarchy) value.
I'm not sure if that makes sense or not, but if you notice in Netsuite they use that structure also with the name and name (no hierarchy) that you can see if you create an item saved search as an example and you should see that both of those are available on the results.
Upvotes: 0