Help with trying to create an Item via API

Help with trying to create an Item via API

mslosarKES
Participant Participant
111 Views
4 Replies
Message 1 of 5

Help with trying to create an Item via API

mslosarKES
Participant
Participant

Short explanation is i'm trying to create a method for us to import items from our very old ERP to Vault Items. I have create a test function to work from and i finally go to at least report success, but no item is getting created in Vault. Can anyone point out what exactly we're missing here? Thanks

 

private void TestCreateSingleItem(Connection connection)
{            
    try
    {                
        Cat[] categories = connection.WebServiceManager.CategoryService.GetCategoriesByEntityClassId("ITEM", true);
        long catId = -1;
        foreach (Cat category in categories)
        {
            if (category.SysName == "Assembly")
                catId = category.Id;
        }
        var categoryId = GetDefaultItemCategoryId(connection);
        MessageBox.Show($"Found category ID: {catId}");

        // Step 1: Find available numbering schemes
        long numschid = -1;
        NumSchm[] schemes = connection.WebServiceManager.NumberingService.GetNumberingSchemes("ITEM", NumSchmType.Activated);
        foreach (NumSchm scheme in schemes)
        {
            if (scheme.Name.Contains("Item"))
            {
                numschid = scheme.SchmID;
            }
        }

        try
        {                       
            // create the item 
            var newItem = connection.WebServiceManager.ItemService.AddItemRevision(catId);
            MessageBox.Show($"Item created with ID: {newItem.Id}, MasterID: {newItem.MasterId}");

            // Step 2: 

            var fieldInputs = new StringArray[1];
            var sarray = new StringArray
            {
                Items = new string[] { "TEST-001" }  
            };
            fieldInputs[0] = sarray;

            // Try with numbering scheme ID = 1 (might not exist, but let's see the error)
            var itemNums = connection.WebServiceManager.ItemService.AddItemNumbers(
                new long[] { newItem.MasterId },
                new long[] { numschid },  
                fieldInputs,
                out ProductRestric[] restrictions);

            MessageBox.Show($"AddItemNumbers succeeded! Got item number: {itemNums[0].ItemNum1}");

        }
        catch (Exception ex)
        {
            MessageBox.Show($"Error with AddItemNumbers: {ex.Message}");
            // The error might tell us what numbering scheme IDs are available
        }

    }
    catch (Exception ex)
    {
        MessageBox.Show($"Error: {ex.Message}");
    }
}
0 Likes
Accepted solutions (1)
112 Views
4 Replies
Replies (4)
Message 2 of 5

Markus.Koechl
Autodesk
Autodesk
Accepted solution

Flying over your code, I haven't seen two important steps: you need to commit the new number (as it replaces the default numbering) and commit the item creation itself. I hope you can identify the details by comparing a working code here: Vault-API-Snippets-and-Samples/Vault-API-PowerShell-Script-Samples/Items/Vault-API-Sample-CreateNewI...



Markus Koechl

Solutions Engineer PDM, Autodesk Central Europe
0 Likes
Message 3 of 5

mslosarKES
Participant
Participant

Thank you for that. I believe i see the 2 steps there at the end. I'll give that a try and I will let you know how it goes.

0 Likes
Message 4 of 5

mslosarKES
Participant
Participant

Took some beating it over the head, but i got there 🙂

 

There was some slight reworking of the base, but adding these at the end got it to write to the Item Master:

 connection.WebServiceManager.ItemService.CommitItemNumbers(
     new long[] { newItem.MasterId },
     new string[] { mNumbers[0].ItemNum1 });

 connection.WebServiceManager.ItemService.UpdateAndCommitItems(new Item[] { newItem });

 

Now, i cannot seem to figure out how to edit properties.  It would seem the process is to create the item then edit it.  I'm trying a separate function to get the process down, but no matter what option i try, i get Error 1321, EditItemRevisionFailed. Like before, i feel like i'm missing something. Fails at UpdateItemProperties.

 

try
{
    var itemResult = connection.WebServiceManager.ItemService.GetLatestItemByItemNumber("Mike1");
    MessageBox.Show($"Found item: {itemResult.ItemNum}");
                    
    var propDefs = connection.WebServiceManager.PropertyService.GetPropertyDefinitionsByEntityClassId("ITEM");
    var stockPropDef = propDefs.FirstOrDefault(p => p.Id == 99);                                
    
    var editItems = connection.WebServiceManager.ItemService.EditItems(new long[] { itemResult.RevId });

    if (editItems != null && editItems.Length > 0)
    {
        MessageBox.Show("Item in edit mode");
        
        var propArray = new PropInstParamArray()
        {
            Items = new PropInstParam[]
            {
                new PropInstParam()
                {
                    PropDefId = 99,
                    Val = "STOCK-TEST-001"
                }
            }
        };

        connection.WebServiceManager.ItemService.UpdateItemProperties(
            new long[] { editItems[0].Id },
            new PropInstParamArray[] { propArray });

        connection.WebServiceManager.ItemService.UpdateAndCommitItems(editItems);

        MessageBox.Show("Properties updated and committed!");
    }

}

  

0 Likes
Message 5 of 5

mslosarKES
Participant
Participant

Finally got it.

 

So, the process in code is very much like the manual process. From a file standpoint, think, find it, check it out, make your changes, check it back in.

 

For an item that is, find it, put it into edit mode, make your changes and check it back it (UpdateItemProperties). But, there is a slight catch. When you put it into edit mode, you are creating a revision/history for the Item. So, once you are in edit mode, you have a RevId.  When calling UpdateItemProperties, you have to pass the RevId and NOT the item's ID which you don't have the access to change which is why i was getting 1321.

 

 

0 Likes