Fusion Lifecycle Forum
Welcome to Autodesk’s Fusion Lifecycle Forums. Share your knowledge, ask questions, and explore popular Fusion Lifecycle topics.

Fusion Lifecycle Forum

Reply
Highlighted
Accepted Solution

Validation Script problem

135 Views, 8 Replies
10-25-2016 11:42 AM

I am new to scripting and am having a problem with a validation script I created (shown below) .  SUPPLIER_QUALITY_MANUAL is a radio button field.  My goal is to stop the workflow if this field is not completed.  At the moment it works in that it stops the workflow when the field is left blank however it also stops the workflow if the field is completed.  I think my problem is that the SUPPLIER_QUALITY_MANUAL field is in a different workspace from the workspace the workflow is in.  How do I direct the script to the correct workspace?  Any help would be greatly appreciated.

 

var messages = [];

if(item.SUPPLIER_QUALITY_MANUAL !== null){
messages.push('A response must be entered for "Supplier Quality Manual" ');
}

returnValue(messages);

 

Carmen

Here is the final script after some more email discussions.

 

Bottom line, the customer has two workspaces and use relationship to link the two.  The attribute to validate the workflow is not on the workflow workspace but on one of the item of the relationship workspace.

 

Here is the final code:

 

var relationshipSize = item.relationships.length;
var messages = [];
var i;
for( i=0;i<relationshipSize;i++){
var relationshipItem = item.relationships[i].item;
if(relationshipItem.master.workspaceID ===57){
if(relationshipItem.SUPPLIER_QUALITY_MANUAL === null){
messages.push('A response must be entered for "Supplier Quality Manual" for ' + relationshipItem.descriptor.descriptor );
}else if(relationshipItem.SUPPLIER_QUALITY_MANUAL === "Yes"){
//Do Something if needed for yes
}else if(relationshipItem.SUPPLIER_QUALITY_MANUAL === "No"){
//Do Something if needed for no
}
}
}
returnValue(messages);
Post 2 of 9

Re: Validation Script problem

10-25-2016 12:40 PM in reply to: Customer.Service

The pattern that is usually used is to create a linking pick-list attribute to reference the record in the other workspace.  The picklist would refernce records in the other workspace, and the attribute would select one of those records to associate to the item.

 

If I understand correectly the workflow is associated to the item.  So item.LINKING_PICK_LIST.SUPPLIER_QUALITY_MANUAL would refernce the associated attribute in the other workspace.

 

Does this help?

Post 3 of 9

Re: Validation Script problem

10-25-2016 11:04 PM in reply to: tony.mandatori

Hello Carmen,

 

First of all, if you want to check if it is NOT set, you need to test for === null  .

 

 

var messages = [];
if(item.SUPPLIER_QUALITY_MANUAL === null){
messages.push('A response must be entered for "Supplier Quality Manual" ');
} returnValue(messages);

 

 

From your comment about the "SUPPLIER_QUALITY_MANUAL field is in a different workspace from the workspace ", it seems that this field is on the Affected/Managed Item (the item you want to revision) and not on the workflow item directly (the revisioning item).

 

In both cases I assume that the radio button is from a custom picklist with yes and no, correct?

 

One thing to help you, you can set the "default" value of the SUPPLIER_QUALITY_MANUAL so your new items will never be "empty"/null. Except of course if you actually "always" want the user to manually set a value.

 

Here is the field description that I used for the code below:

 

supplier document.png

 

Here is an example if it is the case (that the field is actually from the linked item in the Managed/Affected Item Tab. You need to loop through each linked revision controlled items under that revisioning item.

 

 

var messages = [];

var wfiSize = item.workflowItems.length;

var i;
for( i=0;i<wfiSize;i++){

var wfi = item.workflowItems[i].item;


if(wfi.SUPPLIER_QUALITY_MANUAL === null){

messages.push('A response must be entered for "Supplier Quality Manual" ');

}else if(wfi.SUPPLIER_QUALITY_MANUAL === "Yes"){

//Do something if it shouldn't be yes

}else if(wfi.SUPPLIER_QUALITY_MANUAL === "No"){

//Do something if it shouldn't be no
}
}
returnValue(messages);


 

And here the example if the field is on the Revisioning item itself (like you have in your example):

 

 

var messages = [];

if(item.SUPPLIER_QUALITY_MANUAL === null){ messages.push('A response must be entered for "Supplier Quality Manual" '); }else if(item.SUPPLIER_QUALITY_MANUAL === "Yes"){ //Do something if it shouldn't be yes
}else if(item.SUPPLIER_QUALITY_MANUAL === "No"){ //Do something if it shouldn't be no }
returnValue(messages);

 

 



Dany Poudrier

PLM Product Manager

Re: Validation Script problem

10-26-2016 07:07 AM in reply to: tony.mandatori

Thanks for the response.  I'm sorry if I did not supply enough context to my problem.  The workflow I placed the validation script in is linked to the workspace the field is in via a relationship.  The radio button is from a custom picklist with a yes and no option.  I know I can make the radio button a mandatory field but that option will not solve our problem.  I need the workflow to stop if the field is not answered.  When I run my current script I get the error...

Field SUPPLIER_QUALITY_MANUAL does not exist in the target workspace. This may be an error.

 

I have very limited knowledge of scripting and may not have understood your response.  I did change the line in my script so my script now reads as follows:

 

var messages = [];
if(item.LINKING_PICK_LIST.SUPPLIER_QUALITY_MANUAL === null){
messages.push('A response must be entered for "Supplier Quality Manual" ');
}
returnValue(messages); 

 

This returns an error .....

There is an error in the script on line 2, column 0. TypeError: Cannot read property "SUPPLIER_QUALITY_MANUAL" from null (SMC_166_fields_required#2)

 

Did I totally misunderstand??

 

Re: Validation Script problem

10-26-2016 07:18 AM in reply to: dany.poudrier

Hi Dany,

Thanks for the response.  I'm sorry, but I am not familiar with the wfi and wfisize statements and what they mean in this script and how to incorporate them.  I did correct my line to test for === null.  

 

The workflow I placed the validation script in is linked to the workspace the field is in via a relationship.  The radio button is from a custom picklist with a yes and no option.  I know I can make the radio button a mandatory field but that option will not solve our problem.  I need the workflow to stop if the field is not answered.  When I run my current script I get the error...

 

Field SUPPLIER_QUALITY_MANUAL does not exist in the target workspace. This may be an error.

 

Does the additional context help explain my problem better?

 

Carmen

Post 6 of 9

Re: Validation Script problem

10-27-2016 02:05 AM in reply to: Customer.Service

Hi,

 

Sorry about that confusion.

 

I assumed that you were a little more familiar with Javascript.  The wfi is just a variable name (I could have named it toto). But in your specific case you do not need it.

 

 

The right code is bellow item.LINKING_PICK_LIST.SUPPLIER_QUALITY_MANUAL

 

 

var messages = [];
if(item.SUPPLIER_QUALITY_MANUAL === null){
   messages.push('A response must be entered for "Supplier Quality Manual" ');
}
returnValue(messages); 

 

 



Dany Poudrier

PLM Product Manager

Re: Validation Script problem

10-27-2016 04:34 AM in reply to: dany.poudrier

Thanks for your patience Dany.  I have only had 2 days training total so my knowledge is extremely limited.  I have revised my script per your post and when I debug, I can see that the field is being linked.  I get the correct response message when I try to move the workflow to the next step without entering anything in the field, however I also get the same response message when I put "yes" or "no" in the field.  So the workflow will stop me no matter what is in the field at this point.  Do you have any other suggestions?

 

Also, do you have any suggestions on books that are available for learning scripts?

 

I greatly appreciate your assistance.

 

Carmen 

Post 8 of 9

Re: Validation Script problem

11-03-2016 04:51 AM in reply to: Customer.Service

Here is the final script after some more email discussions.

 

Bottom line, the customer has two workspaces and use relationship to link the two.  The attribute to validate the workflow is not on the workflow workspace but on one of the item of the relationship workspace.

 

Here is the final code:

 

var relationshipSize = item.relationships.length;

var messages = [];
var i;
for( i=0;i<relationshipSize;i++){
    
    var relationshipItem =  item.relationships[i].item;
    
    if(relationshipItem.master.workspaceID ===57){
    
        if(relationshipItem.SUPPLIER_QUALITY_MANUAL === null){
            
            messages.push('A response must be entered for "Supplier Quality Manual" for ' + relationshipItem.descriptor.descriptor );
            
        }else if(relationshipItem.SUPPLIER_QUALITY_MANUAL === "Yes"){
            
            //Do Something if needed for yes
        }else if(relationshipItem.SUPPLIER_QUALITY_MANUAL === "No"){
            
            //Do Something if needed for no
        }
    }
}
returnValue(messages);


Dany Poudrier

PLM Product Manager

Re: Validation Script problem

11-03-2016 08:02 AM in reply to: dany.poudrier

Thanks again for your assistance Dany.  The solution works like a dream!

 

Carmen

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post


Couldn’t make it to Autodesk University? Some of the Fusion Lifecycle Classes are available to watch on-demand:
http://autode.sk/2gJmBy3