Combining Duplicate Accounts
From C3 Wiki
This page is still under construction, information may be incomplete until this warning is removed
[edit] The Problem - Duplicate Accounts
Inevitably it will come to an administrators attention that a particular person has two accounts in the sytem.
[edit] Creating an Activity to Combine Duplicate accounts
- Create a new activity on CDT type Person called 'Combine Accounts'
- Add an attribute to that activity called duplicate of type Person
- Expose that attribute on the Activity Form
- For additional safety, we added a custom search filter to only display users with a 'deactivated_' prefix on their userid to prevent the administrator from accidentally selecting the wrong user.
- Add the following code to the Actvity's Pre-Script
NOTE Every Person in the system is in 3 sets by default. Two are shared system sets, one is their .extension.permissionPersonSet . We do not want to change any data in these sets. Make sure that the OID's of the two system sets in the code below match up with your store's data (I would think that they should automatically). The script below also removes the duplicate accounts roles (so they cant log in etc), so you need to update those to site specific values as well
// The purpose of this script is take the OID of some Person entity (PersonA), find all other Entities in the entire system that refer
// to it and then replace the Person with another person (PersonB)
// Input Parameter. Put in OID of the object for which you want to find refrences to it. For example - the OID of the duplicate Person account
var INPUT_ENTITY = activity.customAttributes.duplicate; // PersonA
var CHANGETO_ENTITY = targetEntity; // PersonB
var debug_string = "";
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Instantiate the inputs
var ReferredToEntity = activity.customAttributes.duplicate;
var ChangeToEntity = targetEntity;
if(ReferredToEntity.getEntityTypeName() != "Person" || ChangeToEntity.getEntityTypeName() != "Person"){
throw(new Error(-1, "One of the input OIDs is not for a Person"));
}
debug_string += "The Person to find is " + ReferredToEntity.fullName() + "\n";
debug_string += "The Person to change to is " + ChangeToEntity.fullName() + "\n";
/************* cpg 10.7.2008 - Handle all sets of person that contain the duplicate first ************/
var setSetReferrers = EntityUtils.getAllSetsContainingEntity(ReferredToEntity);
var personExtensionSet = ReferredToEntity.extension.permissionPersonSet; // we dont want to change the permission list inside of person extension to the other user
debug_string += "Number of entities that hold a reference to a set containing the Input Person = " + setSetReferrers.count + "\n";
if (setSetReferrers.count > 3){ // there will always be at least three sets containing the person that must remain, 1 in personExtension.permissionPersonSet, and two System sets that shouldn't be touched
for (var i = 1; i <= setSetReferrers.count; i++){
var aSet = setSetReferrers(i);
if (aSet == "com.webridge.eset.UniqueTypeEntitySet[OID[2816822A8286D311BBA200104B6A6E19]]"){
debug_string += "Ignored System UniqueTypeEntitySet\n";
}
else if (aSet == "com.webridge.eset.EntitySet[OID[2855C408E718D511A943005004B85800]]"){
debug_string += "Ignored System EntitySet\n";
}
else if (aSet == personExtensionSet){
debug_string += "Ignored Permission List Set in Person Extension\n";
}
else{
debug_string += "Processing set Change\n";
aSet.AddElement(ChangeToEntity);
aSet.removeElement(ReferredToEntity);
}
}
activity.name += ": " + setSetReferrers.count + " set referrers processed - please run again";
}
else{ // now process all single entity referrers
// Run method to return an enumerated set of all entities that hold a reference to the Input Entity
var setReferrers = EntityUtils.getEntityReferrers(ReferredToEntity);
// Print out the number of other Entities that hold a reference to the Input Entity
debug_string += "Number of objects that hold a reference to the Input Person = " + setReferrers.count + "\n";
var targetType = "";
var enumAttrDescs = null;
var strAttributeName = "";
// The size might be to large for a single transaction, limit the size and set a flag
var maxLoop = 500;
if (setReferrers.count > maxLoop){
activity.name += ": " + maxLoop + " records processed - " + (setReferrers.count - maxLoop) + " remain";
}
else{
maxLoop = setReferrers.count;
activity.name += ": " + maxLoop + " records processed - accounts successfully merged";
var PI = wom.getEntityFromString("com.webridge.entity.Entity[OID[1CF1C67BCEB3894288BCCD1D8901C587]]");
var PIandS = wom.getEntityFromString("com.webridge.entity.Entity[OID[432F04251C068C47A13019D840074014]]");
activity.customAttributes.duplicate.roles.removeElement(PI);
activity.customAttributes.duplicate.roles.removeElement(PIandS);
}
// Loop thru the set
for(i=0; i<maxLoop; i++){
objReferrer = setReferrers(i+1); // This is an Object that holds a reference to the Input Person
debug_string += (i+1) + ") Type of Referrer = " + objReferrer.getEntityTypeName() + " and OID = " + String(objReferrer) + "\n";
// For the current Object type, find all attributes of type Person
targetType = objReferrer.getEntityTypeName();
enumAttrDescs = new Enumerator(AttributeDescription.getAllForEType(targetType, false, "Entity/Person"));
// Loop thru this collection of Person attributes and replace PersonA with PersonB
for (;!enumAttrDescs.atEnd(); enumAttrDescs.moveNext()){
attrDesc = enumAttrDescs.item();
strAttributeName = attrDesc.qualifiedName;
if(objReferrer.inheritsFrom(ApplicationEntity)){
objToChange = objReferrer.getQualifiedAttribute(strAttributeName);
if(objToChange != null){
// See if this object is PersonA
if(objToChange == ReferredToEntity){
debug_string += " Found a property of type Person with the name '" + attrDesc.qualifiedName + "'. Replacing " + objToChange.fullName() + " with " + ChangeToEntity.fullName() + "\n";
objReferrer.setQualifiedAttribute(strAttributeName, ChangeToEntity);
}
}
}
}
// Special case the BGQNodeMethodArgument
if(objReferrer.getEntityTypeName() == "BGQNodeMethodArgument"){
if(objReferrer.reference == ReferredToEntity){
debug_string += " Replacing the object in .reference with " + ChangeToEntity.fullName() + "\n";
objReferrer.reference = ChangeToEntity;
}
}
}
}
//throw(new Error(-1, debug_string));
