Unleashing Dynamics 365 Excellence: Your Source for Pro Tips and Snippets at D365Snippets.com

Our blog provides a curated collection of tips, tricks, and code snippets that streamline your workflow and enhance your proficiency.

What is a Container in Dynamics 365 Finance and Operations?

0

In the previous article i explained How to create a dialog in Dynamics 365 Finance and Operations. In this article "we learn what is a Container in Dynamics 365 Finance and Operations with a nice example". As we know in X++, data types are divided into primitive data types and composite data types. Examples for primitive data types are int, str, real …. etc and examples for composite data types Arrays, Containers, Collection Classes.

What is a container in X++ ?

A container is a “composite data type”, which contains an ordered sequence of values (primitive dataTypes – int, str, real, date, boolean, enum etc.) or other containers or/and some composite data types. A container can be stored in the database as a dataBase coloumn created through AOT especially used to store images/files as a blob.

Why We Use containers in X++ ?

Primitive dataType variable can hold only one value and that too of same dataType, also it doesn’t support variables of different dataTypes. To overcome this problem X++ provides composite dataTypes arrays and containers, however arrays solve the former but supports only one dataType.

Container in Dynamics 365 Finance and Operations

Container can solves this problem by storing many elements of different dataypes. However there are few limitations with Containers like “container cannot store objects” and they can be addressed by collection classes and temporary tables concepts. Containers are most frequently used in Dynamics 365 development so let us explore Containers and its features.

Since a  container is not a class therefore classes/objects cannot be passed/put into a container. Also A container is a “PASS BY VALUE” type not “pass by reference” type which means while passing container the copy of container with values is passed not the reference. Therefore, any variable declared as a container is automatically initialized to an empty container that contains no values.

What is conPeek()?

The conPeek() is used to retrieve a specific element from a container. 

Syntax: anytype conPeek(container container, int number). 

Here container is the container to return an element from and the number is the position of the element to return. Specify 1 to get the first element. 

Return value: The element in the container at the position specified by the number parameter. The conPeek function automatically converts the peeked item into the expected return type.

How to do it...

If you are a beginner or new to dynamics 365 Finance & Operations, you can use Microsoft'f Free Virtual Machine. Learn How to Set Up a Free Virtual Machine for Dynamics 365 Development.

The below example code shows how to declare a container and how to add values to a container also the examle shows how to retreive the data from a container, Here we have declared three containers "EmpId" "EmpName" and "Salary",  where "EmpId"stores employee ids, "EmpName" stores the name of the employees and "Salary" stores employee salary. For declaring a container we use the keyword "Container" the the conatiner name.

class RunnableClass1

{

   /// <summary>

   /// Runs the class with the specified arguments.

   /// </summary>

   /// <param name ="_args">The specified arguments.</param>

   public static void main(Args _args)

   {

       container EmpId;

       container EmpName;

       container Salary ;

       int i;

       EmpId=[1000,1001,1002,1003];

       EmpName=["Rafeeque","John","Peter","Dev"];

       Salary=[50000.00,45000.00,75000.00,40000.00];

       for(i=1;i<=4;i++)

       {

           info(strFmt("EmpId = %1 -- EmpName = %2 -- Salary  = %3",conPeek(EmpId,i),

conPeek(EmpName,i),conPeek(Salary,i)));

       }

   }

}

After successful build of the project you will get an output as shown below, which lists the three containers values  on infolog.

What is Container in Dynamics 365 Finance and Operations

After successful build of the project you will get an output as shown below, which lists the three containers values  on infolog.

What is Container in Dynamics 365 Finance and Operations

How the code works ...

Here an "[] " bracket is used to assign the values to the container and values are comma separated. 

For example conatiner EmpId, which stores employee ids [1000,1001,1002,... etc] and container EmpName, which stores employee Names["Rafeeque","John",...etc]. this is the  way a container stores data.

Then how to retrieve data from containers?. The conPeek() is used to retrieve a specific element from a container. 

From the above example it is very clear that values from container EmpId is retrieved by using the code conPeek(EmpId,i), where EmpId is the name of the container and "i" is used for the position of the element to be returned. Similiar way data are retrieved from EmpName and Salary containers. You can copy paste above codes directly to your visual studio.

Happy coding with D365Snippets

Useful Tags
What are the composite data types available in X Plus Plus? 
What is X++ used for? How do you add value to a container? 
How do you declare an array in X++?
How to get value from container x++?
What is container in X++? 
What is conPeek?
How do you add value to a container?
What are the composite data types available in X Plus Plus?
What is a container in D365?
X++ container with examples

Post a Comment

0Comments
Post a Comment (0)