Skip to content
This repository has been archived by the owner on May 2, 2023. It is now read-only.

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit e57624a
Author: James Christie <[email protected]>
Date:   Sun Oct 28 22:05:59 2012 -0500

    bugfixes for transformation line item handling

commit e0d1c86
Author: James Christie <[email protected]>
Date:   Sun Oct 28 20:40:33 2012 -0500

    removed some more old code

commit d39f1f2
Author: James Christie <[email protected]>
Date:   Sun Oct 28 20:39:00 2012 -0500

    removed iteration from previous version of the transform operation

commit e506a0b
Author: James Christie <[email protected]>
Date:   Sun Oct 28 20:24:27 2012 -0500

    full line item operation coverage on record transformations
  • Loading branch information
James Christie committed Oct 29, 2012
1 parent ac382a3 commit f96d218
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 30 deletions.
20 changes: 8 additions & 12 deletions lib/netsuite-rest-client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,19 @@ def delete(record_type, internal_ids, options={})
results
end

def transform(initial_record_type, result_record_type, internal_ids, options={})
def transform(initial_record_type, result_record_type, internal_id, field_changes, sublist_changes, options={})
results = Array.new
params = { 'script' => @script_id,
'deploy' => @deploy_id }

internal_ids = internal_ids.map { |id| id.to_s }

internal_ids.each_slice(options[:batch_size] || DEFAULT_TRANSFORM_BATCH_SIZE) do |internal_ids_chunk|
payload = { 'operation' => 'TRANSFORM',
'initial_record_type' => initial_record_type,
'result_record_type' => result_record_type,
'internal_ids' => internal_ids }

results += parse_json_result_from_rest(:post, params, :payload=>payload)
end
payload = { 'operation' => 'TRANSFORM',
'initial_record_type' => initial_record_type,
'result_record_type' => result_record_type,
'internal_id' => internal_id,
'field_changes' => field_changes,
'sublist_changes' => sublist_changes }

results
parse_json_result_from_rest(:post, params, :payload=>payload)
end

def get_saved_search(record_type, search_id, options={})
Expand Down
67 changes: 49 additions & 18 deletions lib/restlets/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,31 +266,62 @@ function transformRecords(request) {
* Description: Transforms records from their original, given type to the requested type.
*
* Params:
* request['initial_record_type']: String of the initial record type to lookup
* request['result_record_type']: String of the record type post-transformation
* request['id_list']: Array of record ids to fetch
* request['initial_record_type']: String of the initial record type to lookup
* request['result_record_type']: String of the record type post-transformation
* request['internal_id']: Internal ID of the record to be transformed
* request['field_changes']: A hash of field names as keys with their values
* request['sublist_changes']: A hash of sublist names with assigned arrays of hashes;
* hashes within the list correspond by internal id to
* referenced line items. Line items referenced will be
* altered according to field values given, unreferenced
* items will be removed from the list in the transformed
* record.
*
* Return: Array of original record ids paired with the internal id of the transformed
* record or an error for a failed transformation
*/
var initialRecordType = request['initial_record_type'];
var resultRecordType = request['result_record_type'];
var internalIds = request['internal_ids'];
var results = [];

for(var index = 0; index < internalIds.length; index++) {
recordId = internalIds[index];

try {
newRecord = nlapiTransformRecord(initialRecordType, recordId, resultRecordType);
results = results.concat([recordId, newRecord.getFieldValue('id')]);
}
catch(exception) {
results = results.concat([recordId, formatException(exception)]);
var initialRecordType = request['initial_record_type'];
var resultRecordType = request['result_record_type'];
var internalId = request['internal_id'];
var fieldChanges = request['field_changes'];
var sublistChanges = request['sublist_changes'];

newRecord = nlapiTransformRecord(initialRecordType, internalId, resultRecordType);

// Alter field values on transformed record
for(var field in fieldChanges) { newRecord.setFieldValue(field, fieldChanges[field]); }

for(var sublistName in sublistChanges) {
sublist = sublistChanges[sublistName];

// Alter line item values to match hash values, remove items that are not referenced
for(var index = 1; index < (newRecord.getLineItemCount(sublistName) + 1); index++) {
found = false;
deletionCount = 0;
lineItemsToRemove = [];

for(sublistItemDataIndex in sublist) {
sublistData = sublist[sublistItemDataIndex];

if(newRecord.getLineItemValue(sublistName, 'item', (index - deletionCount)) == sublistData['item']) {
found = true;

for(var sublistItemField in sublistData) {
if(sublistItemField == 'item') { continue; }
newRecord.setLineItemValue(sublistName, sublistItemField, (index - deletionCount),
sublistData[sublistItemField]);
}
}

if(!found) {
newRecord.removeLineItem(sublistName, (index - deletionCount));
deletionCount++;
}
}
}
}

return(results);
return([nlapiSubmitRecord(newRecord, false, false), [internalId, fieldChanges, sublistChanges]]);
}

function getSavedSearch(request) {
Expand Down

0 comments on commit f96d218

Please sign in to comment.