Dear community,
I'm looking for how to generate a name for a record using server-side logic.
Something similar to this in the academy: Auto-Numbering
An example would be to add a name field on a page that has an 'Account' lookup and a 'Created on' date field. Whenever a record is created, we generate 'AccountName - TodaysDate'.
What would be the best way to do this?
Kind regards,
Yosef
Like
There are a couple of approaches you could take. First is to generate this name when the page opens in the onEntityInitialized function if the page is in "add" mode (assuming the Account would already be populated. If not, you could do it on the change of the Account lookup. You can see more about wiring up a change event for the Account field here https://customerfx.com/article/triggering-an-event-when-a-field-is-chan…
Something like:
onEntityInitialized: function() { this.callParent(arguments); if (this.isAddMode()) { var account = this.get("UsrAccount"); if (account) { this.set("UsrName", account.displayValue + " " + new Date().toLocaleDateString()); } } }
An alternative approach would be to do it just before the record is saved. Something like this:
save: function() { var account = this.get("UsrAccount"); if (account) { this.set("UsrName", account.displayValue + " " + new Date().toLocaleDateString()); } this.callParent(arguments); }
As a side note, this article might be some help to understand overriding these types of functions https://customerfx.com/article/getting-started-with-writing-code-for-bp…
Ryan
I just noticed you mention doing this server side after posting my last response.
To do it server-side, you'll create an entity sub process for the entity, similar to how you do it in the article for generating auto numbers.
All you'd need is a script task with code something like this:
var accountId = Entity.GetTypedColumnValue<Guid>("UsrAccountId"); var account = UserConnection.EntitySchemaManager.GetInstanceByName("Account").CreateEntity(UserConnection); if (account.FetchFromDB(accountId)) { var name = account.GetTypedColumnValue<string>("Name"); Entity.SetColumnValue("UsrName", name + " " + DateTime.Now.ToShortDateString()); }
Hope this helps.
Ryan
Hello Yosef,
Also a little example from me (additional "thank you!" to Ryan for sharing both server and client side logic examples):
this should be added to the object sub-process methods:
public void GenerateRecordName() { DateTime currentDateTime = DateTime.Now; string recordName = currentDateTime.ToString(); string accountName = GetAccountCaption(); if (accountName != String.Empty) { recordName = accountName + " - " + recordName; } Entity.SetColumnValue("UsrName", recordName); } public string GetAccountCaption() { var accountId = Entity.GetTypedColumnValue<Guid>("UsrAccountId"); var accountQuery = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "Account"); accountQuery.AddColumn("Name"); var account = accountQuery.GetEntity(UserConnection, accountId); if (account == null) { return string.Empty; } return account.GetTypedColumnValue<string>("Name"); }
and here is the screenshot of the process (don't forget to enable the "record inserting" event in the object and publish it):
Best regards,
Oscar
Hi,
If you have an assembly package, the server-side scenario requires an EntityEventListener