Wednesday, May 4, 2011

ObjectScript Cheat Sheet for Ensemble [Cache]


Action

Code
Alternative way
Call a class method
##class(package.class).method(arguments)
set variable = ##class(package.class).method(arguments)
Call an instance method
do oref.method(arguments) 
set variable = oref.method(arguments)
Create a new object
set oref = ##class(package.class).%New()

Open an existing object
set oref = ##class(package.class).%OpenId(id)

Save an object
set status = oref.%Save()

Validate an object without saving
set status = oref.%ValidateObject()

Validate a property without saving
set status = ##class(package.class).PropertyIsValid(oref.Property)

Delete an existing object
set status = ##class(package.class).%DeleteId(id)

Link two objects
set oref1.property = oref2


Clone an object
set clonedOref = oref.%ConstructClone()


Start the SQL shell
do $system.SQL.Shell()

>>go
>> SELECTMODE = DISPLAY

Test a class query
do ##class(%ResultSet).RunQuery(class, query)

Create a new standalone array
set arrayOref=##class(%ArrayOfDataTypes).%New()

Insert an element into an array
do oref.arrayProperty.SetAt(value,key)
do arrayOref.SetAt(value,key)
Display an element of an array
do oref.arrayProperty.GetAt(key)
do oref.arrayProperty.GetAt(key)
Display the size of an array
do oref.arrayProperty.Count()
write arrayOref.Count()
Date conversion (external à internal)
set variable = $zdh(“mm/dd/yyyy”)

Date conversion (internal à external)
set variable = $zd(internalDate, format)

Time conversion (external à internal)
set variable = $zth(“hh:mm:ss”)

Time conversion (internal à external)      
set variable = $zt(internalTime, format)

Display internal date/time string
write $system.SYS.Horolog()
write $horolog
Display UTC date/time string
write $system.SYS.TimeStamp()

Check if variable exists
write $data(variable)

Return value of variable, or "" if undefined
write $get(variable)

For Loop Syntax
For ControlVariable = StartValue:IncrementAmount:EndValue


Cheat Sheet for Crystal Report Formulas

Conversion Functions:
Conversion Function
Description
CBool(number), CBool(currency)
Convert to Boolean.
CCur(number), CCur(string)
Convert to Currency.
CDbl(currency), CDbl(string),
CDbl(boolean)
Convert to Number. Equivalent to ToNumber().
CStr()
Convert to String. Equivalent to ToText().
CDate(string), CDate(year, month, day), CDate(DateTime)
Convert to Date.
CTime(string), CTime(hour, min, sec), CDate(DateTime)
Convert to Time.
CDateTime(string),
CDateTime(date),
CDateTime(date, time),
CDateTime(year, month, day)
Convert to DateTime.
CDateTime(year, month, day, hour, min, sec)
Convert to DateTime.
ToNumber(string), ToNumber(boolean)
Convert to a Number.
ToText()
Convert to String. Same as CStr().
IsDate(string), IsTIme(), IsDateTime()
Test a string for being a valid date/time.
IsNumber(string)
Test a string for being a valid number.
ToWords(number),
ToWords(number, decimals)
Convert a number to its word equivalent.


·         Formula = ToWords(123.45) Result is one hundred twenty-three 45 / 100
 Math Functions:
Function Name
Description
Abs(number)
Return the absolute value.
Fix(number, decimals)
Return a number with a specified number of significant digits.
Int(number), numerator \ denominator
Return the integer portion of a fractional number.
Remainder(numerator, denominator),
Return the remainder of dividing the numerator by the denominator.
numerator Mod denominator
Return the remainder of dividing the numerator by the denominator.
Round(number, decimals)
Round up a number with a specified number of significant digits.
Sgn(number)
Return a number's sign.
Sqr(number), Exp(number), Log(number)
The standard arithmetic functions.
Cos(number), Sin(number), Tan(number), Atn(number)
The standard scientific functions.


Convert display string to cost: 
CStr (CCur(ToNumber({Data.SupplyCost})/100))

New Line in formula : ChrW(13)
New Line in XML     :  

Alternate Row Color:
if RecordNumber mod 2 = 0 then crSilver else crNoColor



3-Formula Trick:     Init/Calc/Display:










Tuesday, May 3, 2011

Trick to create separate .cs file for global.asax file and using JavaScript to redirect to custom error page for unauthorized users (code 401)

There is no way to create a global.asax.cs page in vs 2008 or older versions of Visual studio. So we have a work around.

1. Add Global.asax file to your project, notice it won’t let you put the code in separate .cs file.

2. Add a new code file to your project "Global.cs". It will ask you to put the file in App_Code folder (Important step).You click "Yes" which will put the file in App_code folder.


3. Now open the global.cs file you just created and add
using System.Web.Configration;
4. Inherit the Global class from System.Web.HttpApplication like
public class Global : System.Web.HttpApplication

5. Now go back to your old Global.asax file and copy everything inside <Script> tag to this Global.cs file.
6. In Global.asax add Inherit property in Application tag to point to this new code file, remove everything from Global.asax file but only one line is left
<%@ Application Language="C#"  Inherits="Global" %> 

7. Inherits tag in the above line points to the .cs file we are using "Global"
8. You should be good to go and use the Global.cs file without any problem.


Now to create a custom error page for Unauthorized users (code 401/ code 401.2) simply add Application_EndRequest() function in Global.cs page and use JavaScript code to redirect self to custom error page by trapping the 401 Unauthorized code.
1. Create a custom error Page ( E.g. CustomError.cs)
2. Create authorization tag in web.config file to deny or allow users.
3. If the user’s access is denied the Application_EndRequest() function will try to capture the Context Response. We can all kinds of response like error code 401, 402, 404 for my example I am using 401.
4. If the code is indeed 401 (we only look for first 3 letters of the Response status ignoring all the other detail) we redirect the user to the custom error page with this line of code.
context.Response.Write("<script language=javascript>self.location='CustomError.aspx';</script>");
5. So the whole function will look like this:
protected void Application_EndRequest(Object sender, EventArgs e)
{
   HttpContext context = HttpContext.Current;
   if (context.Response.Status.Substring(0, 3).Equals("401"))
   {
      context.Response.ClearContent();
      context.Response.Write("<script  
      language=javascript>self.location='CustomError.aspx'; </script>");
    }
 }


6. You have successfully created a custom code file for Global.asax as well as created a custom error page for unauthorized users.

RootComponent types in solution.xml file in Dynamics CRM 365/2016

In Microsoft Dynamic CRM 2016/365 are you as confused as me when looking at the solution.xml from the solution export? looking at the xml a...