Hi everyone, I would like to merge any duplicate l...
# suitescript
k
Hi everyone, I would like to merge any duplicate leads coming from online form to the existing one and if anyone is manually typing in a new lead, setup a duplication check and make sure they can;t enter it, if the lead already exists. How would I be able to achieve this?
b
you can do a search using the duplicate filter (or use search.duplicates) to detect duplicates and then use the entity duplication task to merge them
k
All this would be done in a scheduled script or user event script?
b
either
k
So basically, do a search.duplicate based on my criteria, if it exists then merge using entity duplication task and save, if its a scheduled script, would be done beforeSubmit, right? How would I make sure that the lead is coming from an online source or manual input?
b
the deduple task doesn't trigger user events and is asynchronous
k
I'm not sure what that means exactly
b
have you used the entity duplicate resolution mass update
k
Not yet
b
try it out, its what drives the entity duplication task
essentially you will be passing parameters to that mass update from script
k
Sorry I'm relatively new yo suitescript, does that mean using the script and search in it, pass the internal id of the dupes to task.EntityDeduplicationTask, and that will merge the dupe record to the original record?
b
all of the task related modules work by sending the task to a queue
which process the task at a later date
But otherwise yes. Make your script do a search to find duplicates. If there are duplicates submit an entity deduplication task
your choice of script depends on when you want to do your logic
if you want it to run on a schedule and process many records at a time, choose a map-reduce script
if you want it to run it after an individual record is submitted, choose user event
if you want to run it in the ui before the user submits the record, choose a client script
k
Yes, I was working on the UE script
Using search.duplicates to get the duplicates and update it using entity deduplication task
Would it be something like this?
function beforeSubmit(){ log.debug('Started'); var newRecord = context.newRecord; var newEmail = newRecord.getValue({ name: 'email' }); var searchResult = search.duplicates({ type: search.Type.CONTACT, fields:{ 'email': newEmail } }); var recIds = []; for (var i =0 ; i < searchResult.length; i++){ recIds.push(searchResult[i].id); } var dedupeTask = task.create({taskType task.TaskType.ENTITY_DEDUPLICATION}); dedupeTask.entityType = task.DedupeEntityType.CONTACT; dedupeTask.masterSelectionMode = task.MasterSelectionMode.MOST_RECENT_ACTIVITY; dedupeTask.RecordsIds = recIds; var dedupeTaskId = dedupeTask.submit(); log.debug('Ended'); }
b
i believe you would need this afterSubmit
beforeSubmit won't have an id on the current record so it would be excluded from duplicates
k
@battk Currently I have the following code and it doesnt't detect any duplicates at all
practice.js
b
is the record marked by netsuite as a duplicate
k
Yes it does
Right after I save the second (duplicate) lead, it pops up an alert and now I am getting the following error
message has been deleted
b
i think your error message is saying that you have an invalid recordIds group
probably because you only had 1 element at the time
k
I had one lead stored in NS and when I tried to add another lead with the same entityname and email, this is what I got.
b
Your code doesn't add newRecord.id to recordIds