Tuesday, 9 November 2021

Adding a newline into a string in C# and X++

Below is the sample code we can use for adding a newline after every occurrence of "@" symbol in the string in C#

 using System;

using System.IO;

static class Program
{
    static void Main()
    {
        WriteToFile
        (
        @"C:\test.txt",
        "fkdfdsfdflkdkfk@dfsdfjk72388389@kdkfkdfkkl@jkdjkfjd@jjjk@",
        "@"
        );

        /*
        output in test.txt in windows =
        fkdfdsfdflkdkfk@
        dfsdfjk72388389@
        kdkfkdfkkl@
        jkdjkfjd@
        jjjk@ 
        */
    }

    public static void WriteToFile(string filename, string text, string newLineDelim)
    {
        bool equal = Environment.NewLine == "\r\n";

        //Environment.NewLine == \r\n = True
        Console.WriteLine("Environment.NewLine == \\r\\n = {0}", equal);

        //replace newLineDelim with newLineDelim + a new line
        //trim to get rid of any new lines chars at the end of the file
        string filetext = text.Replace(newLineDelim, newLineDelim + Environment.NewLine).Trim();

        using (StreamWriter sw = new StreamWriter(File.OpenWrite(filename)))
        {
            sw.Write(filetext);
        }
    }
}


Below is the sample code we can use for adding a newline the string in X++
textContainer is a string which I'm used in my code to add my concatinated data. 
  if(textContainer)
			textContainer += "\n"
fileData is a string variable and it will be holding my final return value
fileData += System.Text.RegularExpressions.Regex::Replace(textContainer,"\n", System.Environment::NewLine);


Zero Padding Numbers in D365 F&O x++

 Sometimes we need to add padding numbers in the files in AX. Ex: Postive pay files

I found the below post that helped to add the padding zero numbers in the positive pay file.

Original post: http://www.atomicax.com/content/zero-padding-numbers


AX provides us with an easy way to pad strings with extra characters. A common situation where this is required is use a "fake" number sequence inside AX.
If you wish to increment a counter, but store or display it as a string such as "ABC-00001", "ABC-00002" etc then padding the counter with zeros is required.

We can use the strRFix() and strLFix() methods to achieve this.

static void testZeroPadding(Args _args)
{
    int i = 1;
    str padded;
    str finalResult;
    ;
    // Create a string, with a length of 5, ending with the value of i and padded with zeros
    padded = strRFix(int2str(i), 5, "0");
    finalResult = strFmt("ABC-%1", padded);

}

This will populate finalResult with ABC-00001. If we use strLFix instead of strRFix, it will pad to the right with zeros, giving us ABC-10000.

Alternatively, you could use a function like this: (However, the approach above is most likely faster)

static str zeroPad(str item, int numZero)
{
    int i;
    str out;
    ;
    for(i=0;i<numZero-strlen(item); i++)
    {
        out += "0";
    }

    return(out + item);
}

You are not authorized to login with your current credentials. You will be redirected to the login page in a few seconds D365 FO Visual studio

 I was getting the below error while using VS 2015/17 to debug x++ code


You are not authorized to login with your current credentials. You will be redirected to the login page in a few seconds




Reason: Usually, this happens when multi factor authentication login is enabled and your VS session got expired.


Solution: Make sure your account is login by reentering the login credentials as shown in below screenshot(arrow mark in green color).










Unable to read beyond the end of the stream D365 F&O

 exceptionMessage An error occurred when running report TestReport.Report. Message: Unable to read beyond the end of the stream.

exceptionType System.IO.EndOfStreamException
stackTrace System.IO.EndOfStreamException: Unable to read beyond the end of the stream. at System.IO.__Error.EndOfFile() at System.IO.BinaryReader.ReadString() at Microsoft.Dynamics.AX.Metadata.Storage.RuntimeDeserializer.DeserializeAxReportPrecisionDesign(BinaryReader reader) at Microsoft.Dynamics.AX.Metadata.Storage.RuntimeDeserializer.DeserializeAxReport(BinaryReader reader) 


Solution: To fix following issue you can simply build the model you are working on.

Display individual financial dimension value in grid control in D365 FO x++

 To display inventory dimensions in the grid we have control of dimension display which can be set up as per the requirement. 

But there is no control in D365FO to display financial dimensions in grid control. 
There are 2 ways it can be done. 

1. Create display methods to display particular financial dimensions like the below method which displays division. I have seen many examples on the internet using multiple find methods but I prefer it by doing one query with joins to improve performance issues.

public display DimensionValue getDimensionDivision()
{
        DimensionAttributeValueSetItem  dimensionAttributeValueSetItem;
        DimensionAttributeValue              dimensionAttributeValue;
        DimensionAttribute                       dimensionAttribute;
     
        select DisplayValue from dimensionAttributeValueSetItem
            join dimensionAttributeValue
            join dimensionAttribute
            where dimensionAttributeValueSetItem.DimensionAttributeValue    == dimensionAttributeValue.RecId 
                 && dimensionAttributeValue.DimensionAttribute                == dimensionAttribute.RecId
                 && dimensionAttribute.Name                                                == 'Division'
                 && dimensionAttributeValueSetItem.DimensionAttributeValueSet == this.DefaultDimension;;

       return dimensionAttributeValueSetItem.DisplayValue;
}

You can replace Division with whatever financial dimension you want to display.

2. The problem with the display method even with query is performance is never good so to solve that use views to display values using SQL rather than X++.

Create a view in AOT as below:
DimensionViewForDivision is the first view created which can be used across D365FO for any master table. The second view is where I connect DimensionViewForDivision with the Customer table.
Change the value in range for Name with FD you want.


The view can be used on a form, query or another view to display data in UI.



Adding a newline into a string in C# and X++

Below is the sample code we can use for  adding a newline after every occurrence of "@" symbol in the string in C#   using System...