-
Notifications
You must be signed in to change notification settings - Fork 3
OperationBase Class
We develop our framework with the mindset of getting the code clean. That is why we divide the plugin components into 2 classes. One is business logic class, the second one is the caller which will be the plugin itself.
Making business logic (OperationBase) class is very easy, you just need to inherit from OperationBase class. By default, IntelliSense will ask you to implement the constructor with ITransactionContext and HandleExecute method. You can set as early-bound class or late-bound class. The code below is referring for early-bound sample:
using Demo.Entities;
using Niam.XRM.Framework;
using Niam.XRM.Framework.Interfaces.Plugin;
using Niam.XRM.Framework.Plugin;
namespace Demo.Plugins.Business
{
public class SetTransactionName : OperationBase<cr506_transaction>
{
public SetTransactionName(ITransactionContext<cr506_transaction> context) : base(context)
{
}
protected override void HandleExecute()
{
var name = GetCustomerName();
var dateFormat = GetDateString();
Set(e => e.cr506_name, name + " - " + dateFormat);
}
private string GetCustomerName()
{
var customerRef = Get(e => e.cr506_customerid);
if (customerRef == null) return "";
var name = customerRef.LogicalName == Account.EntityLogicalName
? Service.GetReferenceName<Account>(customerRef)
: Service.GetReferenceName<Contact>(customerRef);
return name;
}
private string GetDateString()
{
var transactionDate = GetValue(e => e.cr506_transactiondate);
var dateFormat = transactionDate.ToString("dd MMMM yyyy");
return dateFormat;
}
}
}
Group of collection of methods or extensions that helps you to Get or Set value of an attribute.
Get value of an attribute.
// Late Bound Sample
var transaction = Service.Retrive("new_transaction", transactionId, new ColumnSet("new_transactiondate"));
var transactionDate = transaction.Get<DateTime?>("new_transactiondate");
// Early Bound Sample
var transaction = Service.Retrieve(new_transaction.EntityLogicalName,
transactionId,
new ColumnSet<new_transaction>(e => e.new_transactiondate)).toEntity<new_transaction>();
var transactionDate = transaction.Get(e => e.new_transactiondate);
Get the value of an object. If null, return the default value. Useful if you have logic like checking boolean value, datetime, etc.
var transaction = Service.Retrieve(new_transaction.EntityLogicalName,
transactionId,
new ColumnSet<new_transaction>(e => e.new_transactiondate,
e => e.new_isactive)).toEntity<new_transaction>();
// Will return DateTime.MinValue
var transactionDate = transaction.GetValue(e => e.new_transactiondate);
// If empty, return 1 January 2020
var trnasactionDate2 = transaction.GetValue(e => e.new_transactiondate, new DateTime(2020, 01, 01));
// Will return false if empty
var isActive = transaction.GetValue(e => e.new_isactive);
// If empty, return true
var isActive2 = transaction.GetValue(e => e.new_isactive, true);
Set value of an attribute.
// Late Bound Sample
var transactionDate = new DateTime(2020, 01, 01);
var updateTransaction = new Entity("new_transaction") { Id = transactionId}
.Set("new_transactiondate", transactionDate);
Service.Update(updateTransaction);
// Early Bound Sample
var transactionDate = new DateTime(2020, 01, 01);
var updateTransaction = new new_transaction { Id = transactionId}
.Set(e => e.new_transactiondate, transactionDate);
Service.Update(updateTransaction);
Object / Method | Same Like | Description |
---|---|---|
Get | Context.Current.Get | For getting value of an attribute. |
Set | Context.Current.Set or Context.Input.Set | For setting value of Target attribute. |
Service | Context.Service | Returning current IOrganizationService Object. |
Context | - | Returning ITransactionContext Object. |
Context.SystemService | - | Returning IOrganizationService on behalf as Administration (Highest Privilige). |
Wrapper | - | Returning EntityWrapper of current Entity. |
Wrapper.Id | Context.Input.Id or Context.Current.Id or Context.Initial.Id | Return Id of current Entity. |
Context.PluginExecutionContext | - | Returning IPluginExecutionContext object. |