locked
Setting the value of a Look up in a custom workflow activity RRS feed

  • Question

  •     private DynamicEntity Retrieve(ICrmService iCrmService, string entityName, Guid entityGuid, string[] columnsToRetrieve)
        {
          try
          {
            TargetRetrieveDynamic targetRetrieveDynamic = new TargetRetrieveDynamic();
            targetRetrieveDynamic.EntityName = entityName;
            targetRetrieveDynamic.EntityId = entityGuid;
    
            RetrieveRequest retrieveRequest = new RetrieveRequest();
            retrieveRequest.ColumnSet = new ColumnSet(columnsToRetrieve);
            retrieveRequest.ReturnDynamicEntities = true;
            retrieveRequest.Target = targetRetrieveDynamic;
    
            return (iCrmService.Execute(retrieveRequest) as RetrieveResponse).BusinessEntity as DynamicEntity;
          }
          catch (SoapException ex)
          {
            //throw ex;
            throw new Exception("GetLoggedInUser.Retrieve : Error Message = " + ex.Detail.InnerText);
    
          }
          catch (Exception ex)
          {
            //throw ex;
            throw new Exception("GetLoggedInUser.Retrieve : Error Message = " + ex.Message);
          }
        }
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
          //
          try
          {
            IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
            IWorkflowContext context = contextService.Context;
    
            ICrmService crmService = context.CreateCrmService();
    
            Guid dxID = context.PrimaryEntityId;
            Guid userGuid = context.UserId;
            string[] colSet = new string[] { "xxx_rrrrmanagerid"};
            DynamicEntity dx = new DynamicEntity();
            dx = Retrieve(crmService, "xxx_dx", dxID, colSet);
            if (dx.Properties.Contains("xxx_rrrrmanagerid"))
            {
              LookupProperty oVVVVMgrID = new LookupProperty();
              oVVVVMgrID.Name = "xxx_rrrrmanagerid";
              oVVVVMgrID.Value = new Lookup("systemuser", userGuid);
              dSummons.Properties.Add(oVVVVMgrID);
              
            }
            crmService.Update(dx);
            return ActivityExecutionStatus.Closed;
          }
          catch (InvalidPluginExecutionException)
          {
            return ActivityExecutionStatus.Faulting;
          }
        }
    
    I am trying to set the value of a lookup (xxx_rrrrmanagerid)  with the logged in user value in a custom workflow activity in code. Its just not working. The workflow executes without any errors but it does not the set the value of the user in the lookup attribute. please help.
    Friday, December 3, 2010 12:46 AM

Answers

  • CrmService.Update is only used to update a Business Entity.  

    That, strictly speaking, is not true, Update message can be used to update Dynamic Entities. Either that or all of my customers are using non-functional code :-)

    Most likely, it a simple typo, I can see property being added to entity dSummons following by update to entity dx. If that's not the real code then I see another issue: the update code is conditional on the presence of the attribute. That's not going to fly if entity has this attribute set to null in the database. CRM does not return attributes that have null values.

    Besides, to update an attribute, there is absolutely no need to retrieve anything, code like this should work:

    DynamicEntity dx = new DynamicEntity("xxx_dx");
    
    dx["xxx_dxid"] = new Key(context.PrimaryEntityId);
    dx["xxx_rrrrmanagerid"] = new Lookup("systemuser", context.UserId);
    
    crmService.Update(dx);
    
    --

     

     


    George Doubinski, MVP http://crm.georged.id.au
    Friday, December 3, 2010 1:21 AM
    Moderator

All replies

  • CrmService.Update is only used to update a Business Entity.  To update a Dynamic Entity, you need to use a TargetUpdateDynamic via an UpdateRequest and pass it to CrmService.Execute.

    Sample code from the SDK to update a Dynamic Entity.


    --pogo (pat)
    Friday, December 3, 2010 12:58 AM
  • CrmService.Update is only used to update a Business Entity.  

    That, strictly speaking, is not true, Update message can be used to update Dynamic Entities. Either that or all of my customers are using non-functional code :-)

    Most likely, it a simple typo, I can see property being added to entity dSummons following by update to entity dx. If that's not the real code then I see another issue: the update code is conditional on the presence of the attribute. That's not going to fly if entity has this attribute set to null in the database. CRM does not return attributes that have null values.

    Besides, to update an attribute, there is absolutely no need to retrieve anything, code like this should work:

    DynamicEntity dx = new DynamicEntity("xxx_dx");
    
    dx["xxx_dxid"] = new Key(context.PrimaryEntityId);
    dx["xxx_rrrrmanagerid"] = new Lookup("systemuser", context.UserId);
    
    crmService.Update(dx);
    
    --

     

     


    George Doubinski, MVP http://crm.georged.id.au
    Friday, December 3, 2010 1:21 AM
    Moderator
  • Oops... I didn't read the fine print (DynamicEntity is derived from BusinessEntity) and... damnit... missed the obvious!
    --pogo (pat)
    Friday, December 3, 2010 1:41 AM