DAX Samples

15
DAX Samples Validate Field Example: Public Boolean validateField(fielded _fieldIdToCheck) { Boolean ret; Ret = super(_fieldIdToCheck); If(_fieldIdToCheck == fieldNum(kkk_CustTable, CreditLimit) { If(this.CreditLimit > 2000.00) { Ret = ret && checkFailed(“Credit limit cannot be greater than 2000”); } } Return ret; } Container Example: Container con = [“1000”, “xyz”, 40, 500.00]; Container rcon; Int I; ; Info(int2str(conlen(con)); Info(conpeek(con,3)); Conview(con); For(I =1; i<=conlen(con);i++) { Info(conpeek(con,i)); } Rcon = condel(con,1,2); Conview(rcon); Con = connull(); Conview(con); Info(strfmt(“xyz is found at %1”,confind(con,”xyz”)));

Transcript of DAX Samples

Page 1: DAX Samples

DAX SamplesValidate Field Example: Public Boolean validateField(fielded _fieldIdToCheck){

Boolean ret;Ret = super(_fieldIdToCheck);If(_fieldIdToCheck == fieldNum(kkk_CustTable, CreditLimit){

If(this.CreditLimit > 2000.00){

Ret = ret && checkFailed(“Credit limit cannot be greater than 2000”);}

}Return ret;

} Container Example:Container con = [“1000”, “xyz”, 40, 500.00];Container rcon;Int I;;Info(int2str(conlen(con));Info(conpeek(con,3));Conview(con);For(I =1; i<=conlen(con);i++){

Info(conpeek(con,i));}Rcon = condel(con,1,2);Conview(rcon);Con = connull();Conview(con);Info(strfmt(“xyz is found at %1”,confind(con,”xyz”))); Functions :

Conpoke - replace an elementConins – insert an elementConlen – find lengthConpeek – will get the value

Page 2: DAX Samples

Condel – deletes set of elements in container and return new containerConfind – will find an element in containerConnull – null the containerConview – show all the elements in container

  Macros:Declaring: #define.NoOfStudents(40) Using:#NoOfStudents Passing Values: Static void job1(Args _args){

Int c;#localmacro.sum

C = %1 + %2;#endmacro;#sum(10,20);Info(int2str(c));

} Passing values from one form to another:Public void init(){

KKK_CustTable k;;If(!element.args().caller()){

Throw error(“Form cannot be opened directly”);}K = element.args().record();Super();StringEdit.text(k.AccountNum + ‘ ‘ + k.Name);

}

Page 3: DAX Samples

 Pass the element from caller to the called. The above is code for called form. Opening a form from another form using X++ code:Void clicked(){

Args args;FormRun formRun;;Super();Args = new Args(formstr(KKK_SecondForm));formRun = ClassFactory.formRunClass(args);args.caller(element);args.record(KKK_CustTable);args.parm(college.text());formRun.init();formRun.run();formRun.wait();

} Getting parm values in child form: 

Str College;;College = element.args().parm();

 Following are used to display hour glass: startLengthyOperation()endLengthyOperation() Animate Controls on Form:Add animate control to form, Public void init(){

#AviFilesSuper();Animate.animateFile(#AviSearch);Animate.autoPlay(true);

Page 4: DAX Samples

} Progress Bar: Static void KKK_ProgressBars(Args _args){

SysOperationProgress progress = new SysOperationProgress();#AviFilesInt I;;Progress.setAnimation(#aviPrint);Progress.setTotal(100000);Progress.setCaption(“Progress Bar Example”);For(i=1;i<=100000;i++){

Progress.incCount();Progress.setText(strfmt(“The value of I is %1”,i));

}} Joins:Inner Join Select if key fiels Equal, From parent and childOuter Join Select all, From parent and childExists Join Select if exists, From parent onlyNotExistsJoin Select if child doesnt have matching records, Only from parent Box ClassIf(Box::YesNo(“Do you want to delete records”, DialogButton::No, “Box Example”, “Bottom text of box”) == DialogButton::Yes){} Dialogs:Dialog d = new Dialog(“My Dialog”);DialogField dfName, dfAge;;dfName = d.addField(Types::String, “Name”, “Name of Customer”);dfAge = d.addField(Types::Integer, “Age”, “Age of Customer”);if(d.run()){

Page 5: DAX Samples

Info(dfName.value());Info(dfAge.value());

}Else{

Throw error(“User Cancelled”);} RunBase Framework: Tutorial_RunBase       Queries:Using AOT Queries: Query q = new Query(querystr(KKK_CustTable));QueryRun qr;KKK_CustTable k;;Qr = new QueryRun(q);If(qr.prompt()){

While(qr.next()){

K = qr.getNo(1); // Use qr.get(tablenum(KKK_CustTable));Info(k.Name + ‘,’ + k.AccountNum);

}} Dynamic Queries (X++ Queries): Query q = new Query();QueryBuildDataSource qbds;QueryBuildRange qbrAccNo, qbrName;QueryRun qr;KKK_CustTable k;

Page 6: DAX Samples

;Qbds = q.addDataSource(tablenum(KKK_CustTable));qbrAccNo = qbds.addRange(fieldnum(KKK_CustTable, AccountNum));qbrAccNo.status(1);qbrAccNo.value(‘100..200’);qbrName = qbds.addRange(fieldnum(KKK_CustTable, Name));qr = new QueryRun(q);if(qr.prompt()){

While(qr.next()){

K = qr.getNo(1);Info(k.AccountNum);

}}  Creating Filtrations on Form:QueryBuildRangeexecuteQuery on data source example: public class FormRun extends ObjectRun{

QueryBuildRange qbrGender;} Override init() method of data source: Public void init(){

Super();qbrGender =

this.query().dataSourceTable(tablenum(KKK_CustTable)).addRange(fieldnum(KKK_CustTable, Gender));} Override executeQuery() of data source: Public void executeQuery()

Page 7: DAX Samples

{;qbrGender.value(enum2str(KKK_Gender::Female));super();

} If filtrations are required on combo, use: 

qbrGender.value(combobox.valueStr()); and, trigger the executeQuery() method of data source in combo box selectionChange() method: public int selectionChange(){

Int ret;Ret = super();KKK_CustTable_ds.executeQuery();Return ret;

}   List Box:FormListItem item;Int I;;I = ListView.getNextItem(FormListNext::Selected);While(i!=-1){

Item = ListView.getItem(i);Info(item.text())I = ListView.getNextItem(FormListNext::Selected, i);

} ListView.delete(i); Resources: ImageCtrl.imageName(SysResource::getImagePath(resourcestr(JaiGanesh_JPG))); 

Page 8: DAX Samples

 Methods handling on Reports:Public boolean fetch(){

Boolean ret;Query q = new Query(element.query());QueryRun qr;KKK_CustTable k;;Qr = new QueryRun(q);If(qr.prompt()){

While(qr.next()){

K = qr.getNo(1);If((k.CreditLimit *20/100) >= 100){

Element.send(k);}

}}

}Return true;   Dialogs on Report:Use/override the following methods

-          dialog()-          getFromDialog()-          pack()-          unpack()

 Example: Public class ReportRun extends ObjectRun{

NoYes showGlassItemMsg;DialogField dfShowGI;

 #define.CurrentVersion(1)

Page 9: DAX Samples

#localmacro.CurrentListshowGlassItemsMsg

#endmacro} Dialog() method: Public object dialog(Object dialog){

DialogRunbase dialog = dialog;;Dialog.addGroup(“My Group”);dfShowGI = dialog.addFieldValue(TypeId(NoYes), showGlassItemsMsg, “Showglass

message”, “Do you want to print the glass error message”);return dialog;

} getFromDialog() method: Boolean getFromDialog(){

;showGlassItemsMsg = dfShowGI.value();return true;

} Implement pack() and unpack(). Goto fetch() method and use the variable to conditionally execute the programmable section: If(showGlassItemMsg == NoYes::Yes){

Element.execute(2);}   File Handling:Create text file: TextBuffer t = new TextBuffer();;

Page 10: DAX Samples

t.setText(“I am happy today”);t.toFile(“c:\\sample.txt”);  Read text file: TextBuffer t = new TextBuffer();;If(WinAPI::fileExists(“c:\\sample.txt”)){

t.fromFile(“c:\\sample.txt”);info(t.getText());

}Else{

Throw error(“File doesn’t exist”);} CSV FileCommaIO [CommaIO, FileIOPermission, WinAPI] Important methods in CommaIO class:

-          InFieldDelimiter-          InRecordDelimiter-          OutFieldDelimiter-          OutRecordDelimiter-          Status-          Read-          Write

 Example: CommaIO commaIO;Container readCon;FileIOPermission fio;KKK_CustTable k;;If(!WinAPI::fileExists(“C:\\Customers_KKK.txt”));

Throw error(“File doesn’t exist”); Fio = new FileIOPermission(“c:\\CustomersKKK.txt”,”r”); //w to write

Page 11: DAX Samples

Fio.assert();commaIO = new CommaIO(“c:\\CustomersKKK.txt”,”r”); // w to writecommaIO.inFieldDelimiter(“,”); //outFieldDelimiter() while writingcommaIO.inRecordDelimiter(“\r\n”); //outRecordDelimiter() while writingif(commaIO){

While(commaIO.status() == IO_Status::OK){

readCon = commaIO.read(); //write() to write recordsif(conlen(readCon)>0){

k.AccountNum = conpeek(readCon, 1);k.Name = conpeek(reacCon, 2);k.CreditLimit = conpeek(readcon, 3);k.insert();

}}

}  Reading and Writing XML Files: Classes:

XMLDocumentXMLTextWriter Important methods:-          getElementsByTagName()-          length()

 Reading from XML File: XMLDocument xmlDoc = XMLDocument::newFile(“c:\\CustomerKKK.xml”);Int I, noOfTags;KKK_CustTable k;;noOfTags = xmlDoc.getElementsByTagName(“CustomerDetails”).length(); //Count the tags

//XML tag starts with index 0 For(i=0;i<noOfTags;i++)

Page 12: DAX Samples

{k.AccountNum = xmlDoc.getElementsByTagName(“AccNo”).item(i).text();k.Name = xmlDoc.getElementsByTagName(“Name”).item(i).text();k.insert();

}  Writing into XML File: XMLTextWriter xmlWriter;KKK_CustTable k;;xmlWriter = XMLTextWriter::newFile(“c:\\KKKCustomersAX.xml”);xmlWriter.writeStartDocument();xmlWriter.writeStartElement(“Details”);while select k{

xmlWriter.writeStartElement(“CustomerDetails”);xmlWriter.writeElementString(“AccNo”,k.AccountNum);xmlWriter.writeElementString(“Name”,k.Name);xmlWriter.writeEndElement();

}xmlWriter.writeEndElement();xmlWriter.writeEndDocument();xmlWriter.close();