Is there a way to create a Vendor from a Customer as an Other Relationship using SuiteScript?

We need to generate 5000 new, linked Vendors from existing Customer records using a server-side SuiteScript (map/reduce, scheduled script).  Is there a way to create a Vendor record from a Customer record and link them as an Other Relationship using a server-side script?

Rookie Asked on March 3, 2021 in SuiteScript.
Add Comment
1 Answer(s)
Best answer

The safe bet is to create your 5000 vendors as new vendors and then use an EntityDeduplicationTask to merge your new vendors into your original customers.

The unsupported way is to use record.transform to transform your customer into the vendor you want to save.

The lame SuiteAnswer way is to essentially programatically navigate to the url that creates the relationship after you create/edit the customer. Minor effort can be made to write javascript code that does the navigation without requiring a record submit.

Intermediate Answered on March 3, 2021.

Can’t seem to get either of those approaches to create an ‘Other Relationship’ by associating an existing customer with an existing vendor or  creating a new associated vendor from an existing customer. The requirement is to perform the same functionality as the Relationships/Other Relationships popup in the Customer app which appears to update a multi-select field called ‘customer.otherrelationships’ which is not available in the API. The record.transform method appears to ignore “toType: ‘vendor'” when using “fromType: ‘customer'”.

var CUSTOMER_ID = 2725;
varcustomerRecord = record.transform({
fromType: record.Type.CUSTOMER,
fromId: CUSTOMER_ID,
toType: record.Type.VENDOR,
isDynamic: true,
});
customerRecord.save();
The EntityDeduplicationTask returns “Failed to submit job request: Inserted entity is not of declared primary flavor”:
var CUSTOMER_ID = 2728;
var VENDOR_ID = 6768;
var dedupeTask = task.create({ taskType: task.TaskType.ENTITY_DEDUPLICATION });
dedupeTask.entityType = task.DedupeEntityType.CUSTOMER;
dedupeTask.dedupeMode = task.DedupeMode.MAKE_MASTER_PARENT;
dedupeTask.masterSelectionMode = task.MasterSelectionMode.SELECT_BY_ID;
dedupeTask.masterRecordId = CUSTOMER_ID;
dedupeTask.recordIds = [VENDOR_ID];
var dedupeTaskId = dedupeTask.submit();
     

on March 4, 2021.

You will need to be more clear about whats wrong with the record.transform, your code looks fine to me

require(["N/record"], function (record) {
 var customer = record.create({ type: record.Type.CUSTOMER });
 customer.setValue({ fieldId: "companyname", value: "Company " + Date.now() });
 customer.setValue({ fieldId: "subsidiary", value: "1" });
 var customerId = customer.save();
 try {
  record.load({ type: record.Type.VENDOR, id: customerId });
 } catch (e) {
  log.error("im not a vendor yet");
 }
 record
  .transform({
   fromType: record.Type.CUSTOMER,
   fromId: customerId,
   toType: record.Type.VENDOR,
   isDynamic: true,
  }).save();
 log.debug("im a vendor now", record.load({ type: record.Type.VENDOR, id: customerId }));
 log.debug("and still a customer", record.load({
  type: record.Type.CUSTOMER,
  id: customerId,
 }));
});

on March 4, 2021.

Choose the correct mode for the EntityDeduplicationTask. You want to merge rather than set the parent.

The entity type should also match the record type of the record being merged into the parent. It should not match the record type of the parent

on March 4, 2021.

Hi battk,

I tried this code and it is not merging at all, but the task status is set to COMPLETED:

RE: Is there a way to create a Vendor from a Customer as an Other Relationship using SuiteScript?

 

Thanks!

on July 20, 2021.

Your code looks fine to me daj123, I cant try the code myself since its an image.

6 days ago.
Hi battk,

Thanks for your reply, here's the code block.


var CUSTOMER_ID = '33102';
var VENDOR_ID = '33101';
var dedupeTask = task.create({taskType: task.TaskType.ENTITY_DEDUPLICATION });
dedupeTask.entityType = task.DedupeEntityType.VENDOR;
dedupeTask.dedupeMode = task.DedupeMode.MERGE;
dedupeTask.masterSelectionMode = task.MasterSelectionMode.SELECT_BY_ID;
dedupeTask.masterRecordId = CUSTOMER_ID;
dedupeTask.recordIds = [VENDOR_ID];
var dedupeTaskId = dedupeTask.submit();


The entity deduplication seems to be running because I get a warning message not to modify it due to it, but when it completes they are still not merged.

Thanks,
5 days ago.

Your code looks fine to me, the following eventually merges the two for me. You may want to check the Duplicate Resolution Status  for failures.

RE: Is there a way to create a Vendor from a Customer as an Other Relationship using SuiteScript?

require(["N/record", "N/task"], function (record, task) {
  var customer = record.create({ type: record.Type.CUSTOMER });
  customer.setValue({ fieldId: "companyname", value: "Company " + Date.now() });
  customer.setValue({ fieldId: "subsidiary", value: "1" });
  var vendor = record.create({ type: record.Type.VENDOR });
  vendor.setValue({ fieldId: "companyname", value: "Vendor " + Date.now() });
  vendor.setValue({ fieldId: "subsidiary", value: "1" });
  var CUSTOMER_ID = customer.save();
  var VENDOR_ID = vendor.save();
  var dedupeTask = task.create({
    taskType: task.TaskType.ENTITY_DEDUPLICATION,
  });
  dedupeTask.entityType = task.DedupeEntityType.VENDOR;
  dedupeTask.dedupeMode = task.DedupeMode.MERGE;
  dedupeTask.masterSelectionMode = task.MasterSelectionMode.SELECT_BY_ID;
  dedupeTask.masterRecordId = CUSTOMER_ID;
  dedupeTask.recordIds = [VENDOR_ID];
  var dedupeTaskId = dedupeTask.submit();
});
3 days ago.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.
  • This site made possible by our sponsors:   Tipalti   Celigo   Limebox   Become a Sponsor