I can't get this to work @Markus.Koechl
I get errors when trying to run it. It errors on trying to make the new change order and I don't understand why, I've tried so many things I'm basically lost.
there is something I'm not doing right with the change order number id. I can get it I know what the number is, but only if I put null it will run but gives errors because it can't find the number id....
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using Autodesk.Connectivity.WebServices;
using Autodesk.Connectivity.WebServicesTools;
using Autodesk.DataManagement.Client.Framework.Vault.Currency.Connections;
using Autodesk.DataManagement.Client.Framework.Vault.Services.Connection; // Keep if needed, but login uses WebServices directly here
namespace API_Onboarding_ConsoleApp2
{
class Program
{
private static readonly string CO_ENTITY_CLASS_ID;
static void Main(string[] args)
{
// --- Connection Settings ---
string dataServer = "1.1.1.1"; // Server
string vaultName = "VAULT";
string userName = "Administrator";
string password = "**********"; // Consider more secure way?
WebServiceManager vaultConnection = null;
try
{
// --- Connect To Vault ---
ServerIdentities serverId = new ServerIdentities
{
DataServer = dataServer,
FileServer = dataServer // Often the same as DataServer
};
UserPasswordCredentials credentials = new UserPasswordCredentials(serverId, vaultName, userName, password, LicensingAgent.Client);
vaultConnection = new WebServiceManager(credentials); // Logs in
Console.WriteLine("Successfully logged into Vault.");
// --- Prepare ECO Data ---
string ecoTitle = "New Design Change (via API)";
string ecoDescription = "Implementation of design modifications (via API)";
DateTime dueDate = DateTime.Now.AddDays(30); // Example due date
// --- 2. Get Numbering Scheme ID ---
getChangeOrderRoutingId.ListAvailableRoutings(vaultConnection);
long numbSchemeId = listChangeOrderNumberSchemes.ListAvailableCOSchemes(vaultConnection);
if (numbSchemeId == -1)
{
throw new Exception("Failed to find the required ECO numbering scheme.");
}
// --- 3. Get Routing ID ---
Console.WriteLine("\nChecking for PMC-ECO-ROUTE...");
ChangeOrderService coService = vaultConnection.ChangeOrderService;
// Get default workflow and its routings
Workflow workflow = coService.GetDefaultWorkflow();
Routing[] routings = coService.GetRoutingsByWorkflowId(workflow.Id);
// Find the specific routing by name
string desiredRoutingName = "PMC-ECO-ROUTE";
Routing targetRouting = routings?.FirstOrDefault(r => r.Name.Equals(desiredRoutingName, StringComparison.OrdinalIgnoreCase));
if (targetRouting == null)
{
throw new Exception($"Could not find the routing '{desiredRoutingName}'. Please check Vault configuration.");
}
// Set this as the default routing
coService.SetDefaultRouting(targetRouting.Id);
Console.WriteLine("Confirmed PMC-ECO-ROUTE");
// --- 4. Create the Change Order ---
Console.WriteLine("\nAttempting to create ECO...");
try
{
// Ensure all required parameters are valid
if (targetRouting == null)
{
throw new Exception("Routing or Numbering Scheme is not properly initialized.");
}
// Validate dueDate to ensure it is not in the past
if (dueDate <= DateTime.Now)
{
throw new Exception("The due date must be in the future.");
}
// Create the ECO
// ChangeOrder newEco = coService.AddChangeOrder(
// ecoTitle, // title
// ecoDescription, // description
// dueDate, // dueDate
// targetRouting.Id, // routingId
// categoryId, // categoryId
// defaultNumScheme.SchmID, // <<< Pass the scheme ID here
// new long[0], // fileMastIds
// new long[0], // itemMastIds
// null // propInsts
//);
ChangeOrder newEco = coService.AddChangeOrder(
targetRouting.Id, // long routingId
null, // string changeOrderNumber (Using null for auto-numbering)
ecoTitle, // string title
ecoDescription, // string description
dueDate, // DateTime approveDeadline
new long[0], // long[] itemMasterIds
new long[0], // long[] attachmentIds
new long[0], // long[] fileMasterIds
null, // PropInst[] properties
null, // AssocPropItem[] assocProperties
null, // MsgGroup[] comments
null // Email[] notifyEmails
);
Console.WriteLine($"Successfully created ECO: {newEco.Num} (ID: {newEco.Id})");
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\n--- Failed to create ECO ---");
Console.WriteLine($"Error Code: {ex.Message}");
Console.WriteLine($"Stack Trace: {ex.StackTrace}");
Console.ResetColor();
}
// --- Optional: Add files/items or transition state here ---
// coService.AddChangeOrderItems(...)
// coService.UpdateChangeOrderState(...)
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\n--- An error occurred ---");
Console.WriteLine($"Error Message: {ex.Message}");
Console.WriteLine($"Stack Trace: {ex.StackTrace}");
// Log the exception details appropriately
Console.ResetColor();
}
finally
{
// --- Logout/Dispose ---
// Ensure the connection is disposed to release resources and license
// --- Logout/Dispose ---
if (vaultConnection != null)
{
Console.WriteLine("\nLogging out...");
vaultConnection.Dispose(); // This logs out and cleans up
Console.WriteLine("Logged out.");
}
}
Console.WriteLine("\nPress Enter to exit.");
Console.ReadLine();
}
}
}