Monday, February 24, 2020

Checks for duplicate entities that have the same names

there are different forms where we want to perform a check for duplicate entry names.An example is:

\Forms\HcmPersonalContactNew\Methods\checkDuplicateName

which is using this static method:

\Classes\DirUtility\checkDuplicate

the problem with this method is that it works only in a form which has as datasource a DirParty entity. In order to be able to use the same code also in other contexts I slightly modified the code like this:

public static boolean checkDirPartyDuplicate(Common _nameRecord, DirPartyType _partyType, str _entityName)
{
    //Enrico: I adapted the code from \Classes\DirUtility\checkDuplicate to use it also when not in a form
    
    Common common;
    DictTable partyDicttable;

    FormRun formRun;
    Args    args;
    Object  formObject;
    Common  nameRecord;
    FormDataSource  fds;

    args = new Args(formStr(DirPartyVerification));
    args.record(_nameRecord);
    args.parmEnumType(enumNum(DirPartyType));
    args.parmEnum(_partyType);
    args.parm(_entityName);
    formRun = classfactory.formRunClass(args);
    formRun.init();
    formRun.run();
    formRun.wait();


    if (formRun.closedOk() && formHasMethod(formRun, identifierStr(getName)))
    {
        formObject = formRun;
        nameRecord = formObject.getName();

        if (nameRecord.RecId)
        {
            partyDicttable = new SysDictTable(nameRecord.TableId);
            if(partyDicttable)
            {
                common = partyDicttable.makeRecord();
            }
            //we reselect the party since quick create wich uses this code path requires non pessimistic lock selection
            select common where common.RecId == nameRecord.RecId;
            _nameRecord.data(common);
            
        }
    }
    else
    {
        return false;
    }

    return true;
}


now can be used like this:

static void MYD_Functions_checkDirPartyDuplicate(Args _args)
{
    DirPersonName dirPersonName;
    boolean nameChecked = true;
    
    dirPersonName.FirstName = 'enrico';
    dirPersonName.LastName = 'fuchs';
    
    if (DirParameters::find().UseDuplicateCheck == NoYes::Yes
            && DirPersonName::nameLikeCount(dirPersonName.FirstName, dirPersonName.MiddleName, dirPersonName.LastName) > 0)
        {
            nameChecked = MYD_Functions::checkDirPartyDuplicate(dirPersonName, DirPartyType::Person, tableStr(DirPerson));
        }
    
    if (nameChecked)
        info(strFmt('%1', dirPersonName.RecId));
}

No comments:

Post a Comment