Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 10482

Re: Does anyone have a code sample to generate the current time/server time?

$
0
0

Yes. I did. I spoke with our core developers and they said the changed/created user/datetime stamping on the standard BOs is a core reuse library not exposed to SDK. So the following is a lean version of the basic functionality that can be easily implemented on the root and any node of the customBO

 

a. Captures the logged in user when the customBO is created vs. updated

b. Captures the current server date and time when the customBO is saved

 

1. Add the following fields to the root or node to the custom BO depending on where you want datetime/user history tracking

 

[Label("Changed By")] [Tooltip("Changed By")] element ChangedByID : BusinessPartnerInternalID;

[Label("Created By")] [Tooltip("Created By")] element CreatedByID : BusinessPartnerInternalID;

[Label("Changed On")] [Tooltip("Changed On")] element ChangedOnDate : Date;

[Label("Created On")] [Tooltip("Created On")] element CreatedOnDate : Date;

[Label("Changed On Time")] [Tooltip("Changed On Time")] element ChangedOnTime : Time;

[Label("Created On Time")] [Tooltip("Created On Time")] element CreatedOnTime : Time;

 

2. Add the following associations to the custom BO

 

association ChangedBy to Employee;

association CreatedBy to Employee;

 

3. Add the following to the aftermodify script to the custom BO

 

importABSL;

importAP.PC.IdentityManagement.Global;

importAP.FO.BusinessPartner.Global;

 

//get identity/employee Internal ID

varidentity;

varemployee;

identity = Identity.Retrieve(Context.GetCurrentIdentityUUID());

 

if (identity.IsSet() ){

Trace.Info("I", "Identity found");

employee = BusinessPartner.Retrieve(identity.BusinessPartnerUUID);

 

if ( employee.IsSet() ){

Trace.Info("I", "Employee found");

}

}

 

 

//check and set Created By to logged in user if not set for the first time

if (this.CreatedByID.IsInitial()){

Trace.Info("I", "Created By not set");

this.CreatedByID = employee.InternalID;

}

 

//set Changed By to logged in user

this.ChangedByID = employee.InternalID;

 

//query the employee BO to get the createdby related data

varquery_createdby;

varcreatedby_selparam;

varcreatedby_instance;

varquery_createdby_result;

 

query_createdby = Employee.QueryByIdentification;

createdby_selparam = query_createdby.CreateSelectionParams();

createdby_selparam.Add(query_createdby.InternalID, "I", "EQ", this.CreatedByID);

query_createdby_result = query_createdby.Execute(createdby_selparam);

 

if (query_createdby_result.Count() == 0) {

this.CreatedBy.Reset();

}

 

else {

foreach (createdby_instanceinquery_createdby_result) {

this.CreatedBy = createdby_instance;

break;

}

}

 

 

//query the employee BO to get the changedby related data

varquery_changedby;

varchangedby_selparam;

varchangedby_instance;

varquery_changedby_result;

 

query_changedby = Employee.QueryByIdentification;

changedby_selparam = query_changedby.CreateSelectionParams();

changedby_selparam.Add(query_changedby.InternalID, "I", "EQ", this.ChangedByID);

query_changedby_result = query_changedby.Execute(changedby_selparam);

 

if (query_changedby_result.Count() == 0) {

this.ChangedBy.Reset();

}

 

else {

foreach (changedby_instanceinquery_changedby_result) {

this.ChangedBy = changedby_instance;

break;

}

}

 

4. Add the following to the beforesave script to the custom BO

import ABSL;

import AP.FO.BusinessPartner.Global;

import AP.PC.IdentityManagement.Global;

 

// set the changed on to UTC system time.

var UTCDateTime = Context.GetCurrentGlobalDateTime();

this.ChangedOnDate = UTCDateTime.ConvertToDate();

this.ChangedOnTime = UTCDateTime.GetTime();

 

if (this.CreatedOnDate.IsInitial()) {

    Trace.Info("I", "Created On not set");

    this.CreatedOnDate = this.ChangedOnDate;

    this.CreatedOnTime = this.ChangedOnTime;

}

 

5. Activate the customBO

 

6. Build the UI to display the data on the Thing Inspector, Quick View, and OWL

 

I used a Compound Field for the date and time fields

 

Screen Shot 2014-01-04 at 5.09.38 PM.png

 

Everything should be static text since a user should never edit these fields. The Changed By and Created By should show the "Name" of the user opposed to the ID which the BO stores. In step 2 where you add the associations to the BO, this will allow you to "join" the custom BO with the Business Partner BO so you can display the name stored in the Business Partner BO on the custom BO UI. The path of the user "Name" in the Business partner is the following

 

Root-> ChangedBy (this is the association you created in step 3) -> Common -> BusinessPartnerFormattedName

 

Do the same for CreatedBy as well.

 

*TIP* If you model your associations to the Business Partner in this way, this is the easiest way to display the name and addresses of any business partner on a custom BO without having to store it on the custom BO (you of course need to store the Business Partner ID so you can do the join between the custom BO and the Business Partner BO). The Business Partner BO stores data for Accounts, Contacts, Individual Customers, Partners, Partner Contacts, Employees for Cloud for Customer.

 

Screen Shot 2014-01-04 at 5.13.05 PM.png


Viewing all articles
Browse latest Browse all 10482

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>