Dynamics AX
  RSS Feed  LinkedIn  Twitter
Want to turn you're data into a true asset? Ready to break free from the report factory?
Ready to gain true insights that are action focused for truly data informed decisions?
Want to do all of this across mutliple companies, instances of Dynamics and your other investments?
Hillstar Business Intelligence is the answer then! (www.HillstarBI.com)

Hillstar Business Intelligence for Microsoft Dynamics AX and NAV on Mobile, Desktop, Tablet


Let us prove to you how we can take the complexity out of the schema and truly enable users to answer the needed questions to run your business! Visit Hillstar Business Solutions at: www.HillstarBI.com

Wednesday, June 20, 2007

.Net BC, C# - Working with AxaptaRecord Object

In moving forward with my post about working with the .Net BC and C# below is some sample code of working with an AxaptaRecord object. With this object you can call upon a DataBuffer or better known as a table variable and perform X++ SQL Statements to fill the given object with data to work with. This means that all logic, checking, validation, and methods are exposed via this object that may exist for the given DataBuffer, ie: AxaptaRecord object. Let's take a look at the code:


AxaptaRecord axRecord;
String FieldValue1;
String query;

objDAXCn = (Axapta)Session["objDAXCn"];

if (objDAXCn == null)
return "";

try
{
axRecord = objDAXCn.CreateAxaptaRecord("SomeTable");
query = "select FieldValue1 from %1 where %1.SomeKeyValue == '" + _PassedInVariable + "'";
axRecord.ExecuteStmt(query);

if (axRecord.Found)
FieldValue1 = (string)axRecord.get_Field("FieldValue1");
elseM
FieldValue1 = "";

return FieldValue1;
}
catch
{
// Do something here
}

Alright so with the above block of code what are we doing?
First we are gettting the Axapta object for our connection from a session variable. We then perform a check to see if the given object is null. If so we simply return a blank value. Here though one could re-establish the connection, report back an error, etc. The next peice to this is we call the Axapta Object.CreateAxaptaRecord() call and set our AxaptaRecord object equal to that. Notice this is done Before we call for the query to be used to fill the object with data. Next we set our Query variable equal to the select statement of choice.
Notice this is X++ SQL Statements, and notice that the %1 is used. This is because the name of the table is passed as the create of the object, and the %1 refers to said name, because the Query that is passed in is formatted internally with a strfmt() function. The next thing if we is check if a Record is found, and if so set our variable to that field value, otherwise blank it out. Very simple, but very powerful.
Here we could have created advanced DataSets that where filled, and perform for or while blocks of code used to loop through all the records found, etc. Anyway this code is very useful, and makes it possible to make use of the Business logic that may be contained at the Table object level from within the AOT. Again you can get really advanced with this, but this should give you an idea of how to work with the AxaptaRecord object.

Well please leave comments, and check back as I continue down this path in exploring with you different ways to interact with objects via the .Net BC from C#!


Find a job at: www.DynamicsAXJobs.com

Labels: , , , , , ,

2 Comments:

Blogger Unknown said...

Hi
i would like to know how can i insert data into the tables because when i try to insert the data into the table the recId is automatically being generated .
So, How can i take care of this while i insert the data.

9:07 AM  
Anonymous Anonymous said...

Hi,

I would like to know how can I populate a combo box on form load using Business Connector with data from Ax ?

10:10 AM  

Post a Comment

<< Home


Copyright 2005-2011, J. Brandon George - All rights Reserved