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
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
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
ok tnx i well read the article
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.
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="<cookie page="1"><salesorderdetailid last="{F2087FB8-9636-E511-828D-D6B1CC7458B0}" first="{36992D2E-D638-E511-80C8-00155D010109}" /></cookie>"> <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 !
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).
ok tnx
if the field is null how can i get te product id ?
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.
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
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).
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; } } } }
Mohamed Amine Mahmoudi
83
Super User 2025 Season 1
Community Member
54
dkrishna
6