How to set artifact values using RM.Data.setAttributes?
I am trying to set an attribute value to another one if the artifact changed before. My code looks like following (according to https://jazz.net/wiki/bin/view/Main/RMExtensionsAPI702#SetAttributes)
function resetProcessState(artRefsList){
var toSave = [];
// For each artifact in the module, reset process attributes
artRefsList.forEach(function(item){
console.log (item)
// Create a new ArtifactAttributes object to contain the update data
var artifactAttributes = new RM.ArtifactAttributes();
var attributeValues = new RM.AttributeValues();
//artifactAttributes.ref = item;
artifactAttributes.values = attributeValues;
// Reset some attribute values
attributeValues['Variants']="something";
toSave.push(artifactAttributes);
console.log (toSave)
});
// Perform a bulk save of all attributes
RM.Data.setAttributes(toSave[0], function(result){
if(result.code === RM.OperationResult.OPERATION_OK)
{
console.log ("changed")
}
else{
console.log (result)
}
})
}
If I let the code so, nothing happens.
If I set the artifactAttributes.ref = item I get following error: Operation failed, Operation_Bad_Request.
If I change the code RM.Data.setAttributes(toSave[0], function(result) to RM.Data.setAttributes(toSave, function(result), I get the same error.
If I change the code RM.Data.setAttributes(toSave[0], function(result) to RM.Data.setAttributes(toSave[0].values, function(result), I get following error:
- code: "OPERATION_BAD_REQUEST"
- message: "Illegal Argument: attributes"
Any hint how the RM.Data.SetAttributes first parameter has to be set is welcome.
2 answers
There are a whole lot of issues with this code:
- How are you triggering this code? Can you show the entire example? What are you passing into the function via artRefsList?
- in order to set attributes on an artefact, you first have to retrieve an item and its attributes. That sets up references to the attributes and their values that you then use in the setAttributes function. The line you have commented out is not correct but it's the line you need in order to set up the reference. The line should read:
artifactAttributes.ref = item.ref;
Comments
Thank you Davyd for your hints. With your help I got it now. See afterwards my solution:
let oldArtRef=[]
RM.Event.subscribe(RM.Event.ARTIFACT_SAVED, async function(artRef){
console.log (oldArtRef)
console.log (artRef)
if (oldArtRef[0]){
if (oldArtRef[0].uri != artRef[0].uri){
oldArtRef = await resetProcessState(artRef)
}
}
else{
oldArtRef = await resetProcessState(artRef)
}
})
// Reset workflow state for the changed artifact to "New", if the artifact type is one of "Software Requirement" or "Required Value"
async function resetProcessState(artRef){
return new Promise((resolve, reject)=> {
RM.Data.getAttributes(artRef, [RM.Data.Attributes.ARTIFACT_TYPE], async function(result){
console.log (result.data)
var attrs = result.data[0]
attrs.values["http://www.ibm.com/xmlns/rdm/rdf/ofType"].name==="Required Value"){
let artifactAttributes = await new RM.ArtifactAttributes();
let attributeValues = await new RM.AttributeValues();
artifactAttributes.ref = artRef[0];
artifactAttributes.values = attributeValues
// Reset some attribute values
attributeValues["State (Requirements Workflow)"]="New";
console.log (attributeValues)
console.log (artifactAttributes)
await RM.Data.setAttributes(artifactAttributes, async function(result){
if(result.code === RM.OperationResult.OPERATION_OK){
resolve(artRef)
}
else{
reject(result)
}
})
}
})
})