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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Plugin Problem

(0) ShareShare
ReportReport
Posted on by

Hi,

  When I am try to generate autonumber.so i will wrote like this but it is not triggered then i will try to debug through profile then i have error.

2018_2D00_12_2D00_27.png

   And this is my code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace CRMPluginProject1
{
public class Class1 : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
try
{
Entity entity = (Entity)context.InputParameters["Target"];
QueryExpression stu = new QueryExpression("kbitsol_student");
stu.ColumnSet = new ColumnSet("kbitsol_name");
EntityCollection res = service.RetrieveMultiple(stu);
int totalreocrds = res.Entities.Count;
var finalrecord = totalreocrds + 1;
string prefix = "STU-DAV-00";
entity.Attributes["kbitsol_name"] = prefix + finalrecord;
}
catch (Exception ex)
{
Console.WriteLine("Autonumber Plugin not executed" + ex);
Console.ReadLine();
}

}
}
}

So please help.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Priyesh Profile Picture
    7,396 User Group Leader on at

    Can you paste here what error you're getting?

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi,

    It seems like connection with crm is not created. "Object reference does not have an instance of object".  Try to throw an exception in code.

    Hope it helps.

    Thanks

  • Suggested answer
    Sreevalli Profile Picture
    3,256 on at

    Hello,

    As per the below line, I can see syntax error.

    entity.Attributes["kbitsol_name"] = prefix + finalrecord;

    it should be

    entity["kbitsol_name"] = prefix + finalrecord; // preferred - override if there is an existing attributes in entity

    or

    entity.Attributes.Add["kbitsol_name"] = prefix + finalrecord; // in case you are adding new filed to entity attributes list

    Since, you haven't used "service.update" I believe it is been triggered on Pre-Create of your target entity. Otherwise you cannot see the update.

  • Community Member Profile Picture
    on at

    6787.2018_2D00_12_2D00_27.png

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans