03 November, 2011

FIM 2010/WF: UpdateResourceActivity and While-iterations

I today struggled a great deal with a while-loop that gave me null-values when attempting to use the contained UpdateResourceActivity as seen below. Every time I entered the UpdateResourceActivity (‘fimUpdateGroup’) it kept complaining that some collection was null?


What’s happening?

Well – it turns out that if you execute a child-activity (as ‘updateSequence’ above) more than once, you actually gain a separate instance for each run!

From MSDN:
When a child activity is executed more than once, a separate instance of the activity is created for each iteration. This allows the instances to execute independently (and potentially in parallel, as in the case of a ReplicatorActivity activity). As a consequence, the definition of the child activity in the activity tree (referred to as the template) is never executed and is always in the Initialized state. Access to the running instances of the template is provided by the composite activity that is the parent of the template. For example, in the case of the WhileActivity activity, there is always one active instance, and the DynamicActivity property retrieves this instance.

So – where does this leave us?
In the above scenario – you can the correct instance in this way:

private void prepare_GroupUpdate(object sender, EventArgs e)

//VERY IMPORTANT!: grap hold of the proper fimUpdateGroup INSTANCE!!
var seqParent = (SequenceActivity)this.whileTrue.DynamicActivity;
UpdateResourceActivity currentUpdateActivityInstance = (UpdateResourceActivity)seqParent.Activities.OfType<UpdateResourceActivity>().First(); //only 1!
Logger.DebugAssert(currentUpdateActivityInstance != null, "currentUpdateActivityInstance == null");

//add accounts
var upLst = new List<UpdateRequestParameter>();
this.accountLst.ForEach(x => upLst.Add(new UpdateRequestParameter("ExplicitMember", UpdateMode.Insert, x)));
Logger.DebugWrite("Adding '{0}' accounts to updatelist", upLst.Count);

//set values on this INSTANCE!
currentUpdateActivityInstance.UpdateParameters = upLst.ToArray();
currentUpdateActivityInstance.ActorId = this.ActorId;
currentUpdateActivityInstance.ResourceId = this.groupObjectLst[this.currentIndex].ObjectID;
Logger.DebugWrite("\tCurrent Resource to update: '{0}'", currentUpdateActivityInstance.ResourceId);


So – there you have it. To make this scenario work (with a While-loop); you need to use the <parent>.DynamicActivity property on the parent activity to gain proper access.

Who should have known ;-)

StackOverflow Tags: ,

No comments:

iPhone/XCode - not all cases are equal!

This bit me! Having made some changes to an iPhone application (Obj-C); everything worked fine in the simulator. But, when deploying the s...