Wednesday, January 20, 2021

SSRS Make Enum Multi Select AX 2012

In this tutorial we see how to make Enum Multi Select in ax 2012 and filter values in another lookup based on Enum values. 

Make new table and add fields in it  EnumId and EnumLabel. write the method in the UI builder class you can see the method below.

public void ContactStatusLookup(FormStringControl _control)

{

    Query                       query = new query();

    QueryBuildDataSource        qbds;

    BEContractStatusEnum        enumTmp; //Table name

    SysDictEnum                 dictEnum = new SysDictEnum(enumNum(BEContractStatus));

    int                         idx;


    delete_from enumTmp;


    for (idx = 0;idx < dictEnum.values();idx++)

    {

       enumTmp.clear();

       enumTmp.initValue();

       enumTmp.EnumId       = dictEnum.index2Value(idx);

       enumTmp.EnumLabel    = dictEnum.index2Label(idx);

       enumTmp.insert();

    }



    qbds = query.addDataSource(tableNum(BEContractStatusEnum));


    qbds.fields().dynamic(false);

    qbds.fields().clearFieldList();


    qbds.fields().addField(fieldNum(BEContractStatusEnum, EnumLabel));

    //SysLookupMultiSelectGrid::lookup(query,_control,_control,conNull());

    multiContractStatus = SysLookupMultiSelectGrid::construct(_control, _control);

    multiContractStatus.parmQuery(query);

    multiContractStatus.run();


}

Register this method in post build.

Now we filter values in another lookup see the below code how we filter values based on selected values in enum lookup.

public void ContactIDLookup(FormStringControl _control)

{

    str                             controlValue;

    Query                           query = new query();

    QueryBuildDataSource            qbds;

    List                            strlist=new List(Types::String);

    ListIterator                    iterator;

    SysDictEnum                     dictEnum = new SysDictEnum( enumnum(BEContractStatus) );


    qbds = query.addDataSource(tableNum(BEContractDetails));

    controlValue = contractStatus.value();

    qbds.fields().dynamic(false);

    qbds.fields().clearFieldList();

    if(dictEnum.name2Value(contractStatus.value()) !=0 && controlValue!="")

    {

        strlist     =   strSplit(controlValue,";");

        iterator    = new ListIterator(strlist);


        while(iterator.more())

        {

            //info(strFmt("%1 - %2 - %3",iterator.value(), , dictEnum.index2Symbol(dictEnum.name2Value(contractStatus.value()))));

            qbds.addRange(fieldNum(BEContractDetails, EndUseragreementStatus)).value(SysQuery::value(dictEnum.index2Symbol(dictEnum.name2Value(iterator.value()))));

            qbds.fields().addField(fieldNum(BEContractDetails, ContractId ));

            qbds.fields().addField(fieldNum(BEContractDetails, ClientName ));

            qbds.fields().addField(fieldNum(BEContractDetails, PropertyId ));

            iterator.next();

        }

    }

    else

    {

        qbds.fields().addField(fieldNum(BEContractDetails, ContractId ));

        qbds.fields().addField(fieldNum(BEContractDetails, ClientName ));

        qbds.fields().addField(fieldNum(BEContractDetails, PropertyId ));

    }

    //SysLookupMultiSelectGrid::lookup(query,_control,_control,conNull());

    multiContractId = SysLookupMultiSelectGrid::construct(_control, _control);

    multiContractId.parmQuery(query);

    multiContractId.run();

}

Wednesday, December 30, 2020

Test custom page type api on postman

Today we see how to test the custom page type API on postman.

Step 1: Open postman set the request type GET, paste API url, set authorization type Basic Auth, add user name and password.













Step 2: search the users and open the form











Step 3: copy the username and web service access key and paste it is Postman Authorization.














Step 4: Now click on send button you can see result.





How to create Custom page type API Business central

Today we see how to make custom API in business central.

I ma making API for Item data.


Step 1: 

add new file in project and set the any name.al Write the below code in the file.

page 50103 Products
{
    PageType = Card;
    SourceTable = Item;

    layout
    {
        area(Content)
        {
            group(GroupName)
            {
                field("No."; Rec."No."{ }

                field(Description; Rec.Description{ }
                field(Blocked; Rec.Blocked{ }
                field(Type; Rec.Type) { }
                field("Base Unit of Measure"; Rec."Base Unit of Measure"{ }
                field("Last Date Modified"; Rec."Last Date Modified"{ }
            }
        }
    }
}


Step 2: Add new file in the project and set the name.xml. In file write twebservices and click enter you will automatically get the structure.

<?xml version="1.0" encoding="UTF-8"?>
<ExportedData>
    <TenantWebServiceCollection>
        <TenantWebService>
            <ObjectType>Page</ObjectType>
            <ServiceName>Products</ServiceName>
            <ObjectID>50103</ObjectID>
            <Published>true</Published>
        </TenantWebService>
    </TenantWebServiceCollection> 
</ExportedData>


Step 3: Now run the project it automatically publish your api.

Step 4: Search web service in business central search bar and click on it.



Step 5: You can see the webservice in the below image.



In the next post we will look at how we use our API on postman.


Monday, September 14, 2020

X++ Call rest Api from AX 2012

Now we see how to Authenticate the REST API from AX 2012

  System.Net.WebRequest webreq = System.Net.WebRequest::Create("URL");

    System.IO.Stream streamstr,responsestr;

    System.IO.StreamWriter streamWriter;

    System.Net.WebResponse webresponse;

    System.IO.StreamReader reader;

    System.Exception ex;

   


    str json = "{\"clientId\":\"Value\",\"clientSecret\":\"Value\"}";


   webreq.set_Method("POST");

   webreq.set_ContentType("application/json");

   streamstr = webreq.GetRequestStream();

   streamWriter = new System.IO.StreamWriter(streamstr);

   streamWriter.Write(json);

   streamWriter.Flush();

   streamWriter.Close();

   streamWriter.Dispose();


    try

    {

       webresponse = webreq.GetResponse();

       responsestr = webresponse.GetResponseStream();

       reader = new System.IO.StreamReader(responsestr);

       info(reader.ReadToEnd());

    }

    catch(Exception::CLRError)

    {

        ex = ClrInterop::getLastException();

        if (ex != null)

        {

            ex = ex.get_InnerException();

            if (ex != null)

            {

                error(ex.ToString());

            }

        }

    }

Thursday, August 20, 2020

Create Sales order Using X++ Ax 2012

 public void CreateSalesOrder()

{

    CustTable                   custtable;

    SalesTable                  salesTable;

    NumberSeq                   NumberSeq;

    SalesId                     sid;

    SalesLine                   sl;

   

// sales order header part

        NumberSeq = NumberSeq::newGetNum(SalesParameters::numRefSalesId() , true);

        sid = NumberSeq.num();

        salesTable.SalesId                  = sid;

        salesTable.initValue();

        salesTable.CustAccount              = custtable.AccountNum;

        salesTable.initFromCustTable();

        salesTable.ShippingDateRequested    = systemDateGet();

        salesTable.ShippingDateConfirmed    = systemDateGet();

        salesTable.ReceiptDateConfirmed     = systemDateGet();

        salesTable.ReceiptDateRequested     = systemDateGet();

        ttsBegin;

            salesTable.insert();

        ttsCommit;



// Sales order line part        

    sl.clear();

            sl.SalesId      = salesTable.SalesId;

            sl.ItemId       = //ItemID;

            sl.initFromInventTable(InventTable::find(sl.ItemId));

            sl.SalesQty     = //QTY;

            sl.SalesPrice   = InventTableModule::find(sl.ItemId,ModuleInventPurchSales::Sales).Price;

            sl.LineAmount   = sl.SalesQty*sl.SalesPrice;

            sl.createLine(NoYes::Yes, // Validate

                NoYes::Yes, // initFromSalesTable

                NoYes::No, // initFromInventTable

                NoYes::Yes, // calcInventQty

                NoYes::Yes, // searchMarkup

                NoYes::No); // searchPrice

   

        }

     

}

Monday, April 13, 2020

C# how to make PDF file password protected

how to make PDF file password protected in C# using itext sharp

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp.text;
using System.IO;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            String FilePath = @"C:\Users\rashid\Desktop\Customer.pdf";
            string PDFFilepassword = "12345";
            try
            {
                byte[] bytes = System.IO.File.ReadAllBytes(FilePath);
                using (MemoryStream inputData = new MemoryStream(bytes))
                {
                    using (MemoryStream outputData = new MemoryStream())
                    {
                   
                        PdfReader reader = new PdfReader(inputData);
                        PdfReader.unethicalreading = true;
                        PdfEncryptor.Encrypt(reader, outputData, true, PDFFilepassword, PDFFilepassword, PdfWriter.ALLOW_SCREENREADERS);
                        bytes = outputData.ToArray();
                        File.WriteAllBytes(FilePath , bytes);
                     
                     
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

Tuesday, April 7, 2020

batch job ax 2012 using x++

Basic structure of Batch Job in AX 2012 using X++

class BEProjectEmailAutomationBatch extends RunBaseBatch
{
}
public void run()
{
    //write login in this method
}
public container pack()
{
    return conNull();
}
public boolean unpack(container packedclass)
{
    return true;
}
public static ClassDescription description()
{
    return "Project Report Email Automation";
}
public static void main(Args _args)
{
    BEProjectEmailAutomationBatch EmailAutomation = new BEProjectEmailAutomationBatch();

    if (EmailAutomation.prompt())
    {
        EmailAutomation.run();
    }
}