Java Script Task-9: On selection of asset in case entity read the project in asset project have project items with 1: N relation project items will have the case category and incident type combination records pick the combination and get the lowest price and update it in case?
Whenever you’re selecting the asset in case category you need to be full fill the case price based on the combination of incident type, category type. We have a relationship between asset to project , project to project Items in the project Items we have the combinations of Incident type, Case category with price, accordingly you need to be retrieve and show here.
In the case entity create a lookup of Asset entity and create one field as Price (Currency -datatype)
Create a Project entity.
· In the asset entity create a lookup of Project entity
Create Case category entity and Incident Type entity.
Create a project Items entity.
· In the project items entity create a look up Incident type entity, Case Category type entity and Project entity and create one currency field named as Price
· Create a 1: N relationship between project and project Items entity
Create combinations with different price in Price Item entity
Create One Empty Action and note down the action name
Open CRM Rest builder and choose as it is like how I choose option in below pic
Click on Create Request then CRM Rest builder provide below code:
var parameters = {};
var entity = {};
entity.id = "";
entity.entityType = "incident";
parameters.entity = entity;
var new_pluginactionRequest = {
entity: parameters.entity,
getMetadata: function() {
return {
boundParameter: "entity",
parameterTypes: {
"entity": {
"typeName": "mscrm.incident",
"structuralProperty": 5
}
},
operationType: 0,
operationName: "new_pluginaction"
};
}
};
Xrm.WebApi.online.execute(new_pluginactionRequest).then(
function success(result) {
if (result.ok) {
//Success - No Return Data - Do Something
}
},
function(error) {
Xrm.Utility.alertDialog(error.message);
}
);
Java script Code:
By using above (CRM Rest Builder provided code) we need to be write java script code
function actionPlugin(executionContext) {
var formContext = executionContext.getFormContext();
var currentRecordId = formContext.data.entity.getId().replace("{", "").replace("}", "");
var parameters = {};
var entity = {};
entity.id = currentRecordId;
entity.entityType = "incident";
parameters.entity = entity;
//above code prepares the parameters for the custom action,specifying the entity type and ID
var new_pluginactionRequest = {
entity: parameters.entity,
getMetadata: function () {
return {
boundParameter: "entity",
parameterTypes: {
"entity": {
"typeName": "mscrm.incident",
"structuralProperty": 5 //Here 5 for entity type
}
},
operationType: 0, // 0 means this is an action,1:function,2:CRUD
operationName: "new_pluginaction"
};
}
};
Xrm.WebApi.online.execute(new_pluginactionRequest).then(
function success(result) {
if (result.ok) {
//Success - No Return Data - Do Something
}
},
function (error) {
Xrm.Navigation.openAlertDialog(error.message);
}
);
}
Download Fetch XML code by using below condition
Fetch xml code:
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="effi_projectitems">
<attribute name="effi_projectitemsid"/>
<attribute name="effi_name"/>
<attribute name="createdon"/>
<order attribute="effi_name" descending="false"/>
<filter type="and">
<condition attribute="effi_project" operator="eq" uiname="Heat Sensor" uitype="effi_project" value="{9EC8549A-3CC6-EE11-9079-6045BDAD1DA3}"/>
</filter>
</entity>
</fetch>
Plugin code:
namespace plugin
{
public class scenario9 : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
tracer.Trace("before If condition");
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
{
try
{
tracer.Trace("entering in to try block");
EntityReference caseentityreference = (EntityReference)context.InputParameters["Target"];
Entity caseentity = service.Retrieve(caseentityreference.LogicalName, caseentityreference.Id, new ColumnSet(true));
EntityReference casecategory = caseentity.Contains("new_casecategory") ? (EntityReference)caseentity.Attributes["new_casecategory"] : null;
EntityReference incidentType = caseentity.Contains("new_incidenttype") ? (EntityReference)caseentity.Attributes["new_incidenttype"] : null;
Money price = caseentity.Contains("new_price") ? (Money)caseentity.Attributes["new_price"] : null;
EntityReference asset = caseentity.Contains("new_asset") ? (EntityReference)caseentity.Attributes["new_asset"] : null;
if (asset != null)
{
tracer.Trace("entering in asset if");
Entity assetentity = service.Retrieve(asset.LogicalName, asset.Id, new ColumnSet(true));
EntityReference project = assetentity.Contains("new_project") ? (EntityReference)assetentity.Attributes["new_project"] : null;
if (project != null)
{
tracer.Trace("entering the if project");
Entity projectentity = service.Retrieve(project.LogicalName, project.Id, new ColumnSet(true));
string fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='effi_projectitems'>" +
"<attribute name='effi_projectitemsid'/>" +
"<attribute name='effi_name'/>" +
"<attribute name='createdon'/>" +
"<order attribute='effi_name' descending='false'/>" +
"<filter type='and'>" +
"<condition attribute='effi_project' operator='eq' uiname='"+ project.Name +"' uitype='effi_project' value='"+ project.Id+ "'/>" +
"</filter>" +
"</entity>" +
"</fetch>";
tracer.Trace("below fetch xml");
EntityCollection projectitems = service.RetrieveMultiple(new FetchExpression(fetchXml));
List<decimal> pricevalue = new List<decimal>();
foreach (var items in projectitems.Entities)
{
tracer.Trace("entering into for each");
Entity P_items = service.Retrieve(items.LogicalName, items.Id, new ColumnSet(true));
EntityReference PIcasecategory = P_items.Contains("effi_casecategory") ? (EntityReference)P_items.Attributes["effi_casecategory"] : null;
EntityReference PIincidentType = P_items.Contains("effi_incidenttype") ? (EntityReference)P_items.Attributes["effi_incidenttype"] : null;
if ((PIcasecategory.Name == casecategory.Name) && (PIincidentType.Name == incidentType.Name ))
{
tracer.Trace("Entering into price if");
Money PIprice = P_items.Contains("effi_price") ? (Money)P_items.Attributes["effi_price"] : null;
pricevalue.Add(PIprice.Value);
}
}
decimal minValue = pricevalue.Min();
caseentity["new_price"] = new Money(minValue);
service.Update(caseentity);
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}
}
}
Open plugin Registration Tool and create one new assembly and add step
Output: Whenever you are giving the same combinations in case entity whatever previously given in Project items entity then what is lowest price in project items entity that price value will be updated automatically in case entity price field
Comments
Post a Comment