Copying records

Follow this Source

Advertisements

Find existing record in other company

Have you already have the problem to insert a record a company to an other company ?

The problem is : “I can’t know if the index exist already in the other company !”

There is two methods are create from these two posts : SysDictCode and Dilip’s blog.

The first method allow you to find the primaryKey list an make a container with the fieldId.

container findPrimaryField(Common _record)
{
    str             primaryKey;
    container       ret;
    List            primaryKeyList;
    ListEnumerator  enumerator;
    ;

    primaryKeyList  = SysDictTable::getUniqueIndexFields(_record.TableId);
    enumerator      = primaryKeyList.getEnumerator();

    while(enumerator.moveNext())
    {
        primaryKey   = strRem(enumerator.toString(), '[]');
        ret         += str2int(primaryKey);
    }

    return ret;
}

The second while check the entire table, and make a check for each fieldId in the container.

And when you found a record, you return this record.

Common findRecord(Common _record1, DataAreaId _companyId)
//Look if the index of record1 exist in record2 (they must be ine different company !)
{
    int             i;
    container       primaryKeysList;
    boolean         same;
    FieldId         fieldId;
    Common          ret;
    Common          record2;

    DictTable       dicTableLoc;
    ;

    changecompany(_companyId)
    {
        dicTableLoc     = new DictTable(_record1.TableId);
        record2         = dicTableLoc.makeRecord();
    }

    primaryKeysList = this.findPrimaryField(_record1);

    if (primaryKeysList)
    {
        while select record2
        {
            same = true;
            for (i = 1; i <= conlen(primaryKeysList); i++)
            {
                fieldId = conpeek(primaryKeysList, i);

                if (_record1.(fieldId) != record2.(fieldId))
                {
                    same = false;
                }
            }

            if (same)
            {
                ret = record2;
                break;
            }
        }
    }

    return ret;
}