Common x ++ examples

Source: Internet
Author: User

Simple SELECT statement example
Static void selectexample (ARGs _ ARGs)
{
Custtable;
Int counter;
;
Select * From custtable;
While (custtable. accountnum)
{
Print custtable. Name + "" + custtable. accountnum;
Next custtable;
Counter ++;
}
Print "the no. of customer records are:" + int2str (Counter );
Pause;
}

Difference between the while and while select
Small eg:
While select custtable
{
Print custtable. accountname;
} // Automatically loops through records until the last record.

Select custtable;
While (custtable)
{
Print custtable. accountname;
Next custtable; // without this statement (next) the looping becomes infinite
}

How to pop up a new form through code...ARGs;
Formrun;
;

ARGs = new ARGs ();
Args. Name (formstr (your_form_name ));
Args. Caller ();

Formrun = classfactory. formrunclass (ARGs );
Formrun. INIT ();
Formrun. Run ();
Formrun. Wait ();

Execute query code with Filter
1) This. Query (). datasourcetable (tablenum (custtable). addrange (fieldnum (custtable, accountnum). Value (ledgerjournaltrans. accountnum );

Or else u can use
2) The SELECT statement with the WHERE clause. Make sure to override the super ();

Create a dialog radiobutton...
First create a new base Enum with the property selected as radiobutton.

Dialog dialog = new dialog ();
Dialogfield DF;
Formradiocontrol FRC;
;
DF = dialog. addfield (typeid (baseenum1 ));
FRC = DF. Control ();
FRC. Caption ("whatever the label shocould be ");
FRC. helptext ("whatever the help message shocould be ");
Dialog. Run ();

Look up on the form datasource...
Public void Lookup (formcontrol _ formcontrol, STR filterstr)
{
Table1 Table1;
Export ablelookup lookup = export ablelookup: newparameters (tablenum (Table1), _ formcontrol );
Query query = new query ();
Querybuilddatasource;
Querybuildrange;
;
Lookup. addlookupfield (fieldnum (Table1, fieldname ));
Querybuilddatasource = query. adddatasource (tablenum (Table1 ));
Querybuilddatasource. addrange (fieldnum (Table1, fieldname). Value (table2.fieldname1 );
Lookup. parmquery (query); // query to be executed
Lookup. Define mformlookup ();
}

This job aid concentrates on string manipulations.
1) retrieve a substr...
Static void job13 (ARGs _ ARGs)
{
STR a, B, c, d;
Str s = 'AAAAAA, bbbbb, cccccccccc, dd ';
;
[A, B, c, d] = Global: str2con (S ,',');
Print;
Print B;
Print C;
Print D;
Pause;
}

2) to replace a filename with theaxapta standard filename use the following...
Strreplace (filepath ,'//','/');

3)
Textbuffer = new textbuffer ();
STR text "XXXX, xxxxxx, xxxx, XXXX ";
Int lines, I;
;

TEXT = strreplace (text, "/R/N ");
Textbuffer. settext (text );
Lines = textbuffer. numlines ();

For (I = 0; I <= lines; I ++)
{
Box: Info (strline (text, I ));
}

Some light on arrays and calling a AOT item...
Static void arrayinfo (ARGs _ ARGs)
{
Str l;
Array I = new array (types: integer );;
Startlengthyoperation (); // For that hour glass symbol to start
// I. Value (0, 0); // There's no pointer to 0
I. Value (1, 12 );
I. Value (2, 13 );

Info (I. tostring (); // I am displaying the contents of the array
L = infolog. Text (); // having a string and getting the value in the infolog, currently the array value is present
Sleep (2000); // simply making the system idle for 2 seconds
Info (L );
Endlengthyoperation (true); // hour glass symbol ends and becomes a mouse pointer again

}

// Calling a job or any AOT item from another method in a class in ax
Static void calljob (ARGs _ ARGs)
{

Treenode;
;
Treenode = treenode: findnode ("Jobs // arrayinfo ");
Treenode. aotrun ();

Treenode = treenode: findnode ("forms // testcontrols ");
Treenode. aotrun ();
}

Upload an image in a form window control...
Public void clicked () // On the Click Event of a button we need to upload an image on the form in a window control.
{
STR filename;
Filenamefilter filter = ['jpg files', '*. jpg'];
Bindata;
Image signatureimage = new image ();

Super ();

Filename = winapi: getopenfilename (element. hwnd (), filter, ''," upload your image ",'','');

Bindata = new bindata ();

If (bindata. LoadFile (filename ))
{
Signatureimage = bindata. getdata ();
Formwindowcontrol. Image (signatureimage );
Formwindowcontrol. widthvalue (signatureimage. Width ());
Formwindowcontrol. heightvalue (signatureimage. Height ());
Element. resetsize ();
Element. Unlock ();
}

Generates the index for the base enums
Static void baseenumid (ARGs _ ARGs)
{
Dictenum;
Int I;
;
Dictenum = new dictenum (enumname2id ('numberseqmodule '));

For (I = 0; I {
Print int2str (I) + ',' + dictenum. index2name (I );
Pause;
}
}

Exporting the data to Doc Format
Static void exporttofile (ARGs _ ARGs)
{
Container con;
Sqldictionary;
Tablename tabname;
Commaio myfile;
Custtrans;

;

Tabname = "custtrans ";

Select * From sqldictionary where tablename2id (tabname) = sqldictionary. Tabid;

If (sqldictionary. recid> 0)
{
Myfile = new commaio ("C: // exportedfile.doc", "W ");
Myfile. outfielddelimiter ("/t"); // leaves space between each column
// Myfile. outrecorddelimiter ("/R/N ");
While select custtrans
{
Con = [custtrans. accountnum, custtrans. txt];
Myfile. writeexp (CON );

}

}
}

Post a journal
// Posting a ledgerjournal automatically
Ledgerjournalcheckpost ljcp;
Ledgerjournaltable ljt;
;

// Have To Find A ledgerjournaltable, and assign it to ljt

Lcjp = ledgerjournalcheckpost: newledgerjournaltable (ljt, Noyes: Yes );
Ljcp. Run ();

LETZ see some treenode functionality
// This Job uses the treenode functionality to the fullest to find a child in a report with a given name... check it out
// Copy the lines to ax and Tab them so that the code looks better to understand...

Static void aotgetreportfinal (ARGs _ ARGs)
{
# Define. menu ('menus ')
# Define. Reports ('report ')

Treenode, child, ittree;
Treenodeiterator iterator;

STR reportname = 'your _ report_label_name '; // report label name as it appears in the menus> (module)> reports in the AOT

Void checkreport (treenode _ subnode)
{
STR path;

Path = _ subnode. treenodepath ();
_ Subnode = _ subnode. aotfindchild (reportname );
If (_ subnode)
{
Info ('found-'+ _ subnode. treenodepath ());
Return;
}
Else if (infolog. findnode (PATH). aotfirstchild ())
{
Checkreport (infolog. findnode (PATH). aotfirstchild ());
}
Return;

}

Boolean traverse (treenode _ child)
{
Treenode tmpnode;
Treenodeiterator it;

Tmpnode = _ child. aotfindchild (# reports );
If (tmpnode)
It = tmpnode. aotiterator ();
While (tmpnode)
{
Checkreport (tmpnode );
Tmpnode = it. Next ();
}
Return 0;
}

;
Treenode = infolog. rootnode ();
Child = treenode. aotfindchild (# menu );
// Iterator = Child. aotiterator ();
Child = Child. aotfirstchild ();
While (child)
{
If (traverse (child ))
Break;
Child = Child. aotnextsibling ();
}

}

Query, querybuilddatasource, querybuildlink...
// This job is for seeing the relations that are there in the query datasource
// Please tab the lines in X ++ editor to have a better undersatnding...

Static void querylinks (ARGs _ ARGs)
{
Query query = new query (querystr (custtable); // give your query name as in AOT...

Void getlinks (querybuilddatasource _ qbds)
{
Querybuilddatasource childdatasource;
Querybuildlink;
Counter links;
Int I;
;
If (_ qbds. enabled ())
{
Setprefix (tableid2name (_ qbds. Table ()));
If (_ qbds. Level ()> 1)
{
While (Links <_ qbds. linkcount ())
{
Links ++;
Querybuildlink = _ qbds. Link (LINKS );
Info (strfmt ("% 1.% 2 --> % 3.% 4 ",
Tableid2name (querybuildlink. relatedtable ()),
Fieldid2name (querybuildlink. relatedtable (), querybuildlink. relatedfield ()),
Tableid2name (querybuildlink. Table ()),
Fieldid2name (querybuildlink. Table (), querybuildlink. Field ())));

}
}
For (I = 1; I <= _ qbds. childperformancecount (); I ++)
{
Childdatasource = _ qbds. childdatasourceno (I );
Getlinks (childdatasource );
}
}
}
;

Setprefix (strfmt ("query: '% 1'", query. Name ()));
Getlinks (query. Fetch Ceno (1 ));

}

Number Sequence in projects...
// Create your own number sequence in the projects Module
// All modules have methods to create number sequences... use those methods for creating the records from X ++

Static void projnumberseq (ARGs _ ARGs)
{
Numberseq projnumberseq;
;

Projnumberseq = numberseq: newgetnum (projparameters: numrefprojid (), true );
Print projnumberseq. Num ();
Pause;
}

Get u r database details from sqlsystem class
// Check out the other methods of the sqlsystem class

Static void sqlsystem (ARGs _ ARGs)
{
Sqlsystem = new sqlsystem ();
;
Info (sqlsystem. loginserver () + ''+ sqlsystem. loginname ());

}

Get data from table in XML format...
Static void job5 (ARGs _ ARGs)
{
Custtable;
;
While select custtable
Info (custtable. XML ());
}

Get the default language...
Static void defaultlangid (ARGs _ ARGs)
{
Session session = new session (sessionid ());
;
Info (session. interfacelanguage ());
}

Using wildcard "like"
// The "*" symbolises the like in the statement
Static void usewildcards (ARGs _ ARGs)
{
Query custquery = new query ();
Queryrun;
Custtable;
;
Custquery. adddatasource (tablenum (custtable). addrange (fieldnum (custtable, name). Value ('L *');
Queryrun = new queryrun (custquery );
While (queryrun. Next ())
{
Custtable = queryrun. Get (tablenum (custtable ));
Info (custtable. Name );
}
}

Mandatory fields in table through code
Hi... this is a simple job specially for the fresh learners of ax ..
This job will give us the mandatory fields in a table... check out

Static void mandatoryfields_intable (ARGs _ ARGs)
{

Dicttable;
Dictfield;
Tableid;
Fieldid;
STR result;
# Dictfield
;
Dicttable = new dicttable (tablenum (salestable ));
For (fieldid = dicttable. fieldnext (0); fieldid = dicttable. fieldnext (fieldid ))
{
Dictfield = dicttable. fieldobject (fieldid );

If (! Dictfield. issystem () & bittest (dictfield. Flags (), # dbf_visible)
& Bittest (dictfield. Flags (), # dbf_mandatory ))
{
Result + = dictfield. Name ();
Result + = '/N ';
}
}
Info (result );
}

All the fields in a table through code
The idea behind writing this job is to make clear the concepts of sqldictionay and how to get the fields from a table. also a little introduction to file generation class (textbuffer ). this job create a CSV file by name fieldlist in C: Drive

Static void fieldsintable (ARGs _ ARGs)
{

STR fieldlist = '';
Sqldictionary;
Textbuffer buffer = new textbuffer ();
;
While select * From sqldictionary where sqldictionary. Tabid = tablenum (salestable)
{
Fieldlist + = sqldictionary. sqlname;
Fieldlist + = '/R ';
}
If (fieldlist)
Fieldlist = substr (fieldlist, 1, strlen (fieldlist)-1 );
Buffer. settext (fieldlist );
Buffer. tofile ("C: // fieldslist.csv ");
}

Get specified layer object
This job gets U the list of objects (Forms) which are developed in USR layer. this can be achieved in your possible ways... this job will be time consuming as it uses treenode to find the objects. also, if u click the object in the info displayed, the corresponding form gets opened .. I used sysinfoaction_formrun to achieve this. check out ..

Static void getlayers (ARGs _ ARGs)
{
Treenode;
Xinfo = new xinfo ();
# AOT
;
Treenode = xinfo. findnode (# formspath );
Treenode = treenode. aotfirstchild ();
While (treenode)
{
If (treenode. applobjectlayer () = utilentrylevel: USR)
{
Info (treenode. treenodename (), '', sysinfoaction_formrun: newformname (treenode. treenodename (),'',''));
}
Treenode = treenode. aotnextsibling ();
}

}

No of users online and sessions
This job gives u the number of current online users. Also it throws some light of session classes in axapta 3.0 and its limit.
Static void noofusersonline (ARGs _ ARGs)
{
Int maxsessions = info: licenseduserstotal ();
Userinfo;
Int counter;
Int num;
Xsession session;

If (! Maxsessions) // Demo mode
Maxsessions = 3;

Num = 0;
For (counter = maxsessions; counter --)
{
Session = new xsession (counter, true );
If (Session & session. userid ())
{
Select userinfo where userinfo. ID = session. userid ();
Print userinfo. Name;
}
}
Print "Maximum session ID's in axapta 3.0-", xsession: maxsessionid ();
Pause;

} Transferred from ERP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.