Reuse Code in C# Method

Good day all.  I am trying to use a variant on my previously posted Workflow Process Get Change ItemType.  I want to reuse some of the code multiple times without having to write it out every time.  The code references identId, but I can't figure out how to change the variable based on information pulled from the Workflow.  I have tried If statements to change the variable, but they return an error about not being able to convert a string to a string.  I know if there is a way to change the identId then I don't have to continue to copy/paste the foreach section.  Thank you for any help that can be provided.

foreach (string i in identId)
{
Item assignment = this.newItem("Activity Assignment", "");
assignment.setAction("add");
assignment.setProperty("source_id", actId);
assignment.setPropertyAttribute("locked_by_id","is_null","1");
assignment.setProperty("related_id",(i));
assignment.setProperty("voting_weight", (100 / identId.Count() + 1).ToString()); // Divides the number of IDs in identId and adds 1
assignment.setProperty("is_required", "1");
assignment = assignment.apply();
if (assignment.getItemCount() != 1) return inn.newError("Error creating assignment: "+assignment.getErrorDetail());
}
Parents
  • Really funny! I worked on an exact same scenario yesterday. Just look at my reviewer fields in this post:

    https://community.aras.com/f/development/36437/what-is-the-easiest-way-to-show-a-placeholder-when-an-item-selector-is-empty

    I also thought about make this one reusable, cause I already have three similar Methods. But in my case all my CM processes have their special scnearious, so I sticked to the copy&paste version.

    In my case the line of code with your "identId" looks like this:

    assignment.setProperty("voting_weight", (100 / reviewers.Count + 1).ToString());

    "reviewers" in my case is a list that contains my reviewers. I use some extra code (hashset) to avoid people assign themselves multiple times to speed up the progress. It´s based on the available reviewer fields in the form. Do you always have the same amount of reviewers or do they change? Then you can just use a static value in side the method.

    You basically have to count your reviewer fields to calculate the voting_weight. But you can also make this one dynamic based on your Team roles. 

  • AngelIp,

    In this case I have several Identities that I want to add based on a couple of factors, System field and Activity.  I would like to be able to use the same method for several activities if possible.  I have named strings that contain the Identity ID and use a Union to combine a couple of them for the assignment.  Because I want to use this in a few places, I want to be able to set the var identId and then move to the posted code.  I can make it work by having If statements with var identId (with the Identity IDs associated) and the code inside of the If statement, but that means I have to continually paste the code into all of the If statements.

Reply
  • AngelIp,

    In this case I have several Identities that I want to add based on a couple of factors, System field and Activity.  I would like to be able to use the same method for several activities if possible.  I have named strings that contain the Identity ID and use a Union to combine a couple of them for the assignment.  Because I want to use this in a few places, I want to be able to set the var identId and then move to the posted code.  I can make it work by having If statements with var identId (with the Identity IDs associated) and the code inside of the If statement, but that means I have to continually paste the code into all of the If statements.

Children
No Data