Important function in X++
I would like to share some important function in x++
Like subStr(),
strCmp(),strDel() , strFind (),strfmt ()
,strLen (),strLwr (),strUpr (),strRep(),systemDateGet(),today(),trunc (),boxExample
(),conins(), conLen(), conPeek(), conNull() which seems to easy but some time became very
tough to recall in between to coding, so don’t worry and keep coding…….
Sub String in X++
// for cut a string from given
posititon SubStr("ABCDEFGHIJ",7,-4) returns the text string
"DEFG" (the specified sub-string).
static void subStr(Args
_args)
{
str s;
real r;
;
r =
strLen("ipankaj.u@gmail.com");
s =
subStr("ipankaj.u@gmail.com",12, 2);
print(strFmt("s=%1
and r=%2",s,r));
pause;
}
String Comparison in X++
static void strCmp(Args
_args)
{
int i=2;
str s1,s2;
;
s1="string
1";
//s2="string
1";
s2="string
2";
i
= strCmp(s1,s2);
if
(0 == i)
{
print
"s1 and s2 are the same";
}
else
{
print
"s1 and s2 are different";
}
pause;
}
String Deletion in X++
static void strDel(Args
_args)
{
str s;
;
s =
strDel("Kanhakumar", 5, 2);
print
s;
pause;//for
cut the string size first give from where it will cut thn give till thn it will
cut
}
Find characters in string in X++
static void strFind(Args
_args)
{
int i;
;
i =
strfind("kanhakumar", "kan", 0, 3);
if (1
== i)
{
print
"Characters are found in string";
}
else
{
print
"Characters are NOT found in string";
}
pause;
}
Strfmt()in X++
static void strfmt(Args
_args)
{
str s1
="testing";
int s2
= 2;
real s3 =4.56;
str s;
;
s =
strfmt("string =%1,Integer = %2, Real = %3, ", s1,s2,s3);
print s;
pause;
}
Length of string in X++
// It will return no. of characters in given
string
static void strLen(Args _args)
{
int i;
;
i =
strLen("kanha");
print(strFmt("i=%1",i));
pause;
}
Convert string in lower case in X++
static void strLwr(Args
_args)
{
str l;
;
l
= strLwr("KANHA");
print(strFmt("l=%1",l));
pause;
}
Convert string in upper case in X++
static void strUpr(Args
_args)
{
str u;
;
u=
strUpr("kanha");
print(strFmt("u=%1",u));
pause;
}
Repetition of string in X++
static void strRep(Args
_args)
{
str r;
;
r =
strRep("xyz ", 5);
print(strFmt("r=%1",r));
pause;
}
Convert date into string in x++
// we can get date according to own format
static void
systemDateGet(Args _args)
{
date d;
str d1,d2,d3 ;
;
d
= systemdateget();
d1
= date2str(d,123,2,-1,2,-1,4);
d2
= date2str(d,231,2,-1,2,-1,2);
d3
= date2str(d,321,2,-1,2,-1,4);
print(strFmt("dates
are %1,%2,%3,%4",d,d1,d2,d3));
pause;
}
Truncate Real value in X++
// will round off all digit
after decimal(.)
static void trunc(Args
_args)
{
real t;
;
t =
trunc(4.6789);
print
strfmt("t = %1", t);
pause;
}
Creation of Dialog in X++
static void
dialogTest(Args _args)
{
Dialog dialog;
DialogGroup dialogGroup;
DialogField dialogField;
;
dialog = new Dialog("Test
Dialog");
dialogGroup =
dialog.addGroup("Customer Details");
dialogField =
dialog.addField(extendedTypeStr(Custaccount),"Account Number");
dialog.run();
}
Create box for prompt in X++
static void
boxExample(Args _args)
{
if(box::yesNo("Are
u sure to close this form",dialogbutton::No,"Box
Title")==dialogbutton::yes)
{
print("Closing.......");
pause;
}
else
{
print("Not
closing......");
pause;
}
}
Container Operation in X++
// conins to insert into
container
// conLen() for the size of
the container
// conPeek() to get contents
of container item
// conNull() to Assign null
value
static void conpeek(Args
_args)
{
container c;
int i;
;
c=conNull();
c
=["item1", "item2","kan"];
c=
conIns(c,2,"test");
for (i = 1 ;
i <= conLen(c) ; i++)
{
print
conPeek(c, i);
}
pause;
}
Rounding in X++
I recently had to do some rounding in X++ on a real variable.
This was due to some custom Rebate code. Anyway, I have some sample code here
for rounding. I found it strange really how the synatx had to be and could not
find anywhere on the internet what the synatx needed to be, so here you go:
server static void RoundExample(Args _args)
{
real r = 7.8413;
real a;
;
a = round(r,0.01);
r = a;
}
Anyway, if you put 0.00 in the decimal, then no decimal is used,
but if you supply 0.01 in the decial variable then it works great. So if you
wanted to round to 0.0000 then you would need to put there 0.0001.
strRem Removes the characters specified in one string from
another string.
str strRem(str text1, str text2) where text1 : The string from
which to remove characters.text2 : The characters to exclude from the output
string.
subStr
Retrieves part of a string.
str subStr(str _text, int _position, int _number)
where_text : The original string._position : The position in the
original string where the part to retrieve begins.
_number : A signed integer that indicates the direction and
number of positions to retrieve from the original string.
If there is a minus sign preceding _number, the system selects
the substring backward from the specified position.
strDel
Creates a copy of a string with the specified substring removed.
str strDel( str _text, int _position, int _number)
where_text : The string to copy from_position : The position at
which to begin ignoring characters during the copy.
_number : The number of characters to ignore.
A minus in front of _number parameter indicates that the
(_number-1) characters before the character at the _position parameter are to
be removed along with the character at the _position.
strFmt
Formats the specified string and substitutes any occurrences of
%n with the nth argument.
str strFmt(str _string, …)where _string : The strings to be
formatted.
example: to set the number of page in a report :
Display STR 20 pageNum()
{
return StrFmt(“@SYS24160″, element.page());
}
num2Str
Converts a real number to a string.
str num2Str(
real number,
int character,
int decimals,
int separator1,
int separator2)where: number The real number to convert to a
string.
character The minimum number of characters required in the text.
decimals The required number of decimal places.
separator1 A DecimalSeparator enumeration value.
separator2 A ThousandSeparator enumeration value.
Example
static void Job_Num2Str(Args _args)
{
real realNum = 0.1294567890123456777; // 19 decimals places.
;
info(Num2Str(realNum,0,16,1,3)); // 16 decimals
info(Num2Str(realNum,0,17,1,3)); // 17 decimals
} Round Rounds a real number to the nearest multiple of another
real number.real round(real _arg, real _decimals) _arg The original number.
_decimals The number that the value of the _arg parameter must
be rounded to a multiple of.
Examples:round(123.45,5.00); //Returns the value 125.00.
round(7.45,1.05); //Returns the value 7.35.
round(23.9,5.0); //Returns the value 25.00;
round(26.1,5.0); //Returns the value 25.00;
strLen
Returns the size of a string
int strLen ( Str _text )
ConPeek
Retrieves a specific element from a container.
anytype conPeek(container container, int number)Example:static
void conPeekExample(Args _arg)
{
container c;
int i;
;
c = conIns(["item1", "item2"], 1);
for (i = 1 ; i {
print conPeek(c, i);
}
pause;
}
strUpr
Converts all the letters in a string to uppercase.
str strUpr(str _text)
static void strUprExample(Args _args)
{
print strUpr(“Abcdd55EFGhiJ”);
pause;
}
strLwrConverts all the letters in a string to
lowercase. str strLwr(str _text)
static void strLwrExample(Args _args)
{
// Returns the text string “abcdd55efghij”.
print strLwr(“Abcdd55EFGHIJ”);
pause;
}
Units We use it to set the unit of a dimension.Example :
element.design().controlName(“CustInvoiceJour_InvoicingName”);
CustInvoiceJour_InvoicingName.top(20.8, Units::mm);
Global::dateEndYr Method
Retrieves the date of the last day in the same year as the
specified date.
client server public static date dateEndYr(date transDate)
Global::DateMthFwd Method
Adds the specified number of months to a date.
client server public static date DateMthFwd(date transDate, int
qty)
Global::DateStartYr Method
Retrieves the first date of the specified year.
client server public static date DateStartYr (date transDate)
tableName2Id FunctionRetrieves the ID of a table.
int tableName2Id(str _name
Excellent piece of knowledge,I was really impressed by seeing this article, it was very intresting and it is very useful.I hope you can continue and post more.
ReplyDeleteMicrosoft Dynamics CRM Online Training | AX Training