Fusion Manage Forum
Welcome to Autodesk’s Fusion Manage (formerly Fusion 360 Manage) Forum. Share your knowledge, ask questions, and explore popular Fusion Manage topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problem wit script to populate picklist

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
brian.olson
937 Views, 7 Replies

Problem wit script to populate picklist

Can anyone tell me how to cast a java.lang.String to com.dms.scripting.wrappers.PartWrapper. I need my script to make selections in a multi-select workspace connect pick list with data from a single line text field.  Can this be done?

7 REPLIES 7
Message 2 of 8
DonovanJr
in reply to: brian.olson

Brian,

 

If I understand you correctly then yes.

 

You are looking to use data from a single line text field to make selection entries in multi-select linked picklist?

 

A multi-select picklist is an array field, so you need to put your single line text into an array then use that array to update the multi-select picklist.

 

Key is that your single line text must match exactly available entries in the linked picklist.

 

Hope that helps.

 

Bob D

Message 3 of 8
brian.olson
in reply to: DonovanJr

Bob, you understand exactly what I'm trying to do... And my script does just as you suggest. The error I get is "java.lang.String cannot be cast to com.dms.scripting.wrappers.PartWrapper" Here is my code:

var optArray = [];
var i=0;

for (var reqTests in item.REQUIRED_TESTS)
{
for (var markets in item.REQUIRED_TESTS[reqTests].MARKET_ASSIGNED_TO)
{
optArray[i] = item.REQUIRED_TESTS[reqTests].MARKET_ASSIGNED_TO[markets];
i++;
}

optArray[i] = item.MARKET_SEGMENT;
item.REQUIRED_TESTS[reqTests].MARKET_ASSIGNED_TO = optArray;
}

Where item.MARKET_SEGMENT is an exact entry from the picklist??
Message 4 of 8
brian.olson
in reply to: brian.olson

The error would suggest that I'm trying to data which is of type "string" into a field which is of type "object". When I take the approach of converting the string to and object, I get the same error except complaining about trying to cast a javascript native object to ... I think what I need to know is the name of the property of the object (the picklist field)
Message 5 of 8
jared.sund
in reply to: brian.olson

Brian, 

 

Can you try the following?

 


for (var reqTests in item.REQUIRED_TESTS)
{
item.REQUIRED_TESTS[reqTests].MARKET_ASSIGNED_TO.add(item.MARKET_SEGMENT);
}

 

multi-select lists contain 3 functions:  add(), remove(), and contains(). So you should be able to simply add the new item without building out an additional array.  The functions are also intellegent, such that you don't have to check for duplicates with adding items, or check for existance when removing items.  

Jared Sund
Sr. Product Line Manager, Product Lifecycle Management
Autodesk, Inc.
Message 6 of 8
brian.olson
in reply to: jared.sund

Thanks Jared, worked like a charm. I'm equal parts pleased and disturbed. Why is this not in the scripting reference? It makes me wonder how many two minute tasks I'm spending days on for lack of documentation??
Message 7 of 8
gasevsm
in reply to: brian.olson

brian.olson,

Glad this worked for you. I will see this is added in the documentation or the next release.

For future reference, the current model doesn't allow assigning array directly as an object.

You would traverse the array's elements to MULTI.add(item of i) or MULTI.remove(item of i);

--
Martin Gasevski

Martin Gasevski | Fusion 360 Team Product Manager
Message 8 of 8
gasevsm
in reply to: brian.olson

Also, a general comment regarding JS arrays[].

One approach is to use the index i and keep incrementing it to add new elements. Consider these alternatives in the future.


1. You can add values to the end like this.

var array = []; array.push("val1"); array.push("val2"); array.push("val3");

2. Can add them at the beginning:

var array = []; array.unshift("val1"); array.unshift("val2"); array.unshift("val3");

3. Or the approach you've taken, can add values at a specific index:

var array = []; array[0] = "val1"; array[1] = "val2";

---
A note on removing items from PLM containers eg boms or quotes or grids. If traversing over the elements for purpose of removal, it is recommended to go backwards since indices and length would change as you're emptying the container. I'm working on providing you a method to clear all at once to avoid looping whatsoever.

for(var i = container.length-1; i>=0; i--) { container.remove(i); }

In case this is a classic JS array as above, I would use array.splice(i, 1); again in a reverse loop. Avoid using "delete array[i];" as this doesn't update the length and leaves a blank entry in the array at that index position.

--
Another rule of thumb, evaluate if the script runs faster (don't know by how much yet; in process of finding out) if your FOR loops are reversed. The theory is with forward traversal, you are evaluating the array.length every time you increment the index i, whereas for reverse loop you only do it once and decrement the index only to completion. With large arrays, the difference may be completion vs. a timeout. Eg.

FWD: for (var i=0; i
REV: for (var i=arr.length-1; i>=0; i--) { /* do stuff */ }

Cheers and hope this helps with future scripting,
--
Martin Gasevski

Martin Gasevski | Fusion 360 Team Product Manager

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report