Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Unanswered

Dynamic CRM Plugins

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;
}

}
}

 

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans