Notifications
Announcements
No record found.
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
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
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"
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
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,
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
Share your code here for inquiry
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; } } } }
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).
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
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 :
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.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
SA-08121319-0 4
Calum MacFarlane 4
Alex Fun Wei Jie 2