Thursday, July 9, 2009

AX: Rename Primary Key

Here is an example to rename existing item number by reading csv. RenameItems.CSV contains 2 columns. Column 1 contains old item number and Column 2 contains new item number. This job can be used to rename any primary key just by modifying few code lines.

static void renamePrimaryKey(Args _args)
{
#define.read('r')

Container line;
str oldValue;
str newValue;
fieldId fieldId;
Common common;
CommaIO fileIO;
FileName file = @'F:\ImportData\RenameItems.csv';
;
ttsbegin;
//Asserting rights
new FileIOPermission(file,#read).assert();

// BP Deviation Documented
fileIO = new CommaIO(file,#read);

if(!fileIO fileIO.status() != IO_Status::Ok)
{
throw error(strfmt("@SYS76826",file));
}
while(fileIO.status() == IO_Status::Ok)
{
line = fileIO.read();
oldValue = conpeek(line,1);
newValue = conpeek(line,2);
if(!InventTable::exist(newValue))
{
common = InventTable::find(oldValue);
fieldId = fieldnum(InventTable,ItemId);
//SIG - start
if (isConfigurationkeyEnabled(configurationkeynum(SIG)))
{
SIGBaseDocument::checkAndCacheRename(common,fieldId,newValue);
}
//SIG -end

// CC Start
CCPrimaryKey::renamePrimaryKey(common, newValue, fieldId);
// CC End
common.(fieldId) = newValue;common.renamePrimaryKey();
}
}
ttscommit;
}

No comments:

Post a Comment