Get current state of selected custom object C#, please help

Get current state of selected custom object C#, please help

Anonymous
Not applicable
1,258 Views
2 Replies
Message 1 of 3

Get current state of selected custom object C#, please help

Anonymous
Not applicable

I have a custom object called task. how do i get the current lifecycle state of the selected custom object, please help.

 

The custom object has a lifecycle definition called

Name = Task Process / ID = 10

[Task Process  10].

 

The lifecycle definition [ Task Process 10 ] has these states.

Name =Created / ID = 45

[Created 45]

Name =Assigned / ID = 46

[Assigned 46]

Name =In progress / ID = 47

[In progress 47]

Name =On hold / ID = 48

[On hold 48]

Name =Completed / ID = 49

[Completed 49]

Name =Cancelled / ID = 50

[Cancelled 50]

 

i have found this code but is't for a item, and i can't figure out how to rewrite this for a custom object.

 

string strCustomObjectId = m_objectid.ToString();

Item m_selecteditem = m_mgr.ItemService.GetLatestItemByItemNumber(strCustomObjectId);

// grab information about the lifecycle definition that the item is in

LfCycDef lifeCycleDef = m_mgr.LifeCycleService.GetLifeCycleDefinitionsByIds(m_selecteditem.LfCyc.LfCycDefId.ToSingleArray()).First();

// create a map of the states so that we can look them up easily.

Dictionary<long, LfCycState> stateMap = lifeCycleDef.StateArray.ToDictionary(n => n.Id);

// display the value of the current state

//control.Text = stateMap[m_selecteditem.LfCycStateId].DispName;

MessageBox.Show(stateMap[m_selecteditem.LfCycStateId].DispName);

 

regards kent boettger

0 Likes
Accepted solutions (1)
1,259 Views
2 Replies
Replies (2)
Message 2 of 3

psaarloos
Collaborator
Collaborator
Accepted solution

Hi Kent,

 

Here is a short sample which hopefully gets you further. I haven't tested it, but I think it should get you where you want.

 

private void MyCommandHandler(CommandItemEventArgs e)
{
	// This line should be somewhere else in your extension. In a static class for example.
	SelectionTypeId TaskSelectionType = new SelectionTypeId("SystemNameOfYourCustomEntity");
	
	// verify the event args
	long count = e.Context.CurrentSelectionSet.Count();
	if (e.Context.CurrentSelectionSet == null || count == 0)
		MessageBox.Show("Nothing selected");
	else if (count > 1)
		MessageBox.Show("This operation only works on a single selection");

	ISelection selection = e.Context.CurrentSelectionSet.First();
	
	string entityClassId = null;
	long entityId = -1;
	
	if (selection.TypeId == TaskSelectionType)
	{
		entityClassId = "CUSTENT";
		entityId = selection.Id;

		CustEnt entity = _webServiceManager.CustomEntityService.GetCustomEntitiesByIds(new long[] { entityId }).First()
		
		// Here you can get to the LfCyc object of the entity
		long lifeCycleDefId = entity.LfCyc.LfCycDefId;
		long lifeCycleStateId = entity.LfCyc.LfCycStateId;
		
		// Use the LifeCycleService to get to the Lifecycle Definition object for example
		LfCycDef[] lcDefs = _webServiceManager.LifeCycleService.GetAllLifeCycleDefinitions();

		if (lcDefs == null)
		{
			LfCycDef lifeCycleDefinition = lcDefs.SingleOrDefault(n => n.Id == lifeCycleDefId);
		}
	}
	else
	{
		MessageBox.Show("This operation does not work with this entity type");
	}
}

Hope it helps.

Regards,
Pim Saarloos
Product Manager
If my post answers your question, please click the "Accept as Solution" button. Kudos are much appreciated!
Message 3 of 3

Anonymous
Not applicable

Thanks for the quick help psaarloos,

 

i got it working like this, i case some one else needs this.

 

// current state in a textbox

 

// This is special bacause ist not linked to a property i vault

if (tagValue == "TaskCurrentState")

 

{

CustEnt entity = m_mgr.CustomEntityService.GetCustomEntitiesByIds(new long[] { m_objectid }).First();

// grab information about the lifecycle definition that the item is in

LfCycDef lifeCycleDef = m_mgr.LifeCycleService.GetLifeCycleDefinitionsByIds(entity.LfCyc.LfCycDefId.ToSingleArray()).First();

// create a map of the states so that we can look them up easily.

Dictionary<long, LfCycState> stateMap = lifeCycleDef.StateArray.ToDictionary(n => n.Id);

//MessageBox.Show(stateMap[entity.LfCyc.LfCycStateId].DispName);

// set the control value to current entity state

 

control.Text = (stateMap[entity.LfCyc.LfCycStateId].DispName);

}

 

// transitions from the current state in a combobox 

 

if (tagValue == "TaskNewLifeCycleState")

 

{

 

ComboBox combobox = control as ComboBox;

 

combobox.Items.Clear();

try

 

{

 

String FirstComboBoxItem = null;

CustEnt entity = m_mgr.CustomEntityService.GetCustomEntitiesByIds(new long[] { m_objectid }).First();

// grab information about the lifecycle definition that the item is in

LfCycDef lifeCycleDef = m_mgr.LifeCycleService.GetLifeCycleDefinitionsByIds(entity.LfCyc.LfCycDefId.ToSingleArray()).First();

// create a map of the states so that we can look them up easily.

Dictionary<long, LfCycState> stateMap = lifeCycleDef.StateArray.ToDictionary(n => n.Id);

//MessageBox.Show(stateMap[entity.LfCyc.LfCycStateId].DispName);

// Display the value of the current state

//control.Text = stateMap[entity.LfCyc.LfCycStateId].DispName;

foreach (LfCycTrans trans in lifeCycleDef.TransArray)

 

{

// figure out the transitions from the current state and add them to the combo box

if (trans.FromId == entity.LfCyc.LfCycStateId)

 

{

TransItem item = new TransItem(trans, stateMap[trans.ToId]);

 

combobox.Items.Add(item);

if (FirstComboBoxItem == null)

 

{

FirstComboBoxItem = item.ToString();

}

}

}

// set the value in the combobox

 

control.Text = FirstComboBoxItem;

}

catch (Exception ex)

 

{

MessageBox.Show(ex.ToString());

combobox.Items.Add("<Select>");

// set the value in the combobox

control.Text = "<Select>";

 

}

}

 

 

 

 

 

 

0 Likes