RE: 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?

a2metalcore Rookie Asked on March 3, 2021 in SuiteScript.
Add Comment
1 Answers
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.

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

on July 25, 2021.
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,
on July 25, 2021.

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();
});
on July 28, 2021.

Hi battk,   Thanks for your reply! This worked fine!

 

It turns out that I need to enable and disable some features, and after doing that it worked.

 

Thanks for your help!!!

on August 2, 2021.

This works perfectly!

I also have to create hundreds of “other relationships” for customers for an integration between 2 systems. I’m using a SuiteTalk SOAP connector that I built, but of course, the dupe merge needs to be in SuiteScript. So I made a RESTLet using the above code from battk. After creating a Customer and Vendor (saving the resulting internal IDs), I pass them in a JSON string to the RESTLet.

Perfect!

One caveat is that you cannot set the external ID field on either the Customer or Vendor, because you will end up with a DUPE ENTITY error. In my case, the same external ID exists for both customer and vendor. So don’t set the external ID up-front. Once the merge is complete, you can set the external ID on the surviving entity.

on August 9, 2021.

Hello @SuiteResources

I attempted to reach out to you directly through this forum, but I keep getting an “Email Failed” type message.  Either way I hope I can establish contact.  I’m actually in a similar boat as this thread problem and solution.  You mentioned that you were able to update the external ID after the merge and was wondering if I could pick your brain on the process involved to accomplish this.

I’m a bit new to NetSuite, as well as JavaScripting *run away and don’t respond….. NOW* but have done some coding and actively query databases so all of these concepts are not foreign to me.  I hope I may be able to link up with you, or maybe someone on this thread for some guidance!

on October 27, 2021.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.