I have tried to assign our existing profile before create Order, and while execute the code, no exception occurred but when I went to revisit the same created Order it shows 'Default' profile.
When an order is created, it will use the "Orders created as" value in the user's current profile as the order ownership profile. To change it, you must perform an operation on an OPEN order to modify its value. The code below sets the ownership profile. Don't forget that that your current profile and the filters attached to your profile may limit the possible values existing profiles that are valid for the ownership profile.
The code below also provides an example of setting the Trust Account. You should check that the user login has succeeded (TryAuthenticate()) before you call interfaces that exist only on the server (such as IAccountsManager). Otherwise GetService() will return null and this will lead to an exception in your code.
Be aware the ChooseProfile event will only be called if the user has more than one profile AND none have been set as default.
Below is a basic framework for creating an order. It is a bit from memory but should be pretty close.
using (SelectServer ss = new SelectServer(new Uri(server), creds))
{
// Subscribe to profile handler.
ss.ChooseProfile += new EventHandler<ChooseProfileEventArgs>(ss_ChooseProfile);
// Make sure login worked before doing anything else.
string reason;
if (!ss.TryAuthenticate(out reason))
{
string errormessage = "Login to Select Server failed: " + reason;
...
return;
}
// Get Trust account
IAccountsManager accMgr = ss.GetService<IAccountsManager>();
ITrustAccountInfo trustAccount = accMgr.TrustAccounts.Where(t => t.Code == trustAccountName).FirstOrDefault();
// Load up spec as needed. Provided example of setting order trust account.
OrderCreationSpec spec = new OrderCreationSpec();
spec.BaseNumber = "NewOrder";
spec.SettlementType = SettlementType.CDF;
spec.Settings.Add("TrustAccountId", trustAccount.ID.ToString());
// Call the orderstore to create the new order.
IOrder order = ...
// Get profile
IProfileManager profileMgr = ss.GetService<IProfileManager>();
IProfileInfo profile = profileMgr.Profiles.Where(t => t.Path == profileName).FirstOrDefault();
// Set the order ownership profile.
order.SetProperty("OwnershipProfile", profile)
// Do work...
}
I am going to assigning existing profile "Default\\Accounting\\<Profile Name>" is only to get Auto generate Order Number. For this first provided the Trusted Account and then activated the Profile. Please find the below code and let me know is it right approach or anything missing.
// Get Trust account
IAccountsManager accMgr = ss.GetService<IAccountsManager>();
ITrustAccountInfo trustAccount = accMgr.TrustAccounts.Where(t => t.Code == "Escrow5").FirstOrDefault();
// Load up spec as needed. Provided example of setting order trust account.
OrderCreationSpec spec = new OrderCreationSpec();
spec.SettlementType = SettlementType.CDF;
spec.Settings.Add("TrustAccountId", trustAccount.ID.ToString());
// Get profile
IProfileManager profileMgr = ss.GetService<IProfileManager>();
IProfileInfo profile = profileMgr.Profiles.Where(t => t.Path == "Default\\Accounting\\<Profile Name>").FirstOrDefault();
if (profile != null)
{
//Activate Profile
profileMgr.ActiveProfile = profileMgr.GetProfile(profile.ID);
}
// Fetch a new order from the order store.
IOrderStore os = ss.GetService<IOrderStore>();
IOrder order = os.NewOrder();
// Set the order ownership profile.
order.SetProperty("OwnershipProfile", profile);
// Cast to dynamic to make order handling easier.
dynamic objOrder = (dynamic)order;