Skip to main content

Notifications

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

  • Mahendar Pal Profile Picture
    45,095 on at
    RE: how get the salesorderdetail productid

    Hi Mani,

    You can simply get product id like below in your workflow and use it:

    Guid productid=context.PrimaryEntityId;

    And first you not sure when you want to run your workflow and do the calculation, but for me you should write plugin, on the orderproduct as i suggested in earlier thread,

    And it is always better to first test your code in console application, easy to write, easy to debug, once everything is ok you should use your code for plugin/workflow.

    Hope it will help

  • Community Member Profile Picture
    on at
    RE: how get the salesorderdetail productid

    ok tnx i well read the article

  • BackToTheCrm Profile Picture
    on at
    RE: how get the salesorderdetail productid

    As I said before, in this case the error must be somewhere else. The "Object reference not set to an instance of an object" error means that you try to access to an object which haven't been initialized.

    To help you debug your code, I suggest you to check this article : msdn.microsoft.com/.../gg328574.aspx

    Look for Logging and Tracing.

  • Community Member Profile Picture
    on at
    RE: how get the salesorderdetail productid

    it is my fetchxml

    <fetch distinct='false' mapping='logical' >
        <entity name='salesorderdetail'>
    	   <attribute name='productid' alias='productid' />
    	   <attribute name='salesorderid' alias='salesorderid' />
        </entity>
    </fetch>


    and this is sum of result

    <resultset morerecords="0" paging-cookie="&lt;cookie page=&quot;1&quot;&gt;&lt;salesorderdetailid last=&quot;{F2087FB8-9636-E511-828D-D6B1CC7458B0}&quot; first=&quot;{36992D2E-D638-E511-80C8-00155D010109}&quot; /&gt;&lt;/cookie&gt;">
      <result>
        <productid dsc="" name="Core Prime G360H" type="1024">{9434ED3F-082B-E511-80C8-00155D010109}</productid>
        <salesorderid type="1088">{44072F7F-3538-E511-80C8-00155D010109}</salesorderid>
        <salesorderdetailid>{36992D2E-D638-E511-80C8-00155D010109}</salesorderdetailid>
      </result>
      <result>
        <productid dsc="" name="S5 G900H" type="1024">{6DEC7AFD-BD2E-E511-80C8-00155D010109}</productid>
        <salesorderid type="1088">{90CA7E31-1B38-E511-80C8-00155D010109}</salesorderid>
        <salesorderdetailid>{FF4D2454-D638-E511-80C8-00155D010109}</salesorderdetailid>
      </result>
      <result>
        <productid dsc="" name="C3592 Dual" type="1024">{8B9C08FA-B223-E511-80C8-00155D010109}</productid>
        <salesorderid type="1088">{5EB4D954-2138-E511-80C8-00155D010109}</salesorderid>
        <salesorderdetailid>{B751E179-D638-E511-80C8-00155D010109}</salesorderdetailid>
      </result>
    </resultset>


    is not null !

  • BackToTheCrm Profile Picture
    on at
    RE: how get the salesorderdetail productid

    If the field is null, you can't.

    However, if you are sure that the ProductId is entered then the error must be somewhere else ; try to get the data with a query (check Jimena Gaona answer).

  • Community Member Profile Picture
    on at
    RE: how get the salesorderdetail productid

    ok tnx

    if the field is null how can i get te product id ?

  • BackToTheCrm Profile Picture
    on at
    RE: how get the salesorderdetail productid

    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.

  • Community Member Profile Picture
    on at
    RE: how get the salesorderdetail productid

    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
    RE: how get the salesorderdetail productid

    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
    RE: how get the salesorderdetail productid
    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;
                    }
                }
            }
        }

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 54

#3
dkrishna Profile Picture

dkrishna 6

Featured topics

Product updates

Dynamics 365 release plans