Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

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: 
  1. code: "OPERATION_BAD_REQUEST"
  2. message: "Illegal Argument: attributes"
Any hint how the RM.Data.SetAttributes first parameter has to be set is welcome.

0 votes



2 answers

Permanent link
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;

0 votes

Comments
Sorry, this is my trigger:

RM.Event.subscribe(RM.Event.ARTIFACT_SAVED, function(artRef){
    artRefsList=[]
    artRefsList.push(artRef)    
    console.log(artRefsList)
    resetProcessState(artRefsList)
       
})

OK 
 - firstly, the callback function is passed an array of artefact refs, so artRef is an array already.
 - secondly, you probably want to go through that array and make sure the artefacts that were saved are of the correct artefact type, unless you know for sure they are. That would be the time to push the correct types into your own array.

I think the main problem you have is a) you're pushing an array reference into your array, so everything else in your first code snippet will fail, and b) you're not setting a reference to the actual artefact that needs the values updated with new attribute values in that first snippet


Permanent link

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]
            if (attrs.values["http://www.ibm.com/xmlns/rdm/rdf/ofType"].name==="Software Requirement" ||
            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)
                    }
                })
            }
        })    
    })

0 votes

Your answer

Register or log in to post your answer.

Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details
× 10,939

Question asked: Oct 09 '24, 3:59 a.m.

Question was seen: 358 times

Last updated: Oct 14 '24, 9:22 a.m.

Confirmation Cancel Confirm