Thursday, June 17, 2010

Some examples of use

So, a lot of code published with no explanation (so far). In the meantime here are some examples of how the Framework can be used:

Server-Side add the line:
var oFramework = TheApplication().GetService("My Framework").frameworkHandle();

to the declarations section of the BC/BS.

1. Query a BC
So let's get the name of the currently logged in user
var sFullName = oFramework.BusComp.getRecord("Employee.Employee", TheApplication().LoginId(), ["Full Name"])["Full Name"];

*a little flavour of what's to come in the explanation - the Framework returns JS objects to represent complex types (e.g. a record, property set) - so the ["Full Name"] gets this property from the object.

2. Query a child BC
So here we're going to get the Contact records relating to a particular Opportunity, then loop through them (just for illustration). Here BusComp.getRecords returns an array of JS objects.
var oOpty = oFramework.BusComp.getRecord("Opportunity.Opportunity", "1-123F4", "", true);
var aoBorrower = oFramework.BusComp.getRecords("Opportunity.Contact", "", ["First Name", "Last Name", "Login Name"]);

for (var i = 0; i < aoBorrower .length; i++)
{
// just to illustrate logic - build a string
var sString = aoBorrower[i].First_Name + " " + aoBorrower[i].Last_Name + " has a login of " + aoBorrower[i].Login_Name ;
...
...
}

3. Get the "Description" field from an LOV:
var sDescription = oFramework .Lov.getDescription("OCCUPATION","Professional");

4. Run a BS method:
var bIsValid = oFramework .Service.run("My System Process Service.ValidatePpsn", ["Ppsn"], ["4576554F"]).IsValid;

* one thing this has encapsulated is the building of propertysets - the input set here would have one property, "Ppsn" with a value of "4576554F", then in the output set there is one value returned called "IsValid".

If more than one property were returned (say a second called "Message" was present) in the output we could access them both using the JS object:
var oReturn = oFramework .Service.run("My System Process Service.ValidatePpsn", ["Ppsn"], ["4576554F"]);
var bIsValid = oReturn.IsValid;
var sMessage = oReturn.Message;


* to make the BS method run in the background simply use the Service.runAsynch method in place of the Service.run; same inputs, although output is limited to a single boolean.

oFramework.Service.runAsynch("My System Process Service.ValidatePpsn", ["Ppsn"], ["4576554F"]);


For the asynch to work there is a workflow process used for this. This hasn't been posted, so this is for illustration only)

5. Get a system preference:
var sMyPref = oFramework.Utility.getSystemPreference("My System Preference");


6. Send an Email:
(where sEmail is the text, sAttachFile1 and sAttachFile2 are two files to be attached)

oFramework.Utility.sendEmail("Notification From the System",
"",
"Support Team Mailing List",
"",
sEmail,
sAttachFile1 + "*" + sAttachFile2);


7. See if a record (here, taking an LOV for an example) has been updated since a certain date (held here in a system preference):

var sLastRunDate = oFramework.Utility.getSystemPreference("Op Last Run Date");
var oLovRecord = oFramework.BusComp.getRecord("List Of Values.List Of Values","[Type]= 'OCCUPATION'", ["Name","Value"], true);

if (oLovRecord.Updated.compare(">",sCheckParamDate))
{
...


* this gives a sample of some of the Framework features, e.g.: typing of fields from the query (so Updated is a date), Date has a method added to it to compare it against a string representing another date.

8. Some other new Date features:
var sEuDateString = new Date().toDb();
var sUsDateString = new Date().toUs();
var sIsoDateString = new Date().toIso();
var sTimeStamp = new Date().toTimeStamp();

var dDate = new Date();
dDate.setFromString("25/12/1989");


9 Logging:
There are two aspects to logging - the ability to log as things happen. I've removed the code itself to write to a custom CX_ table, but to log from anywhere the following syntax is used:

oFramework.Utility.logMessage(type, code, short description, source name, source type,  source function name, long detail string);



So, for example, in a catch block in a BS PreInvoke method we could have:

oFramework.Utility.logMessage("Error", e.errCode, e.toString(), this.Name(), "BusServ", "Service_PreInvokeMethod(" + MethodName + " ", sDebug);


The second aspect of logging is provided by the Framework object to show its own operations (not just errors) so

var sFeedback = oFramework.getStack();


returns the internal log of the Framework. An example could be:

[UID:20100616162835716]
[20100616163956151] *** START 'BusComp.getRecords' ***
[20100616163956151] Search:1-A3VYMH
[20100616163956151] Reset Context:true
[20100616163956151] Clearing objects
[20100616163956167] Objects cleared
[20100616163956167] Added BusObject: My Application to the pool.
[20100616163956167] Added BusComp: My Application to the pool.
[20100616163956167] Activated: Name
[20100616163956167] Activated: Id
[20100616163956167] Activated: Created
[20100616163956183] Activated: Updated
[20100616163956183] Search:1-A3VYMH Type:string
[20100616163956183] After Search:
[20100616163956386] Error:A script failed to get the value for field Name because the field was not active.(SBL-EXL-00119)


* each line is preceded with the timestamp (down to millisecond) of when that message was logged. This is handy to see exactly how long a step is taking.

10. Browser-Side:

No need to get any references as the browser-side Framework is attached to the "top" object - available anywere.

The syntax (for the methods exposed by the browser-side) matches their server side counterparts. So:

(query a BC)
var sFullName = top.oFramework.BusComp.getRecord("Employee.Employee","0-1" , ["Full Name"])


(get an LOV's description field)
var sDescription = top.oFramework .Lov.getDescription("OCCUPATION","Professional");



(run a BS method)
top.oFramework.Service.run("My System Process Service.ValidatePpsn", ["Ppsn"], ["4576554F"]);



(get a system preference)
var sMyPref = top.oFramework.Utility.getSystemPreference("My Preference Value");


(get a timestamp)
var sTimeStamp = new Date().toTimeStamp();


I'll follow this up with a post on the mechanism used by the browser side of the Framework, so that the methods have the same arguments, and return the same values (including JS objects) as their server side counterparts.

No comments:

Post a Comment