Thứ Ba, 30 tháng 8, 2016

How can I get and show the fields that are part of a table relation?

I assume you are trying to see the fields that make up the specified relation.
In order to do this, you use the DictRelation and DictField classes. Create a new DictRelation based on the table, and then use loadTableRelation() to specify the related table. Create new DictFields and use the DictRelation.lineExternTableValue() method to specify the specific fields:
DictRelation dr;
DictField   Field; 
DictField   RelatedField;
int         RelationIndex = 1;

dr = new DictRelation(tableNum(InventDim));
dr.loadTableRelation(tableNum(EcoResColor));
info(strFmt("%1", tableId2name(dr.externTable())));

Field = new DictField(tableNum(InventDim), dr.lineTableValue(RelationIndex));
RelatedField = new DictField(tableNum(EcoResColor), dr.lineExternTableValue(RelationIndex));

info(strFmt("%1 related to %2", Field.name(), RelatedField.name()));

Find related field in Dynamics AX

The following job helps you find the related field of a table, in the e,g it tries to find the related field of (InventTrans, InventTransId) for the sales Line table

static void FindRelateField(Args _args)
{
    SysDictTable    dictTable = new SysDictTable(tableNum(SalesTable));
    int             i,j;
    SysDictRelation dictRelation;
    TableId         externId = tableNum(PurchTable);
    IndexId         indexId;

    //Searh the explicit relations
    for(i=1; i <= dictTable.relationCnt(); i++)
    {
        dictRelation = new SysDictRelation(dictTable.id());
        dictRelation.loadNameRelation(dictTable.relation(i));

        // If it is a 'relation' then you use externTable(), but for extended data types you use table() (see next blook)
        if(SysDictRelation::externId(dictRelation) == externId)
        {
            for(j=1; j <= dictRelation.lines(); j++)
            {
                info(strFmt("%1", dictRelation.lineExternTableValue(j)));
                info(fieldId2name(dictRelation.externTable(), dictRelation.lineExternTableValue(j)));
            }
        }
    }

    info(strFmt("%1", dictRelation.loadFieldRelation(fieldNum(SalesLine,InventTransId))));
}

Thứ Ba, 23 tháng 8, 2016

Connect from one one AOS to another AOS via X++ and execute commands

I had a requirement to connect from one Axapta instance to another instance staying in MORPHX IDE. I have done this lot many times using Visual Studio in my past, but never tried with MORPHX IDE.

So, here are the steps:

Step1

Go to AOT > References > Add Reference to Microsoft.Dynamics.BusinessConnectorNet.dll (this file is usually located in Client folder, default path if you choose C it would be C:\Program Files\Microsoft Dynamics AX\50\Client\Bin)


Step 2

This piece of code shows how to connect to a remote AOS (Application), Here I'm just executing a class and retreiving the customer name. The LogonAs method used below is used to connect via the business connector proxy account as shown below. Otherwise the code is pretty straight forward



Troubleshooting/Error

I ran into this error (Screenshot shown below), Later I realised that on the remote AOS where I was connecting to the business proxy information was not set in Administration > Security > System Accounts form.

Thứ Hai, 22 tháng 8, 2016

C# Interfaces, what are they and why use them?

What is an Interface
First and foremost, interfaces in C# are a means to get around the lack of multiple inheritance in C#, meaning you cannot inherit from multiple classes but you can implement multiple interfaces. OOP tries to resemble how objects are defined in the real life, and interfaces are a very logical way of grouping objects in terms of behavior.
An interface is a contract between itself and any class that implements it. This contract states that any class that implements the interface will implement the interface's properties, methods and/or events. An interface contains no implementation, only the signatures of the functionality the interface provides. An interface can contain signatures of methods, properties, indexers & events.
You can think of an interface as an abstract class with the implementation stripped out. An interface doesn't actually do anything, like a class or abstract class, it merely defines what a class that implements it will do. An interface can also inherit/implement other interfaces.
Why use interfaces
So if an interface implements no functionality then why should we use them? Using interface based design concept provides loose coupling, component-based programming, easier maintainability, makes your code base more scalable and makes code reuse much more accessible because implementation is separated from the interface. Interfaces add a plug and play like architecture into your applications. Interfaces help define a contract (agreement or blueprint, however you chose to define it), between your application and other objects. This indicates what sort of methods, properties and events are exposed by an object.
For example let's take a vehicle. All vehicles have similar items, but are different enough that we could design an interface that holds all the common items of a vehicle. Some vehicles have 2 wheels, some have 4 wheels and can even have 1 wheel, though these are differences they have something in common, they're all movable, they all have some sort of engine, they all have doors, but each of these items may vary. So we can create an interface of a vehicle that has these properties, then we inherit from that interface to implement it.
While wheels, doors and engines are different they all rely on the same interface (I sure hope this is making sense). Interfaces allow us to create nice layouts for what a class is going to implement. Because of the guarantee the interface gives us, when many components use the same interface it allows us to easily interchange one component for another which is using the same interface. Dynamic programs begin to form easily from this. An interface is a contract that defines the signature of some piece of functionality.
So here's a simple example of an interface and implementing it. From the above example we're created a IVehicle interface that looks like this
namespace InterfaceExample
{    
    public interface IVehicle    
    {        
          int Doors { get; set; } 
          int Wheels { get; set; }
          Color VehicleColor { get; set; }
          int TopSpeed { get; set; }
          int Cylinders { get; set; } 
          int CurrentSpeed { get; } 
          string DisplayTopSpeed();
         void Accelerate(int step);    
    }
}
Now we have our vehicle blueprint, and all classes that implement it must implement the items in our interface, whether it be a motorcycle, car, or truck class we know that all will contain the same functionality. Now for a sample implementation, in this example we'll create a motorcycle class that implements our IVehicle class. This class will contains everything we have defined in our interface

namespace InterfaceExample
{    
     public class Motorcycle : IVehicle    
     {        
              private int _currentSpeed = 0;        
              public int Doors { get; set; }        
              public int Wheels { get; set; }        
              public Color VehicleColor { get; set; }        
              public int TopSpeed { get; set; }        
              public int HorsePower { get; set; }        
              public int Cylinders { get; set; } 
       
              public int CurrentSpeed        
              {            
                 get { return _currentSpeed; }        
              }


              public Motorcycle(int doors, int wheels, Color color, int topSpeed, 
                int horsePower, int cylinders, int currentSpeed)
              {
                  this.Doors = doors;            
                  this.Wheels = wheels;            
                  this.VehicleColor = color;            
                  this.TopSpeed = topSpeed;            
                  this.HorsePower = horsePower;            
                  this.Cylinders = cylinders;            
                  this._currentSpeed = currentSpeed;        
              }     
   
              public string DisplayTopSpeed()        
              {
                  return "Top speed is: " + this.TopSpeed;        
              }        

              public void Accelerate(int step)
              {            
                   this._currentSpeed += step;        
              }    
       }
}
Now in the same application we could interchange our Motorcycle class with a Truck class or a Car class and they will all have the same base functionality, that of a IVehicle.
So as you can see interface based development can make a developers life much easier, and our applications much cleaner, maintainable and extensible.

SQL SERVER – Find Stored Procedure Related to Table in Database – Search in All Stored Procedure

Following code will help to find all the Stored Procedures (SP) which are related to one or more specific tables. sp_help and sp_depends does not always return accurate results.
----Option 1
SELECT DISTINCT so.nameFROM syscomments scINNER JOIN sysobjects so ONsc.id=so.id
WHERE sc.TEXT LIKE '%tablename%'
----Option 2
SELECT DISTINCT o.nameo.xtypeFROM syscomments cINNER JOIN sysobjects o ON c.id=o.id
WHERE c.TEXT LIKE '%tablename%'