Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Plugin Problem

Posted on by Microsoft Employee

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
    Community Member Microsoft Employee on at
    RE: Plugin Problem

    6787.2018_2D00_12_2D00_27.png

  • Suggested answer
    Sreevalli Profile Picture
    Sreevalli 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
    Community Member Microsoft Employee 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
    Priyesh 7,392 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

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans