Asked by:
Crm 2011 + To provide reference target dynamically in a custom workflow

Question
-
Hi ,
I have created a custom workflow to assign multiple records to a Teami am providing 2 input parameters -
1)FetchXml2)Custom parameter
Basically i want to fetch all the records related to the primary entity.Foreg my entity name is "new_abc" and other entity is "new_xyz". i want to fetch all the records "new_abc" records that are related to "new_xyz".
I have a look up of "new_xyz" on my primary entity.But i am not understanding why the primaryid is not getting set. it is throwing me error.
Please if anybody could help me in this.it will be a great helpBelow is my code for the custom workflow -
public class Fetch : CodeActivity
{
[Input("Fetch XML")]
public InArgument<string> FetchXML { get; set; }
[Input("Team")]
[ReferenceTarget("team")]
public InArgument<EntityReference> Team { get; set; }
[Input("Entity Logical Name")]
public InArgument<string> EntityName { get; set; }
[Input("Attribute Name Input")]
public InArgument<string> AttributeName { get; set; }
protected override void Execute(CodeActivityContext executionContext)
{
try
{
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
ITracingService tracer = executionContext.GetExtension<ITracingService>();
RowReturned.Set(executionContext, false);
Guid primaryId = Guid.Empty;
//To get Attribute from workflow
string attribute = AttributeName.Get(executionContext);
string entityName = EntityName.Get(executionContext);
//Creating entity of given type
Entity entity = new Entity(entityName);
EntityReference entityReference = (EntityReference)entity.GetAttributeValue<EntityReference>(attribute);
primaryId = entityReference.Id;
string fetch = FetchXML.Get<string>(executionContext);
fetch = String.Format(fetch, primaryId);
EntityCollection recordlist = service.RetrieveMultiple(new FetchExpression(fetch));
Count.Set(executionContext, recordlist.Entities.Count);
if (recordlist.Entities.Count > 0)
{
foreach (Entity item in recordlist.Entities)
{
// Create the Request Object and Set the Request Object's Properties
var request = new AssignRequest
{
Assignee = new EntityReference("team", Team.Get<EntityReference>(executionContext).Id),
Target = new EntityReference("mcg_locationaddress", item.Id)
};
// Execute the Request
service.Execute(request);
}
}
}
catch (Exception ex)
{
Helpers.Throw(String.Format("An error occurred in the {0} plug-in.",
this.GetType().ToString()),
ex);
}
}Thanks,
- Edited by shradha_singh Wednesday, May 14, 2014 12:13 PM
Wednesday, May 14, 2014 11:55 AM
All replies
-
primaryid is not set because
//To get Attribute from workflow string attribute = AttributeName.Get(executionContext); string entityName = EntityName.Get(executionContext); //To fetch the type of attribute Type type= attribute.GetType();
attribute.GetType() always return string (because attribute type is a string)
My blog: www.crmanswers.net - Rockstar 365 Profile
Wednesday, May 14, 2014 12:03 PM -
Hey thanks for your reply.
I modified my code as above but still it is giving me the same error.Wednesday, May 14, 2014 12:14 PM -
It is possible to know the error you got? did you try to debug or at least check the values of the variables?
My blog: www.crmanswers.net - Rockstar 365 Profile
Wednesday, May 14, 2014 12:22 PM -
I am getting the below error -
System.NullReferenceException: Object reference not set to an instance of an object.
Is there something wrong with this line
EntityReference entityReference = (EntityReference)entity.GetAttributeValue<EntityReference>(attribute);
Wednesday, May 14, 2014 1:16 PM -
Check the attribute is a lookup
Regards Faisal
Wednesday, May 14, 2014 1:30 PM