Tuesday, June 5, 2012

Cannot publish data using Microsoft Dynamics Excel add-in

I was using Microsoft Dynamics AX excel add-in to import master data in Dynamics AX 2012 and the Publish All button was disabled after getting my excel sheet ready with data.

To publish the data using excel add-in, close the FIELD CHOSER window once you have selected all the fields you want to push to AX and then you will see Publish button enabled.

X++ code for document attachement in AX 2009

In Dynamics AX 2009, we had a requirement to attach document with Purchase order table using x++ code while performing few options with PO.

For that purpose, we have to use following x++ AOT objects
  • DocuRef table
  • DocuActionArchive class
I have written following generic method that will attach record to any table in AX based on parameters passed to it

void attachDoc(RefTableId _refTableId, RefRecId _refRecId, selectableDataArea _refCompanyId, FileName _name)
{
    DocuRef docuRef;
    DocuActionArchive archive;
    ;


    docuRef.clear();
    docuRef.RefRecId = _refRecId;
    docuRef.RefTableId = _refTableId;
    docuRef.RefCompanyId = _refCompanyId;
    docuRef.Name = _name;
    docuRef.TypeId = 'File';


    docuRef.insert();


    archive = new DocuActionArchive();
    archive.add(docuRef, _name);
}

To call this method simply write following line and your document will be attached.

this.attachDoc(tableNum(PurchTable), purchTable.RecId, purchTable.dataAreaId, filepathname);

Copying docuemnt attachment record from one table to another table

In Microsoft Dynamics 2012, recently, I had a requirement to attach a document that is attached to Purchase order table to another table.

There is a method copy available in DocuCopy class that does the job for us.

For that purpose I used the following code that copies the record attached to one table to other table.

MyTable table = table::find(...);
PurchTable purchTable = PurchTable::find(...);

Docu::copy(purchTable, table)

Copy method accepts two arguments Common _fromTable, Common _toTable

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();

}

}

}

Create Vendor Postal Address using x++ code

In Microsoft Dynamics AX. I came accross a requirement in which I had to create LogisticsPostalAddress records (i-e postal addressed for party (vendors/customers)) using x++ code. I tried to use excel add-in but it didn't work for me in case I have multiple postal addresses for a single vendor.

Following is the code that reads a csv file and then creates postal addresses for a vendor.

static void PostalAddressCreate(Args _args)

{

VendTable vendTable;

DirParty dirParty;

DirPartyPostalAddressView PostalAddress;

CommaTextIo file;

container record;

str countyId, zipcode;

;

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

file.inFieldDelimiter(',');

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

{

record = file.read();

vendTable = VendTable::find(
conPeek(record,1));

if (vendTable.RecId)

{

dirParty = DirParty::constructFromCommon(vendTable);

PostalAddress.Street =
conPeek(record,2);

PostalAddress.BuildingCompliment = conPeek(record,3);

PostalAddress.City = conPeek(record,4);

PostalAddress.CountryCurrencyCode = conPeek(record,5);

PostalAddress.CountryRegionId = conPeek(record,6);

countyId = conPeek(record,7);

if (Global::strStartsWith(countyId,'~'))

{

countyId =
strDel(countyId,1,1);

}

PostalAddress.County = countyId;

PostalAddress.IsPrimary =
conPeek(record,12);

PostalAddress.LocationName = conPeek(record,16);

PostalAddress.State = conPeek(record,24);

zipcode = conPeek(record,30);



if (Global::strStartsWith(zipcode,'~'))

{

zipcode =
strDel(zipcode,1,1);

}

PostalAddress.ZipCode = zipcode;

PostalAddress.ValidFrom = datetobeginUtcDateTime(
1\1\2012, DateTimeUtil::getUserPreferredTimeZone()) ;

PostalAddress.ValidTo = datetobeginUtcDateTime(1\1\2154, DateTimeUtil::getUserPreferredTimeZone()) ;

PostalAddress.Party = vendTable.Party;

 

dirParty.createOrUpdatePostalAddress(PostalAddress);

}

}

}

Create contacts for Party (Vendor or Customer) using x++ code

To create customer/vendor contacts, we can use Microsoft Dynamics Excel add-in, however, few times we need x++ code to do the job. Following is the code that reads a cvs file and then creates contacts against vendor using x++ code


static
void ContactCreate(Args _args)

{

VendTable vendTable;

DirParty dirParty;

DirPartyContactInfoView contact;

CommaTextIo file;

container record;

;

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

file.inFieldDelimiter(',');

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

{

record = file.read();

vendTable = VendTable::find(
conPeek(record,1));

if (vendTable.RecId)

{

dirParty = DirParty::constructFromCommon(vendTable);

contact.CountryRegionCode =
conPeek(record,2); // e.g "USA"

contact.LocationName = conPeek(record,7); // e.g "Main Phone" "Fax" "Email"

contact.Locator = conPeek(record,3); // e.g (xxx)-2333889

contact.Type = conPeek(record,15); // See base enum "LogisticsElectronicAddressMethodType"

contact.ValidFrom = datetobeginUtcDateTime(1\1\2012, DateTimeUtil::getUserPreferredTimeZone()) ;

contact.ValidTo = datetobeginUtcDateTime(1\1\2154, DateTimeUtil::getUserPreferredTimeZone()) ;

contact.Party = vendTable.Party;

dirParty.createOrUpdateContactInfo(contact);

}

}

}