Microsoft Dynamics AX 2012:Create an XML from user Input in a Dialog

Agenda:

“Based on User input , XML file is created”

So Basically we have an Ax dialog having three sections:

1jpg

List of AOT elements group  =AOT name (“ElementsInAOT”)

3

Selection Criteria group = AOT name (“Criteria”)

4

File field  = AOT name (“FileSelect”),see below screens

5

Also note that , three of the objects have the “AUTODECLARATION” property is set to “Yes”

Note:AUTODECLARATION set to ‘Yes’ enable the user to use that object in the code editior other wise it wont be available on the editor.

So Now user select the input options and Press OK button

2

Now,I have created a method called “CreateXml” which will be doing following things:

1.It will traverse through “ElementInAOT” group and find those objects which are selected and store them

2.It will traverse through “Criteria” group and find those objects which are selected and store them

3.It will check the FileSelect field whether it has a value or not and store it

4.Create an xml with the name “Criteria.xml” in C://

So here is the code:

private XmlDocument createXml()
{
int i,j;
FormStringControl stringControl;
FormCheckBoxControl ctrl;
FormControl formControl;
XmlDocument xmlDoc;
XmlElement xmlRoot;
XmlElement xmlField;
XmlElement xmlRecord;
XMLWriter xmlWriter;

xmlDoc = XmlDocument::newBlank();
xmlRoot = xmlDoc.createElement(#Criteria);
xmlRecord = xmlDoc.createElement(#FilePath);
xmlField = xmlDoc.createElement(#FileName);
xmlField.innerText(FileSelect.text());
xmlRecord.appendChild(xmlField);
xmlRoot.appendChild(xmlRecord);
xmlDoc.appendChild(xmlRoot);

xmlRecord = xmlDoc.createElement(#Inclusion);

//TRAVERSE THROUGH Criteria group

//see this  “Criteria” control is availbe because we set its AutoDeclaration property to “Yes”

for (j = 1;j <= Criteria.controlCount() ;j++)
{
formControl = Criteria.controlNum(j);
if(formControl IS FormStringControl)
{
stringControl = formControl;
if(stringControl.valueStr())
{
xmlField = xmlDoc.createElement(stringControl.name());
xmlField.innerText(stringControl.valuestr());
xmlRecord.appendChild(xmlField);
xmlRoot.appendChild(xmlRecord);
xmlDoc.appendChild(xmlRoot);
}
}
if(formControl IS FormCheckBoxControl)
{
ctrl = formControl;
if(ctrl.value())
{
xmlField = xmlDoc.createElement(ctrl.name());
xmlField.innerText(ctrl.name());
xmlRecord.appendChild(xmlField);
xmlRoot.appendChild(xmlRecord);
xmlDoc.appendChild(xmlRoot);
}
}
}
xmlRecord = xmlDoc.createElement(#Filter);

//TRAVERSE THROUGH Elemenst in AOT group

for (i = 1; i <= ElementInAOT.controlCount() ;i++)
{
ctrl = ElementInAOT.controlNum(i);
if(ctrl.value() != 0)
{
xmlField = xmlDoc.createElement(ctrl.name());
xmlField.innerText(this.findPath(ctrl.labelText()));
xmlRecord.appendChild(xmlField);
xmlRoot.appendChild(xmlRecord);
xmlDoc.appendChild(xmlRoot);
}
}

xmlWriter = XMLWriter::newFile(@”c:\Criteria.xml”);
xmlDoc.writeTo(xmlWriter);

return xmlDoc;

}

Here is the output file:

6

Please let me know if you have any queries!

Thanks

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s