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)

how get the salesorderdetail productid

(0) ShareShare
ReportReport
Posted on by

hi im mani

plz help me i try to get data from entity .

the target entity is salesorderdetail and i want to get the Productid !

how can i do it from code activity

best regard

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    BackToTheCrm Profile Picture
    on at

    Hello Mani,

    Here is a simple way (C#) to get the Guid of product :

    Guid productId = salesOrderDetail.GetAttributeValue<EntityReference>("productid").Id; // Assuming salesOrderDetail is an instance of Entity and was populated before

  • Suggested answer
    Surmeet Singh Profile Picture
    3,075 on at

    Hi Mani,

    You can do a service.retrieve using the PrimaryentityId or use the PostImage to get the value of the product and the get the value as specified by "BacktoTheCRM"

  • Mahendar Pal Profile Picture
    45,095 on at

    Hello,

    You can get data using id or any other condition.

    If you have salesorderid, then you can use retrieve method and get data based on it's id, as suggested.

    If you want to get salesorder details using other condition then you can use retrievemultiple method.

    Please refer below SDK: msdn.microsoft.com/.../gg328416.aspx

    If you have already written code and facing issue in it, you can share it here for inquiry

  • Suggested answer
    Community Member Profile Picture
    on at

    Hello Mani,

    You can retrieve the ProductID from SalesOrder entity with the a fetchxml which is described in the following article:

    + CRM 2013 C# Plugin - Fetch XML error| community.dynamics.com/.../159468

    Hope it helps.

    Best Regards,

  • Community Member Profile Picture
    on at

    tnx "BacktoTheCRM" it will work but when i try to convert it to string and check it by one of item in fetchxml give me this error

    "Object reference not set to an instance of an object"

    Mani

  • Mahendar Pal Profile Picture
    45,095 on at

    Share your code here for inquiry

  • Community Member Profile Picture
    on at
    public class ProductAggregate : CodeActivity
        {
            [Output("Result")]
            public OutArgument<decimal> Result { get; set; }
    
            [Output("Description")]
            public OutArgument<string> Description { get; set; }
    
            protected override void Execute(CodeActivityContext executionContext)
            {
                IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
                IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);
                if (context.Depth == 1)
                {
                    try
                    {
                        
                        if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                        {
                            Entity entity = (Entity)context.InputParameters["Target"];
                            Guid productId = entity.GetAttributeValue<EntityReference>("productid").ToString(); 
                        }
    
    
                        string query;
                        query =
                              "<fetch distinct='false' mapping='logical' aggregate='true'>"
                            + "    <entity name='salesorderdetail'>"
                            + "       <attribute name='quantity' alias='quantity_sum' aggregate='sum'/>"
                            + "       <filter type='and'>"
                            + "            <condition attribute='salesorderstatecode' operator='eq' value='0' />"
                            + "            <condition attribute='productid' operator='eq' value='" + productId + "' />"
                            + "       </filter>"
                            + "    </entity>"
                            + "</fetch>";
                        Entity summary = service.RetrieveMultiple(new FetchExpression(String.Format(query, productId))).Entities.FirstOrDefault();
                        decimal salesOrderDetailSum;
                        if (summary == null)
                        {
                            salesOrderDetailSum = 0; 
                        }
                        else
                        {
                            salesOrderDetailSum = (decimal)((AliasedValue)summary["quantity_sum"]).Value;
                        }
                            
                        Result.Set(executionContext, salesOrderDetailSum);
                        Description.Set(executionContext, "OK");
                    }
                    catch (Exception e)
                    {
                        Description.Set(executionContext, (e.InnerException ?? e).Message);
                        throw e.InnerException ?? e;
                    }
                }
            }
        }
  • BackToTheCrm Profile Picture
    on at

    Mani, You must check the data retrieved for null and get the Id property :

    EntityReference productReference = entity.GetAttributeValue<EntityReference>("productid");
    Guid? productId = null;
    if(productReference != null)
    {
        productId = productReference.Id;
    } 

    If you want the Id in string, use ToString() on productId, not productReference.

    If the productReference is null, maybe it's a product entered manually (not in the product catalog).

  • Community Member Profile Picture
    on at

    tnx but you tell me field of record in salesorderdetail can be null ? in this case you say the product reference can be null but it cant !

    and if the product not in the catalog how can i set it in order ?

    i'm confused

  • BackToTheCrm Profile Picture
    on at

    An user can choose (if he/she has the right to do so) to manually specify a product.

    These 3 fields are involved in the process :

    • IsProductOverridden : False => An existing product is used (ProductId is not null, ProductDescription is null), True => A Write-In product is used (ProductId is null, ProductDescription is not null)
    • ProductId : For existing product
    • ProductDescription : For manually specified product (Write-In product)

    As you can see the ProductId can be null therefore you'll need to check it for null.

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