Wednesday, December 3, 2008

Creating a new Axapta 4.0 document in X++ code.

To satisfy the reporting style of a legacy system, I needed my new Dynamics AX 4.0 system to create a new document note that contains the customer's item code. Yes, Dynamics AX 4.0 does have External Item descriptions and they are populated with the values I want to display but to avoid rebuilding the layout of reports I decided to use external document notes to display these values for the customer's benefit on their sales documentation. The external notes are functioning properly and appearing beneath the respective sales lines in reports.

Here is the method I found to create a new DocuRef:
public static void create(recId docuRefRecId, tableId refTableId, recId refRecId)

I found some help in MSDN, specifically a mapping of the the 3 arguments needed for the create method in relation to the SalesTable:
DocuRef.RefCompanyId==SalesTable.dataAreaId
DocuRef.RefTableId==SalesTable.TableId
DocuRef.RefRecId==SalesTable.RecId
However, I am doing this for the SalesQuotationLine table. My first instinct was to try and use the create method to do this. Yet, experience kicks in and you realize that declaring a variable of type DocuRef and setting the values of the RefTableID and RefRecID then using the insert() method of the new variable is the best course of action. I attached the following code to the modified method of SalesQuotationLine_DS.ItemID.

DocuRef tblDocuRef;
;
super();
ttsbegin;
tblDocuref.RefTableId = salesquotationline.TableId;
tbldocuref.RefRecId = salesquotationline.RecId;
tbldocuref.RefCompanyId = salesquotationline.dataAreaId;
tbldocuref.TypeId = "Notes";
tbldocuref.Restriction = DocuRestriction::External;
tblDocuref.Name = "CUSTOMER ITEM CODE";
tblDocuref.Notes = "CUST ITEM CODE: 00000034343";
tbldocuref.insert();
ttscommit;

The code worked, but since the new record had not been committed to the database yet, my RefRecID had a value of 0. My solution then turned into a post update or in other words writing the DocuRef record after the SalesQuotationLine has been committed to the database. I placed my code in the write method of the SalesQuotationLine datasource right after the salesQuotationLine and SalesQuotationTable reinitialization calls (reread(), refresh()) and removed it from the modified method. The following is the code that works flawlessly for me:

//[rg] 12/3/2008 first make sure the item has an external item id.
if (CustVendExternalItem::find(ModuleInventPurchSalesVendCustGroup::Cust,salesQuotationline.ItemId,"AllBlank",SalesquotationTable.CustAccount).ExternalItemId)
{ //[rg] 12/3/2008 now make sure we did not already write this record.
if (DocuRef::find(salesquotationline.dataAreaId,salesquotationline.TableId,salesquotationline.RecId,salesquotationline.createdDate).Name)
{
tbldocuref.RecId = 3; //[rg] remove this code.
}
else
{
ttsbegin;
tblDocuref.RefTableId = salesquotationline.TableId;
tbldocuref.RefRecId = salesquotationline.RecId;
tbldocuref.RefCompanyId = salesquotationline.dataAreaId;
tbldocuref.TypeId = "Note";
tbldocuref.Restriction = DocuRestriction::External;
tblDocuref.Name = "CUSTOMER ITEM CODE";
tblDocuref.Notes = "CUSTOMER ITEM CODE: " + CustVendExternalItem::find(ModuleInventPurchSalesVendCustGroup::Cust,salesQuotationline.ItemId,"AllBlank",SalesquotationTable.CustAccount).ExternalItemId;
tbldocuref.insert();
ttscommit;
}
}

As you can see I am pulling my note from the CustVendExternalItem.ExternalItemID field. I am happy with the results because I can now see the external note clearly on my Sales Confirmation report. I suggest using the Docu::Copy method to transfer these notes, upon confirmation of quote, to the sales order.

Tuesday, December 2, 2008

How to transfer Sales Quotation Document to Sales Orders upon confirmation in AX 4.0

If you need to move Sales Quotation Document reference notes automatically upon confirmation in AX 4.0 to your new Sales Order follow these steps:

1) Open the class: SalesQuotationEditLinesForm_Sales_Confir
2) In method: createSalesLines paste this code above second to last }

Docu::copy(SalesQuotationLineUpdate,SalesLine);

3) In method: createSalesTable past this code above second to last }

Docu::copy(SalesQuotationTable,SalesTable);