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)

Change Plugin to update instead of create

(0) ShareShare
ReportReport
Posted on by

Hi All after a bit of guidance please. I have a plugin the creates a new price list that was in it's own entity i have now decided to move it into the price list entity but it does create a change in what the plugin is doing. The current plugin creates a new price list based on the fields filled out in it's entity but when i move the plugin to the price list entity it will be dong and update to the price list adding if required and changing amounts if required. Below is the price part of my plugin what would i have to change to make this an update plugin and not a create 

           //Create the context

            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();

            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();

            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            // Create a column set to define which attributes should be retrieved.

            ColumnSet attributes = new ColumnSet(true);

            Entity currentPriceLevelEntity = new Entity("pricelevel");

            currentPriceLevelEntity = service.Retrieve("pricelevel", CurrentPriceList.Get<EntityReference>(executionContext).Id, attributes);

            // Create New Price List

            Entity newPriceLevel = new Entity("pricelevel");

            newPriceLevel["name"] = NewPriceListName.Get<string>(executionContext);

            newPriceLevel["sb_fpaid"] = Contractor.Get<string>(executionContext);

            newPriceLevel["sb_merchant"] = Merchant.Get<string>(executionContext);

            newPriceLevel["transactioncurrencyid"] = currentPriceLevelEntity["transactioncurrencyid"];

            Guid newPriceLevelId = service.Create(newPriceLevel);

            newPriceLevel["Price900"] = ManholePrice900.Get<string>(executionContext);

            newPriceLevel["Price1050"] = ManholePrice1050.Get<string>(executionContext);

            newPriceLevel["Price1200"] = ManholePrice1200.Get<string>(executionContext);

            newPriceLevel["Price1350"] = ManholePrice1350.Get<string>(executionContext);

            newPriceLevel["Price1500"] = ManholePrice1500.Get<string>(executionContext);

            newPriceLevel["Price1800"] = ManholePrice1800.Get<string>(executionContext);

            newPriceLevel["Price2100"] = ManholePrice2100.Get<string>(executionContext);

Kind Regards

Dan

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    I don't understand what you mean by real code. That was the real code but i just trimmed down to where i thought the actual information that is useful is. I have add more of the code below but after the last line it's just adding the data so not much use really.

    The simplest way to explain is that currently the user goes to a entity i created fills in the information and then clicks save which triggers the creation of a new price list based on a few bits of information the user has provided. basically three inputed prices can give the values of 50 parts

    I would like to move this into the price list entity so the user can add the above information to an existing price list saving the need for multiple price lists for the same account  

    Hope that makes more sense. Please see more of the code below. Thank you for your help and time in replying

    Dan

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Query;

    using Microsoft.Xrm.Sdk.Workflow;

    using System.Activities;

    using System;

    namespace pluginprofile

    {

       public class Updatepricelist : CodeActivity

       {

           protected override void Execute(CodeActivityContext executionContext)

           {

               //Create the tracing service

               ITracingService tracingService = executionContext.GetExtension<ITracingService>();

               //Create the context

               IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();

               IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();

               IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

               // Create a column set to define which attributes should be retrieved.

               ColumnSet attributes = new ColumnSet(true);

               Entity currentPriceLevelEntity = new Entity("pricelevel");

               currentPriceLevelEntity = service.Retrieve("pricelevel", CurrentPriceList.Get<EntityReference>(executionContext).Id, attributes);

               // Create New Price List

               Entity newPriceLevel = new Entity("pricelevel");

               newPriceLevel["name"] = NewPriceListName.Get<string>(executionContext);

               newPriceLevel["sb_fpaid"] = Contractor.Get<string>(executionContext);

               newPriceLevel["sb_merchant"] = Merchant.Get<string>(executionContext);

               newPriceLevel["transactioncurrencyid"] = currentPriceLevelEntity["transactioncurrencyid"];

               Guid newPriceLevelId = service.Create(newPriceLevel);

               newPriceLevel["Price900"] = ManholePrice900.Get<string>(executionContext);

               newPriceLevel["Price1050"] = ManholePrice1050.Get<string>(executionContext);

               newPriceLevel["Price1200"] = ManholePrice1200.Get<string>(executionContext);

               newPriceLevel["Price1350"] = ManholePrice1350.Get<string>(executionContext);

               newPriceLevel["Price1500"] = ManholePrice1500.Get<string>(executionContext);

               newPriceLevel["Price1800"] = ManholePrice1800.Get<string>(executionContext);

               newPriceLevel["Price2100"] = ManholePrice2100.Get<string>(executionContext);

               newPriceLevel["Price2400"] = ManholePrice2400.Get<string>(executionContext);

               newPriceLevel["Price2700"] = ManholePrice2700.Get<string>(executionContext);

               newPriceLevel["Price3000"] = ManholePrice3000.Get<string>(executionContext);

               newPriceLevel["StepPriceVal"] = StepsPrice.Get<string>(executionContext);

               newPriceLevel["NoStepVal"] = NoStep.Get<string>(executionContext);

               newPriceLevel["SoakAwayVal"] = Soakaway.Get<string>(executionContext);

               decimal costamount;

               decimal price900int = Convert.ToDecimal(newPriceLevel["Price900"]);

               decimal price1050int = Convert.ToDecimal(newPriceLevel["Price1050"]);

               decimal price1200int = Convert.ToDecimal(newPriceLevel["Price1200"]);

               decimal price1350int = Convert.ToDecimal(newPriceLevel["Price1350"]);

               decimal price1500int = Convert.ToDecimal(newPriceLevel["Price1500"]);

               decimal price1800int = Convert.ToDecimal(newPriceLevel["Price1800"]);

               decimal price2100int = Convert.ToDecimal(newPriceLevel["Price2100"]);

               decimal price2400int = Convert.ToDecimal(newPriceLevel["Price2400"]);

               decimal price2700int = Convert.ToDecimal(newPriceLevel["Price2700"]);

               decimal price3000int = Convert.ToDecimal(newPriceLevel["Price3000"]);

               decimal steppriceint = Convert.ToDecimal(newPriceLevel["StepPriceVal"]);

               decimal nostepint = Convert.ToDecimal(newPriceLevel["NoStepVal"]);

               decimal Soakawayint = Convert.ToDecimal(newPriceLevel["SoakAwayVal"]);

               // Get current product price level

               QueryExpression productPriceLevelExpression = new QueryExpression("productpricelevel");

               FilterExpression productPriceLevelFilterExpression = new FilterExpression();

               productPriceLevelFilterExpression.Conditions.Add(new ConditionExpression("pricelevelid", ConditionOperator.Equal, currentPriceLevelEntity["pricelevelid"]));

               productPriceLevelExpression.ColumnSet = new ColumnSet(true);

               productPriceLevelExpression.Criteria = productPriceLevelFilterExpression;

               EntityCollection productPriceLevelList = service.RetrieveMultiple(productPriceLevelExpression);

               // Create new product price level records

               for (int index = 0; productPriceLevelList.Entities != null && index < productPriceLevelList.Entities.Count; index++)

               {

                   Entity newProductPriceLevelEntity = new Entity("productpricelevel");

                   newProductPriceLevelEntity["pricelevelid"] = new EntityReference("pricelevel", newPriceLevelId);

                   newProductPriceLevelEntity["productid"] = productPriceLevelList.Entities[index]["productid"];

                   newProductPriceLevelEntity["uomid"] = productPriceLevelList.Entities[index]["uomid"];

                   string prodidse = newProductPriceLevelEntity.GetAttributeValue<EntityReference>("productid").Name;

                   //900 ring price makeup

                   if (prodidse == "F0900 TAG VMHS F")

                   {

                       costamount = price900int;

                       goto endt;

                   }

                   else if (prodidse == "F0900 TAG VMHS T")

                   {

                       costamount = price900int / 4 * 3;

                       goto endt;

  • Suggested answer
    Nadeeja Bomiriya Profile Picture
    6,804 on at

    Hi Dandare2020,

    By analysing the code, it looks like a custom workflow activity without any inputs.  You can create a workflow which triggers on update of the PriceList and add this as a STEP in the workflow.

  • Community Member Profile Picture
    on at

    Thank you for your response.

    I tried to do that but i came up with an error that i was trying to create a duplicate price list which in theory i was because i was trying to add the data to a previous price list. What i'm after doing is basically turning this   Entity newPriceLevel = new Entity("pricelevel"); to an update of the price list instead of creating a new one.

    Regards

    Dan

  • Verified answer
    tw0sh3ds Profile Picture
    5,600 on at

    Hi,

    if you want to update element you must just set its ID and call update instead of Create. So you should do newPriceLevel.Id = (id of pricelist you want to update) and then instead of service.Create(newPricelevel) you should call service.Update(newPriceLevel).

  • Community Member Profile Picture
    on at

    OMG

    Thank you so much i feel a bit silly now soon as you pointed that out i saw the answer can't believe i missed it.

    Your help is greatly appricated

    Thanks

    Dan

  • Community Member Profile Picture
    on at

    Hi Thank you for your help i think lack of sleep (New baby) or Monday morning has turned my brain to mush. From what i can figure i'm changing the below to look like what i've got but i seems wrong to me

               Entity newPriceLevel = new Entity("pricelevel");

    newPriceLevelId = PriceListId.Get<string>(executionContext);

               newPriceLevel["name"] = NewPriceListName.Get<string>(executionContext);

               newPriceLevel["sb_fpaid"] = Contractor.Get<string>(executionContext);

               newPriceLevel["sb_merchant"] = Merchant.Get<string>(executionContext);

               newPriceLevel["transactioncurrencyid"] = currentPriceLevelEntity["transactioncurrencyid"];

               Guid newPriceLevelId = service.Update(newPriceLevel);

    I seem to be getting an error at this line Guid newPriceLevelId = service.Update(newPriceLevel);

    the error is cannot implicitly convert type void to system.GUID

    Many Thanks

    Dan

  • Suggested answer
    tw0sh3ds Profile Picture
    5,600 on at

    Hi,

    This line:

    newPriceLevelId = PriceListId.Get<string>(executionContext);

    Should look like that:

    newPriceLevel.Id = Guid.Parse(PriceListId.Get<string>(executionContext));

    and of course you have to make sure that PriceListId.Get<string>(executionContext) is filled out with the proper string (guid)

    and this line:

    Guid newPriceLevelId = service.Update(newPriceLevel);

    makes not sense, in Update you are not getting any value, so just change it to 

    service.Update(newPriceLevel);

  • Community Member Profile Picture
    on at

    That seems pretty simple and i thought getting the GUID would be but it is causing issues.

    I guess the pricelevelid field is what i'm after but i can't get that in the workflow or to update a custom field so i can just point the Pricelistid at it in the workflow set properties.

    Do i need to add this in the plugin so use something like

    String priceguid = entity.id

    Cheers

    Dan

  • Community Member Profile Picture
    on at

    I now have that working just a quick question how can i get the plugin to over write the price if it exists

    Regards

    Dan

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