Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

plugin doesn't update

Posted on by 2,397

Hi, I created an update plugin, when I do the debugging , I can see the attributes get updated, but it doesn't reflect on the form or in the database. did I miss anything?

protected override void ExecuteCrmPlugin(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new InvalidPluginExecutionException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService service = localContext.OrganizationService;

            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {
                Entity entity = (Entity)context.InputParameters["Target"];
                if (entity.LogicalName == "contact")
                {
                    Entity Contact = service.Retrieve("contact", entity.Id, new ColumnSet(true));
                    if (Contact.Attributes.Contains(" businessregistrationid") == true)
                    {
                        String contactid = entity.Id.ToString();
                        string registraionid = Contact.Attributes[" businessregistrationid"].ToString();
                        string fetchxml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                            <entity name='contact'>
                                            <attribute name=' businessregistrationid' />
                                            <attribute name=' brokerofrecord' />
                                            <attribute name='contactid' />
                                            <filter type='and'>
                                                <condition attribute=' businessregistrationid' operator='eq' value='" + registraionid + @"'/>
                                            </filter>
                                            </entity>
                                           </fetch>";
                        EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchxml));

                        foreach (var c in ec.Entities)
                        {
                            if (c.Attributes["contactid"].ToString() == contactid)
                            {
                                c.Attributes[" brokerofrecord"] = 1;
                            }
                            else
                            {
                                c.Attributes[" brokerofrecord"] = 0;
                            }

                        }
                    }

                }
                else { throw new InvalidPluginExecutionException("The account number can only be set by the system."); }
            }
        }


*This post is locked for comments

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: plugin doesn't update

    You still need to prevent the same plugin from running for the same record again.

    Since you have this condition:

    if (Contact.Attributes.Contains(" businessregistrationid") == true)

    you should be fine as long as you don't include businessregistrationid to the update in your loop. However, it'll be included if you leave that code as is since that attribute is added to the fetchxml.

    If, instead, you exclude it from the update by using that approach with "updated" entity above, there should be no recursion.

    Or you can always add

    if(context.Depth > 1) return;

    at the beginning of the plugin

  • sdnd2000 Profile Picture
    sdnd2000 2,397 on at
    RE: plugin doesn't update

    This is to update the whole records with the same ID, not just the selected record. Regarding the prefix, I have updated prefix, it should not affect the update method.

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: plugin doesn't update

    What's the shema name though?

    Somebody mentioned recoserv_brokerofrecord above, so you probably need to add recoserv_ prefix anyway (and handle that recursion as well)

  • Suggested answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: plugin doesn't update

    Besides, you probably have a recursion there.

    You are selecting all the contacts using a condition - I'm wondering if the original contact gets selected as well (in which case you are updating it in the for loop as well, and that causes another update and so on)

    There was a suggestion above - you should probably change the udpate to work like this:

    var updated = new Entity("contact");

    updated.Id = c.Id;

    updated["...brokerofrecord"] = ... (this goes under if/else)

    service.Update(updated);

  • sdnd2000 Profile Picture
    sdnd2000 2,397 on at
    RE: plugin doesn't update

    Hi, Alex,

    I removed all the space, it gets the same error when executing the update method.

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: plugin doesn't update

    What are those attributes - I don't see them in the cotnact entity?

    <attribute name=' businessregistrationid' />

    <attribute name=' brokerofrecord' />

    You don't have any prefix for them either.. and there is that extra space

    '<THERE IS A SPACE HERE>businessregistrationid'

  • sdnd2000 Profile Picture
    sdnd2000 2,397 on at
    RE: plugin doesn't update

    I got this error "OrganizationServiceFault ", when updating the attributes.

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: plugin doesn't update

    You seem to be modifying attributes in each of the entities in that for loop, so you need to call an update for each of them (not for the contact)

  • sdnd2000 Profile Picture
    sdnd2000 2,397 on at
    RE: plugin doesn't update

    Thanks so much. Can we put the update method outside the loop? like the below

     foreach (var c in ec.Entities)
                            {
                                if (c.Attributes["contactid"].ToString() == contactid)
                                {
                                    c.Attributes["recoserv_brokerofrecord"] = true;
                                                         }
                                else
                                {
                                    c.Attributes["recoserv_brokerofrecord"] = false;
                                }
    
                            }
    
    UpdateRequest updateContact = new UpdateRequest()
                            {
                                Target = Contact,
                            };
                            service.Execute(updateContact);


  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: plugin doesn't update

    Hi,

    You should call the IOrganisationService.Update method in order to get the record updated in forms or in the database.

    Try the code below.

                EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchxml));
                foreach (var c in ec.Entities)
                {
                    if (c.Attributes["contactid"].ToString() == contactid)
                    {
                        c.Attributes[" brokerofrecord"] = 1;
                        service.Update(c);
                    }
                    else
                    {
                        c.Attributes[" brokerofrecord"] = 0;
                        service.Update(c);
                    }
    
                }

    See: msdn.microsoft.com/.../microsoft.xrm.sdk.iorganizationservice.update.aspx

    Hope this helps.

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