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 :
Service | Customer Service, Contact Center, Fie...
Unanswered

Post create plugin throws error: Salesorder id does not exist

(0) ShareShare
ReportReport
Posted on by 165

Hello,

I've made a plugin in order to automatically create an order detail when an order is created.

Here is my code:

            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {

                Entity Order = (Entity)context.InputParameters["Target"];


                IOrganizationServiceFactory serviceFactory =
                    (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                try
                {
                    Guid caseSerialNumber = Guid.Empty;
                    Guid orderId = Order.Id;

                    if (Order.GetAttributeValue("zst_case") != null)
                    {
                        caseSerialNumber = Order.GetAttributeValue("zst_case").Id;

                        QueryExpression query = new QueryExpression("zst_parttask");
                        query.ColumnSet = new ColumnSet("zst_product", "zst_quantity", "zst_unit");
                        query.Criteria.AddCondition("regardingobjectid", ConditionOperator.Equal, caseSerialNumber);

                        EntityCollection collection = service.RetrieveMultiple(query);

                        if (collection.Entities.Count > 0)
                        {

                            foreach (Entity product in collection.Entities)
                            {
                                Entity orderProduct = new Entity("salesorderdetail");

                                orderProduct["salesorderid"] = new EntityReference("salesorder", orderId);

                                if (product.Attributes.Contains("zst_quantity"))
                                {
                                    var inputQuantity = product.GetAttributeValue("zst_quantity");
                                    orderProduct.Attributes.Add("quantity", Convert.ToDecimal(inputQuantity));
                                }

                                if (product.Attributes.Contains("zst_product"))
                                {

                                    EntityReference taskProduct = (EntityReference)product.Attributes["zst_product"];

                                    var LookupId = taskProduct.Id;
                                    var logicalName = taskProduct.LogicalName;
                                    orderProduct["productid"] = new EntityReference(logicalName, LookupId);

                                }

                                if (product.Attributes.Contains("zst_unit"))
                                {
                                    EntityReference taskUnit = (EntityReference)product.Attributes["zst_unit"];
                                    var LookupIdUnit = taskUnit.Id;
                                    var logicalNameUnit = taskUnit.LogicalName;
                                    orderProduct["uomid"] = new EntityReference(logicalNameUnit, LookupIdUnit);
                                }

                                bool _price = false;
                                bool _product = false;

                                product["ispriceoverridden"] = _price;
                                product["isproductoverridden"] = _product;

                                service.Create(orderProduct);
                            }
                        }
                    }
                }

The plugin step is on Create of Order (Post operation, I've tried synchronously and asynchronously)

When I use the plugin profiler to debug it, I get the error "Salesorder id does not exist"

Anh help would be appreciated

I have the same question (0)
  • irenegr Profile Picture
    165 on at

    Still need help with this...

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 > Service | Customer Service, Contact Center, Field Service, Guides

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 55 Super User 2025 Season 2

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 27 Most Valuable Professional

#3
Soundari Profile Picture

Soundari 15

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans