How to Merge different person objects

Below, can be found the instruction on how to merge two different person objects into one single object.


Steps:
    1. Identify the 2 object types involved.
    2. Find both objects by searching for them and write down their License Number.
    3. Go to Move ELCM Objects integration and fill in the blanks for Object types and License numbers, also review it, read the steps, make sure it makes sense as each case can be unique.

     When you run the integration with the run flag set to false, you can see a dump of both objects’ data for comparison.
    - Here the process is unique to each case, but usually involves comparing the 2 sets and deciding which to keep in the destination object.

    4. Copy fields from the most recent object (most of the time) to the destination object by manually putting the lines in the integration.
    5. The integration should handle moving the objects, media, handle ELCM / ESM records from here.

There could be gaps depending on the case, so this is the high level overview of the process.

#include<app/entityLifeCycleManager/libCommonUtils> ME().LogStart(); function customDataForObject(objectType, objectId) { CONTEXT().Set('objectType', objectType); CONTEXT().Set('objectId', objectId); return collect(getCustomFieldNamesByObject(objectType)) .flatMap(function (fieldName) { var r = []; r[fieldName] = getSelectedCustomFieldValuesByObjectTypeAndID(fieldName, CONTEXT().Get('objectType'), CONTEXT().Get('objectId'), true); return r }) .sortKeys() .merge(is_numeric(strpos(objectType, 'person')) ? getPersonBaseDataFromBagOfIds(objectType, [objectId]) : []) .toArray(); } // Set to true to process the records or false to just dump the objects data to prepare for processing var run = false; /** * STEP 1 * * Define your globals */ var sourceObjType = 'person'; var destinationObjType = 'person3'; var sourceObjLicNumber = 'O-000986'; var destinationObjLicNumber = 'GC-0304'; /** * PROCESS - PROCESS - PROCESS - PROCESS - PROCESS - PROCESS - PROCESS - PROCESS - PROCESS */ /** * STEP 2 * * Dump and compare the objects' data */ var sourceObj = findObjectsByCustomFieldValue(sourceObjType, 'License Number', sourceObjLicNumber)[0]; var sourceObjReportIds = getReportIdsForObject(sourceObjType, sourceObj); var destinationObj = findObjectsByCustomFieldValue(destinationObjType, 'License Number', destinationObjLicNumber)[0]; var destinationObjReportIds = getReportIdsForObject(destinationObjType, destinationObj); if (isBlank(sourceObj) || isBlank(destinationObj)) { ME().Log('Not all object ids found: Source [%s] - Destination [%s]', sourceObj, destinationObj); return; } dump(customDataForObject(sourceObjType, sourceObj)) // dump(sourceObjReportIds) dump(customDataForObject(destinationObjType, destinationObj)) // dump(destinationObjReportIds) if (false == run) { ME().Log('Not running'); return; } ME().Log('Running'); /** * STEP 3 * * Manually modify the fields and values on the destination object to the most recent version you want to keep */ SETTINGS().TempChange(ELCM_getReadOnlySettingsKeyByObjectType(destinationObjType), 'FALSE'); var report = loadReportById(getMostRecentReportIdForObject(destinationObjType, destinationObj)) // report.GetChild(destinationObjType)[0].custom_data.GetChild('Address').Set('538 Belcaro Dr'); // report.GetChild(destinationObjType)[0].custom_data.GetChild('City').Set('Kings Mountain'); report.GetChild(destinationObjType)[0].custom_data.GetChild('Department').Set('Key-Gaming Commision'); report.GetChild(destinationObjType)[0].custom_data.GetChild('Distinguish Marks').Set('N/A'); report.GetChild(destinationObjType)[0].custom_data.GetChild('Driver License Expiration').Set('2026-12-18'); report.GetChild(destinationObjType)[0].custom_data.GetChild('Driver License Number').Set('011534579'); report.GetChild(destinationObjType)[0].custom_data.GetChild('Driver License State').Set('South Carolina'); report.GetChild(destinationObjType)[0].custom_data.GetChild('Email').Set('justin.pintoff@catawba.com'); report.GetChild(destinationObjType)[0].custom_data.GetChild('Expiration Date').Set('2026-01-26'); report.GetChild(destinationObjType)[0].custom_data.GetChild('Issuance Date').Set('2024-01-26'); report.GetChild(destinationObjType)[0].custom_data.GetChild('Required Action').Set('Permanent'); report.GetChild(destinationObjType)[0].custom_data.GetChild('Position').Set(null); report.GetChild(destinationObjType)[0].custom_data.GetChild('Workflow Status').Set('Approval Pending'); report.GetChild(destinationObjType)[0].specific_data.GetChild('Middle Name').Set('William'); // report.GetChild(destinationObjType)[0].specific_data.GetChild('Eye Color').Set('Hazel'); // report.GetChild(destinationObjType)[0].specific_data.GetChild('Hair Color').Set('Gray'); // report.GetChild(destinationObjType)[0].specific_data.GetChild('Date of Birth').Set('1956-01-04'); report.GetChild(destinationObjType)[0].specific_data.GetChild('Height').Set('76'); // report.GetChild(destinationObjType)[0].specific_data.GetChild('Weight').Set('186'); /** * STEP 4 * * Copy media from source to destination object */ var oldMedia = getMediaIdsForObject(sourceObjType, sourceObj); for (var x = 0; x < count(oldMedia); x++) { ME().Log('Copying media %d', oldMedia[x]); var mb = createMediaBlockFromString(base64_decode(getMediaAsBase64StringById(oldMedia[x])), getMediaFileNameForMediaById(oldMedia[x])); report.GetChild(destinationObjType)[0].related_media.AddNewMediaObject(mb); } if (report.save()) { ME().Log('Saved destination object'); } else { ME().Log('ERROR destination object: %s', getLastError()); } /** * STEP 6 * * Process each of the source object reports and replace them with the destination object */ ME().Log('Moving reports to destination object'); for (x = 0; x < count(sourceObjReportIds); x++) { var r = loadReportById(sourceObjReportIds[x]); r.removeExistingObjectFromReport(sourceObjType, sourceObj); r.addExistingObjectToReport(destinationObjType, destinationObj); r.core[0].custom_data.GetChild(cfg['fields']['entityName']).Set(ELCM_getEntityNameByObjectType(cfg, destinationObjType)); r.core[0].custom_data.GetChild(cfg['fields']['entityID']).Set(destinationObjLicNumber); if (r.save()) { ME().Log('Moved [%d]', sourceObjReportIds[x]); } else { ME().Log('ERROR [%d]: %s', sourceObjReportIds[x], getLastError()); } } /** * STEP 7 * * Handle ELCM / ESM records */ var entityConfig = collect(cfg['entities']) .firstWhere('config.type', destinationObjType); if (is_null(entityConfig)) { ME().Log('Entity Config not found, skipping ELCM and ESM record creation'); return; } var investigationStartReport = loadReportById( filterBagOfIDsByReportTypes('core', getReportIdsForObject(destinationObjType, destinationObj), entityConfig['reports']['newProcess'])[0] ); // Create ELCM - ESM records createELCMMetadataRecord( investigationStartReport.core[0].object_id.Get(), destinationObjType, investigationStartReport.GetChild(destinationObjType)[0].object_id.Get(), strcat(investigationStartReport.GetChild(destinationObjType)[0].specific_data.GetChild('Last Name').Get(), ', ', investigationStartReport.GetChild(destinationObjType)[0].specific_data.GetChild('First Name').Get()), investigationStartReport.GetChild(destinationObjType)[0].custom_data.GetChild('License Number').Get(), true, '' ); createESMIssuanceRecord( destinationObjType, investigationStartReport.GetChild(destinationObjType)[0].object_id.Get(), investigationStartReport.GetChild(destinationObjType)[0].custom_data.GetChild('Current Status').Get(), investigationStartReport.GetChild(destinationObjType)[0].custom_data.GetChild(cfg['fields']['process']).Get(), investigationStartReport.GetChild(destinationObjType)[0].custom_data.GetChild('Issuance Date').Get(), investigationStartReport.GetChild(destinationObjType)[0].custom_data.GetChild('Expiration Date').Get() ); ME().LogEnd();