>> 本版精华贴    X++和MorphxX技术讨论区
搜一搜相关精彩主题 
秋毫ERP咨询-微软Dynamics Ax Axapta Nav SL CRM论坛微软ERP Dynamics系列【本版最新】『Microsoft Dynamics Ax (Axapta) 开发』 → 常用的一些X++例子

您是本帖的第 2106 个阅读者
树形 打印
标题:
常用的一些X++例子
作 者
帖 子
editor
帅哥哟,离线,有人找我吗?
等级:高级顾问
积分:3029
现金:2038
文章:344
门派:无门无派
注册:2008年6月30日
楼主
  点击这里发送电子邮件给editor

发贴心情
常用的一些X++例子
发表于2009-3-30 23:43:07
Simple select statement example
static void selectExample(Args _args)
{
CustTable 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 args;
FormRun 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 should be");
frc.helpText("Whatever the help message should be");
dialog.run();


Look up on the form datasource...
public void lookup(FormControl _formControl, str filterStr)
{
Table1 Table1;
SysTableLookup Lookup = SysTableLookup::newParameters(tableNum(Table1),_formControl);
Query query = new Query();
QueryBuildDataSource queryBuildDataSource;
QueryBuildRange 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.performFormLookup();
}


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 a;
print b;
print c;
print d;
pause;
}

2) To replace a filename with theAxapta standard filename use the following...
strReplace(filepath,'\\','/');

3)
TextBuffer 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 = 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 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 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 sqlDictionary;
TableName tabName;
CommaIo myfile;
CustTrans 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('Reports')

Treenode 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 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.childDataSourceCount(); i++)
{
childDataSource = _qbds.childDataSourceNo(i);
getLinks(childDataSource);
}
}
}
;


setPrefix(strFmt("Query: '%1'", query.name()));
getLinks(query.dataSourceNo(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 SqlSystem = new SqlSystem();
;
info(SqlSystem.loginServer() + ' ' + SqlSystem.loginName());

}

Get data from table in XML format...
static void Job5(Args _args)
{
custtable 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 queryRun;
CustTable 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 dictTable;
DictField dictField;
tableId tableId;
fieldId fieldId;
str result;
#DictField
;
dicttable = new DictTable(tablenum(salestable));
for (fieldId = dictTable.fieldNext(0);fieldId;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 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 many 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 treeNode;
xInfo 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 userinfo;
int counter;
int num;
xSession session;

if (!maxSessions) //Demo mode
maxSessions = 3;

num = 0;
for(counter = maxSessions;counter;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;

}
ip地址已设置保密
2009-3-30 23:43:07
jinrk
帅哥哟,离线,有人找我吗?
等级:顾问
积分:1689
现金:2567
文章:162
门派:无门无派
注册:2006年10月28日
2
  点击这里发送电子邮件给jinrk

发贴心情
发表于2009-3-31 21:51:17

对我这样的初学者很有帮助

3Q


msn: jinrk1226@hotmail.com
ip地址已设置保密
2009-3-31 21:51:17
回帖是一种美德,可表达自己的思想和态度,也可给别人一些指导.
codingbaby
帅哥哟,离线,有人找我吗?
等级:高级顾问
积分:1357
现金:1233
文章:211
门派:无门无派
注册:2008年2月21日
3
  点击这里发送电子邮件给codingbaby

发贴心情
发表于2009-4-1 14:13:25
对于俺这样的处于在入门与刚入门的边缘之间的学习者是有极大的帮助的!Thk!
ip地址已设置保密
2009-4-1 14:13:25
秋毫咨询-问题的海洋,又是解决方案的海洋!是磨炼顾问的沃土!
fly8
帅哥哟,离线,有人找我吗?
头衔:山寨顾问
等级:准顾问
积分:684
现金:889
文章:89
门派:无门无派
注册:2008年3月6日
4
  点击这里发送电子邮件给fly8 访问fly8的主页

发贴心情
发表于2009-4-1 15:50:57

九龙坡社区九龙名企论 坛九龙交通交友笑话主题社区休闲游戏
ip地址已设置保密
2009-4-1 15:50:57
kings1108
帅哥哟,离线,有人找我吗?
等级:顾问
积分:1185
现金:1393
文章:152
门派:无门无派
注册:2008年3月27日
5
  点击这里发送电子邮件给kings1108

发贴心情
发表于2009-4-2 12:50:04

太好了,谢谢楼主!


Life is not fair,get used to i t! MSN: xieyufan1108@hotmail.com QQ:332469390
ip地址已设置保密
2009-4-2 12:50:04
回帖是一种美德,可表达自己的思想和态度,也可给别人一些指导.
食铁兽
帅哥哟,离线,有人找我吗?
等级:顾问
积分:1017
现金:1387
文章:133
门派:无门无派
注册:2007年9月13日
6
  点击这里发送电子邮件给食铁兽

发贴心情
发表于2009-4-17 17:33:50

非常好!非常好!!

微软自己做的帮助就少这个!


QQ:168127001 MSN:shenchen723@hotmail.com QQ群:70231970(企业信息化—微软ERP)
ip地址已设置保密
2009-4-17 17:33:50
食铁兽
帅哥哟,离线,有人找我吗?
等级:顾问
积分:1017
现金:1387
文章:133
门派:无门无派
注册:2007年9月13日
7
  点击这里发送电子邮件给食铁兽

发贴心情
发表于2009-4-19 15:51:07

最后一段代码是否有错,当中一句

for(counter = maxSessions;counter;counter--)


这里的counter是否应该是num或者0?


QQ:168127001 MSN:shenchen723@hotmail.com QQ群:70231970(企业信息化—微软ERP)
ip地址已设置保密
2009-4-19 15:51:07
回帖是一种美德,可表达自己的思想和态度,也可给别人一些指导.
ytlimin
帅哥哟,离线,有人找我吗?
斑竹荣誉奖!
等级:版主
积分:1749
现金:2972
文章:249
门派:无门无派
注册:2006年4月3日
8
  点击这里发送电子邮件给ytlimin

发贴心情
发表于2009-5-6 12:14:06

总结的很好的,可以放到总版主的实用开发技术范例里面


MSN: Stephen.wanglm@hotmail.com
ip地址已设置保密
2009-5-6 12:14:06
zxkid
帅哥哟,离线,有人找我吗?
等级:技术员
积分:197
现金:254
文章:20
门派:无门无派
注册:2008年5月13日
9
  点击这里发送电子邮件给zxkid

发贴心情
发表于2009-5-12 10:26:22

多谢楼主 学习了

ip地址已设置保密
2009-5-12 10:26:22
希望你能把那些最让你骄傲的经验和技术和其他朋友一起分享!
心血来潮
帅哥哟,离线,有人找我吗?
等级:技术员
积分:311
现金:453
文章:24
门派:无门无派
注册:2007年11月14日
10
  点击这里发送电子邮件给心血来潮

发贴心情
发表于2009-6-3 10:54:00
Thanks for sharing.
ip地址已设置保密
2009-6-3 10:54:00
希望你能把那些最让你骄傲的经验和技术和其他朋友一起分享!
later100
帅哥哟,离线,有人找我吗?
等级:技术员
积分:315
现金:350
文章:32
门派:无门无派
注册:2009年6月5日
11
  点击这里发送电子邮件给later100

发贴心情
发表于2009-7-7 18:22:06

so good

ip地址已设置保密
2009-7-7 18:22:06
秋毫咨询-问题的海洋,又是解决方案的海洋!是磨炼顾问的沃土!
angelhua
帅哥哟,离线,有人找我吗?
等级:高级顾问
积分:1062
现金:1058
文章:204
门派:无门无派
注册:2009年6月15日
12
  点击这里发送电子邮件给angelhua

发贴心情
发表于2009-7-8 10:58:46
不错的帖,顶!

Microsoft Dynamics AX开发技术培训; 多年实践经验分享,用最低的成本,让您迅速步入AX开发行列。 MSN:goodserver@live.cn 主页: http://share168.51.com ----踏实做事,诚实做人。----
ip地址已设置保密
2009-7-8 10:58:46
collapsar
帅哥哟,离线,有人找我吗?
等级:准顾问
积分:663
现金:875
文章:98
门派:无门无派
注册:2008年4月16日
13
  点击这里发送电子邮件给collapsar

发贴心情
发表于2009-7-16 22:56:33
不错的资料
ip地址已设置保密
2009-7-16 22:56:33
希望你能把那些最让你骄傲的经验和技术和其他朋友一起分享!
itcql
帅哥哟,离线,有人找我吗?
等级:技术员
积分:56
现金:59
文章:7
门派:无门无派
注册:2010年4月17日
14
  点击这里发送电子邮件给itcql

发贴心情
发表于2010-6-1 9:30:15
哇,謝謝你樓主,我回覆了再看。
ip地址已设置保密
2010-6-1 9:30:15
liuyun33
帅哥哟,离线,有人找我吗?
等级:顾问
积分:732
现金:728
文章:135
门派:无门无派
注册:2010年3月29日
15
  点击这里发送电子邮件给liuyun33

发贴心情
发表于2010-7-2 11:43:40

先收起来再顶一下

ip地址已设置保密
2010-7-2 11:43:40
技术和经验来源于对实际问题的解决!
hpy008
美女呀,离线,留言给我吧!
等级:技术员
积分:66
现金:69
文章:8
门派:无门无派
注册:2009年4月19日
16
  点击这里发送电子邮件给hpy008

发贴心情
发表于2010-7-19 15:31:43
好東東,謝謝!
ip地址已设置保密
2010-7-19 15:31:43
mryap82
帅哥哟,离线,有人找我吗?
等级:技术员
积分:98
现金:102
文章:15
门派:无门无派
注册:2010年7月27日
17
  点击这里发送电子邮件给mryap82

发贴心情
发表于2010-7-28 8:59:35
可以学习!!!!!顶楼主
ip地址已设置保密
2010-7-28 8:59:35
技术和经验来源于对实际问题的解决!

 17   17   1/1页      1    

粤ICP备06100540号 】 广告及业务联系】秋毫IT百科】
Copyright ©2006-2008 www.QiuHao.com地图1】【地图2】【地图3】【所有贴】【技术文集】 【秋毫ERP