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

  • Community Member Profile Picture
    on at
    RE: Plugin Problem

    6787.2018_2D00_12_2D00_27.png

  • Suggested answer
    Sreevalli Profile Picture
    3,256 on at
    RE: Plugin Problem

    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.

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Plugin Problem

    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

  • Verified answer
    Priyesh Profile Picture
    7,396 User Group Leader on at
    RE: Plugin Problem

    Can you paste here what error you're getting?

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans