web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Dynamic CRM Plugins

(0) ShareShare
ReportReport
Posted on by 5

I have to create one account in dynamics app and my task is to get the account number which contain [company name and party numeric data type].

There is relational between account to company[N:1] and account to party[N;1] so on account table i have two lookups(company,party) so my task it to fetch the party number from the party entity(only want numeric data type) and company name and merge it into account number and display. I have written the code but it showing the error :- Unable to cast object of type 'Microsoft.Xrm.Sdk.EntityReference' to type 'System.String'.  here is my code please please help.......................

PostCreate.cs file this is below.........

using System;
using System.Collections.Generic;
using System.IdentityModel.Metadata;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using GlobalAddressBook.pluginD365;
using Microsoft.Xrm.Sdk;
using static System.Net.Mime.MediaTypeNames;

namespace GlobalAddressBook.pluginD365
{
public class PostCreate : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = factory.CreateOrganizationService(context.UserId);
var trackingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
if (context.MessageName.ToLower().Equals("create") && context.Stage == 40)
{
Entity account = (Entity)context.InputParameters["Target"];
string posttopic = string.Empty;
string pretopic = string.Empty;
EntityReference party = null;
EntityReference company = null;
if (account.LogicalName == "account")
{
try
{
Entity acc = new Entity("account");

//List<EntityReference> party_number = new List<EntityReference>();

//List<EntityReference> company_name = new List<EntityReference>();
if (account.Attributes.Contains("hso_party"))
{
if (account.GetAttributeValue<string>("hso_party") != "" && account.GetAttributeValue<EntityReference>("hso_party") != null)
{
party = account.GetAttributeValue<EntityReference>("hso_party");
pretopic = party.ToString();
}
}
if (account.Attributes.Contains("hso_company"))
{
if (account.GetAttributeValue<string>("hso_company") != "" && account.GetAttributeValue<EntityReference>("hso_company") != null)
{
company = account.GetAttributeValue<EntityReference>("hso_company");
//posttopic = ((EntityReference)account.GetAttributeValue<EntityReference>("hso_company");
//company = ((EntityReference)account.GetAttributeValue<EntityReference>("hso_company");
posttopic = company.ToString();
}
}


BLAccount bLAccount = new BLAccount();

string accountNumber= bLAccount.GenerateAccountAutonumber(service,party.Id,company.Id);

// acc["accountnumber"] = party + company;
//service.Create(acc);
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("invalidException", ex);
}

}
}
}
}
}
}

BusinessLogic.cs :- This is the business logic

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Services.Description;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace GlobalAddressBook
{
internal class BLAccount
{
public string GenerateAccountAutonumber(IOrganizationService service,Guid partyId, Guid companyId)
{
Entity party = service.Retrieve("hso_party", partyId, new ColumnSet("hso_partynumber"));
Entity company = service.Retrieve("hso_company", companyId, new ColumnSet("hso_name"));

//party-1000001
var partyNumber = party.GetAttributeValue<string>("hso_partnumber");
string partyNumberNumeric = string.Empty;
if(!string.IsNullOrEmpty(partyNumber) )
{
string[] partyNumberSplitted = partyNumber.Split('-');
}
var companyCode = company.GetAttributeValue<string>("hso_name");

string accountNumber = (!string.IsNullOrEmpty(partyNumberNumeric) && !string.IsNullOrEmpty(companyCode)) ? companyCode + partyNumberNumeric : string.Empty;
return accountNumber;
}

}
}

 

I have the same question (0)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 125

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 110 Super User 2025 Season 2

#3
Erin Lubben Profile Picture

Erin Lubben 73

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans