Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Plug In Issue

(0) ShareShare
ReportReport
Posted on by 850

Hi All,

I have registered this code on create of contact entity. I am retrieving data from Auto Number Entity but not able to update in auto number entity. Below is my code. Please help

if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
if (context.Depth == 1)
{
int NumberFormat=0;
Entity enity = (Entity)context.InputParameters["Target"];
var entityLogicalName = enity.LogicalName;
Entity entityAutoNumber = new Entity("new_autonumbering");
//entityAutoNumber.Id=""
string fetchXML = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='new_autonumbering'>
<attribute name='new_autonumberingid' />
<attribute name='new_name' />
<attribute name='new_numberformat' />
<filter type='and'>
<condition attribute='new_name' operator='eq' value='" + entityLogicalName + @"' />
</filter>
</entity>
</fetch>";
EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchXML));
if (result.Entities.Count == 1)
{
foreach (var c in result.Entities)
{
var LatesAutoNumberValue = c.Attributes["new_numberformat"];
enity["new_contactid"] = Convert.ToString(LatesAutoNumberValue);
NumberFormat = Convert.ToInt32(c.Attributes["new_numberformat"]);
}
}
else
{
throw new InvalidPluginExecutionException("Auto Number increment is not configured for this Entity ot it has multiple record in Auto Number ");
}

NumberFormat = NumberFormat+1;
entityAutoNumber["new_numberformat"] = NumberFormat;
//service.Update(entityAutoNumber);
service.Update(enity);
}
}

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Plug In Issue

    Hi naZir,

    The Guid will be available once the create operation is completed.

    As the create is not completed yet the code is not able to access the GUID of your yet to be created record.

    But in post-operation the transaction is already completed and the GUID is available.

    So your code will work without any issue.

    Hope this helped.

    Thanks

    Phanidhar

  • naZir Profile Picture
    naZir 850 on at
    RE: Plug In Issue

    I have done in post opertaion. but i would like to know. in pre operation why my plug is not working

  • Verified answer
    naZir Profile Picture
    naZir 850 on at
    RE: Plug In Issue

    Thank you guyz for helping. i forgot to provide the guid of the record to update

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Plug In Issue

    Hi naZir,

    If I am correct you are trying create a record and at the time of creation you are assigning it an id using auto numbering.

    If you are trying to achieve this I would like to know how you registered your Plugin.

    If you registered this plugin on pre-operation the update will surely fail because the entity is not created yet.

    You can remove the Service.Update(entity) part and you can see that the creation will happen without any issue.

    Hope this helped.

    Thanks

    Phanidhar

  • naZir Profile Picture
    naZir 850 on at
    RE: Plug In Issue

    I am updating the contact id filed also with the value i am getting from fetchxml. and simultaneously updating field value in diff entity

  • naZir Profile Picture
    naZir 850 on at
    RE: Plug In Issue

    Entity Id must be specified for Update.. This Error message i am getting

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Plug In Issue

    Hi Nazir,

    please use the following code:

    CreateAttributeRequest request = new CreateAttributeRequest();
    request.EntityName = "contact";
    
    StringAttributeMetadata autonumbermd = new StringAttributeMetadata()
    {
    	AutoNumberFormat = "CNT-{DATETIMEUTC:yyyy}-{SEQNUM:5}-{RANDSTRING:5}",
    	SchemaName = "new_autonumber1",
    	MaxLength = 100,
    	RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.ApplicationRequired),
    	DisplayName = new Microsoft.Xrm.Sdk.Label("Contact autonumber", 1033),
    	Description = new Microsoft.Xrm.Sdk.Label("Contact autonumber", 1033)
    };
    
    request.Attribute = autonumbermd;
    CreateAttributeResponse response = organizationService.Execute(request);

    Please let us know.

    If you found the answer helpful, please mark as Verified 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • ashlega Profile Picture
    ashlega 34,477 on at
    RE: Plug In Issue

    You should be updating entityAutoNumber, not entity in the last line.

    Are there any errors, btw?

  • Suggested answer
    Nuno Profile Picture
    Nuno 2,005 on at
    RE: Plug In Issue

    Hi,

    Register the plugin step on pre-operation stage and in your code remove the verification if (context.Depth == 1), uncomment //service.Update(entityAutoNumber); and remove service.Update(enity);

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,307 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans