Hi,
I have two entities one ID entity(custom), the other one is Product. ID entity has Product as a lookup field on ID form. When an ID record is created ID name should be set with Product Code+Count(is a dynamic field that needs to be increased everytime when a new ID record with the same product is created). Well the problem is when I try to update ID with post update plugin it doesn't work properly and gives same name to ID's that have same product as lookup. I tried to do it with workflow, store procedure (without using crm services) but none of them helped. I am wondering how CRM works when we import data with "import excel" button. Does CRM read data row by row or do it as whole?
I have my code below. I appreciate your helps.
Here is my workflow
using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Workflow; using System; using System.Activities; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Plugins { public class UpdateIDNameWorkflow : CodeActivity { protected override void Execute(CodeActivityContext executionContext) { #region |ORGANIZATION| ITracingService tracingService = executionContext.GetExtension<ITracingService>(); IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>(); SqlConnection con = Connection.CrmDB(); #endregion |ORGANIZATION| if (context != null) { string IDid = context.PrimaryEntityId.ToString(); SqlCommand cmd = new SqlCommand("UpdateIDName", con); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@id", IDid); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } } }
That's my procedure
ALTER PROCEDURE [dbo].[UpdateIDName] (@id uniqueidentifier) AS DECLARE @number int DECLARE @idNo nvarchar(max) DECLARE @productId nvarchar(max) BEGIN SET NOCOUNT ON set @productId = (SELECT new_urun FROM new_id WHERE new_idId = @id) set @idNo = (SELECT new_idnobaslangici as idNo FROM Product WHERE ProductId = @productId) IF((SELECT new_iliskiliidnumarasi as number FROM Product WHERE ProductId = @productId) IS NULL) BEGIN set @number = 1 update new_id SET new_name = (@idNo +'-'+ CAST( @number as varchar(5) )) where new_idId=@id update product SET new_iliskiliidnumarasi = @number where ProductId = @productId END ELSE BEGIN set @number = ( (SELECT new_iliskiliidnumarasi as number FROM Product WHERE ProductId = @productId) + 1) update new_id SET new_name = (@idNo +'-' + CAST( @number as varchar(5) )) where new_idId=@id update product SET new_iliskiliidnumarasi = @number where ProductId = @productId END END
new_iliskiliidnumarasi (@number) : dynamic field I use as counter on the basis of product
new_idnobaslangici (@idNo) : product code
ID name needs to be : @idNo - @number
Many thanks,
Regards
Ezgi