Asked by:
QueryExpression - Set/Retrieve a Lookup Field

Question
-
This must be something really simple, How do I retrieve the ParentCustomerId from the Contact and use it as an Output for this custom workflow, The line which is failing currently is
AccountLookedup = c.ParentCustomerId;
Many Thanks
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Activities; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Workflow; using Microsoft.Xrm.Sdk.Query; namespace XXX.CRM.CampaignResponse { public class AccountUpdate: CodeActivity { #region Inputs / Outputs // Inputs [Input("Enter Contact Record")] [ReferenceTarget("contact")] public InArgument<EntityReference> ContactLookup { get; set; } // Outputs [Output("Account ID")] [ReferenceTarget("account")] public InArgument<EntityReference> AccountLookup { get; set; } #endregion protected override void Execute(CodeActivityContext executionContext) { // Context IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); // Lookup Account #region Account Lookup Guid AccountLookedup = Guid.Empty; QueryExpression qe = new QueryExpression { EntityName = "contact", ColumnSet = new ColumnSet("parentcustomerid","fullname","contactid"), Criteria = new FilterExpression() }; // add the filters for the contacts qe.Criteria.AddCondition("contactid", ConditionOperator.Equal, ContactLookup.Id); // Retrieve the records DataCollection<Entity> ec = service.RetrieveMultiple(qe).Entities; if (ec.Count != 0) { foreach (Contact c in ec) { AccountLookedup = c.ParentCustomerId; } } #endregion Account Lookup // Output the Account ID to the workflow AccountLookup.Set(executionContext, AccountLookedup); } } }
Pete
- Edited by Pete.CRM Monday, June 30, 2014 10:38 AM
Monday, June 30, 2014 10:38 AM
All replies
-
Try below
AccountLookedup.Set(executionContext, new EntityReference("account",c.GetAttributeValue<EntityReference>("parentcustomerid").Id));Our Website| Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.Monday, June 30, 2014 11:28 AMModerator -
Hi,
that line fails because you are trying to assign an EntityReference to a Guid (and I'm not sure you are using early bound inside your plugin, but also how you retrieve the ContactLookup Id is not correct. Try with this code:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Activities; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Workflow; using Microsoft.Xrm.Sdk.Query; namespace XXX.CRM.CampaignResponse { public class AccountUpdate : CodeActivity { #region Inputs / Outputs // Inputs [Input("Enter Contact Record")] [ReferenceTarget("contact")] public InArgument<EntityReference> ContactLookup { get; set; } // Outputs [Output("Account ID")] [ReferenceTarget("account")] public InArgument<EntityReference> AccountLookup { get; set; } #endregion protected override void Execute(CodeActivityContext executionContext) { // Context IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); // we retrieve the selected Contact first EntityReference contactLookupRef = ContactLookup.Get<EntityReference>(executionContext); // we have the contactid, so a single Retrieve is enough Entity selectedContact = service.Retrieve("contact", contactLookupRef.Id, new ColumnSet("parentcustomerid", "fullname", "contactid")); // now we take the parentcustomerid if (selectedContact.Contains("parentcustomerid") && selectedContact["parentcustomerid"] != null) { EntityReference parentCustomerRef = (EntityReference)selectedContact["parentcustomerid"]; // we check that the parent customer is an account before assign to the output parameter if (parentCustomerRef.LogicalName == "account") { AccountLookup.Set(executionContext, parentCustomerRef); } } } } }
My blog: www.crmanswers.net - Rockstar 365 Profile
Monday, June 30, 2014 11:30 AM -
What is the error your getting? PeteMonday, June 30, 2014 12:26 PM
-
managed to fix this in a normal plugin, realised I couldn't do it in a custom workflow activity as the input was the customer field which is activity party
thanks anyway
Pete
Tuesday, July 1, 2014 9:55 AM