Tuesday, June 5, 2012

Create PersonName (DirPersonName) records for Person Type Address

In Microsoft Dynamics AX 2012, suppose you want to create Person Names (DirPersonName) records for the party address that is not of an Orgranization type but of type Person.

Below is the code that creates records in DirPersonName table in Dynamics AX 2012 against a vendor using csv file as a datasource.

static void DirPersonCreate(Args _args)

{

DirPartyTable dirParty;

DirPerson dirPerson;

dirPersonName dirPersonName;

CommaTextIo file;

container record;

DirPartyNumber partyId;

;

file =
new CommaTextIo("C:\\DirPerson.csv",'r');

file.inFieldDelimiter(',');

while (file.status() == IO_Status::Ok)

{

record = file.read();

partyId =
conPeek(record, 1);

dirParty = DirPartyTable::findRec(partyId, false, DirPartyType::Person);



if (dirParty.RecId)

{

dirPerson = dirPerson::find(dirParty.RecId);

dirPersonName.clear();

dirPersonName.FirstName =
conPeek(record, 2);

dirPersonName.MiddleName = conPeek(record, 3);

dirPersonName.LastName = conPeek(record, 4);

dirPersonName.ValidFrom = DateTimeUtil::minValue();

dirPersonName.ValidTo = DateTimeUtil::maxValue();

if (!dirPerson)

{

dirPerson.initValue();

dirPerson.updateName(dirPersonName);

dirPerson.insert();

}

else

{

dirPerson.updateName(dirPersonName);

dirPerson.update();

}

dirPersonName.Person = dirPerson.RecId;

dirPersonName.insert();

}

}

}