RTC - javascript - parent link required howto
Accepted answer
Hi Olivier,
The most compact way to retrieve links is to use the getValue() function on the proxy like so:
var childLinks = workItem.getProxy().getValue({
path : [ 'linkTypes', 'com.ibm.team.workitem.linktype.parentworkitem' ]
});
path : [ 'linkTypes', 'com.ibm.team.workitem.linktype.parentworkitem' ]
});
To actually create a link, the following code will do the trick (newProxy is the proxy for the new child item, and parentWorkItem is the actual parent work item, not its proxy):
var linkparam = {
isNew : true,
addLinkType : "com.ibm.team.workitem.linktype.parentworkitem",
addLinkIsSource : "false",
addLinkedProjectAreaItemId : parentWorkItem.getValue(WorkItemAttributes.PROJECT_AREA),
addLinkedItemId : parentWorkItem.getWorkItemItemId()
};
newProxy.addLinkTarget(linkparam);
var saveparam = {
self : this,
operationMsg : "Saving",
applyDelta : true,
onSuccess : function(param) {
console.log('Save Success');
parentWorkItem.getProxy().setHasNewLinksPending(true);
}
};
newProxy.storeWorkItem(saveparam);
Comments
Thanks a lot for this answer.
My code will be more readable now.
BTW - the attribute in linkParam called addLinkIsSource is used to select which end of the relationship you are creating.
Each relationship type has an id - the Parent/Child relationship in this case has the id "com.ibm.team.workitem.linktype.parentworkitem", and then it has two ends. One end (the first in the relationship definition XML) is designated as the Source, and then IsSource allows you to declare which end you want to attach to the work item.
In this case we want the Child end, so addLinkIsSource is set to false
One other answer
Yes, this is possible.
The officially supported Java Script API does not have the capability to detect links.
There is a work item proxy, that seems to allow finding links in JavaScript.
var proxy = workItem.getProxy();
This only works in the Web UI, is unsupported and should only be tried by really experienced JavaScript developers. There are posts in this forum using this.
Requiring a link to a work item can be achieved using a work item save advisor. Here an example of an advisor https://rsjazz.wordpress.com/2014/05/26/only-owner-can-close-workitem-advisor/
The blog has also examples around link APIs. E.g. https://rsjazz.wordpress.com/2012/11/27/resolve-parent-if-all-children-are-resolved-participant/
Comments
Thanks.
The workItem.getProxy() is the perfect solution.
I can read the workItem.getProxy()._valueSets._workItem._proxy.object.linkTypes array to check the links defined in the workItem.
It is possible to use the getProxy().storedObject to search for the workItem datas.
Perfect. Thanks.
Note that this is all not supported and does also not work in the RTC Eclipse client.
Yes, and a strange behavior appears when we use workItem.getProxy() in, by example, ) attribute Validator javascript.
If we just do a call of this method, in the validate method of the validator code :
validate: function(attributeId, workItem, configuration) {
var proxy = workItem.getProxy();
return new Status(Severity["ERROR"], "Feature sans EPIC parent !");
}
then the "return new Status() ..." is not interpreted.
If we remove the line : var proxy = workItem.getProxy(), everything works fine.
I think you have something else going on there - calling getProxy() in that situation works fine for me.