Not able to set the Existing Profile before Create Order

Discussions related to custom development with Select.
Post Reply
jturek
Posts: 14
Joined: Fri Jul 20, 2018 6:17 pm

Not able to set the Existing Profile before Create Order

Post by jturek »

Hi Team,

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.

Please correct me if anything missing.

1. Assign existing profile code

Code: Select all

 NetworkCredential creds = new NetworkCredential(username, password, domainName);
                using (SelectServer ss = new SelectServer(new Uri(server), creds))
                {
                    // Subscribe to profile handler.
                    ss.ChooseProfile += new EventHandler<ChooseProfileEventArgs>(ss_ChooseProfile);

                    IProfileManager profileMgr = ss.GetService<IProfileManager>();

                    // Get desired profile information.
                    IProfileInfo profileInfo = profileMgr.Profiles.FirstOrDefault(t => t.Path == "Default\\Accounting\\<Existing profile Name>");
                    if (profileInfo != null)
                    {
                        //*****  Two ways to search for IProfile object to allow gets or sets of profile properties.

                        // 1) Search profiles by IProfileInfo.
                        IProfile profile = profileMgr.GetProfile(profileInfo);

                        profile["OrdersCreatedAs"] = profile;
                        profileMgr.ApplyChanges(profile);
                    }

                    string reason;
                    if (!ss.TryAuthenticate(out reason))
                    {
                        // Write login failure reason to console and exit.                
                        return reason;
                    }

                    // At this stage Logic is success
                    OrderNumber = CreateNewOrder(ss, objSoftProCreateOrderRequest);
2. After Create Order the Existing Profile not assigned, It shows the Default Profile.
Attachments
Default Profile.PNG
Default Profile.PNG (34.67 KiB) Viewed 1046 times
BobRichards
Posts: 1377
Joined: Wed Jan 15, 2014 3:50 pm
Location: Raleigh, NC
Contact:

Re: Not able to set the Existing Profile before Create Order

Post by BobRichards »

  • 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.

Code: Select all

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...
}
Bob Richards, Senior Software Developer, SoftPro
jturek
Posts: 14
Joined: Fri Jul 20, 2018 6:17 pm

Re: Not able to set the Existing Profile before Create Order

Post by jturek »

Hi Bob,

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.

Code: Select all

 		// 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;
BobRichards
Posts: 1377
Joined: Wed Jan 15, 2014 3:50 pm
Location: Raleigh, NC
Contact:

Re: Not able to set the Existing Profile before Create Order

Post by BobRichards »

Nothing is obviously wrong. The real question is does it work for you.
Bob Richards, Senior Software Developer, SoftPro
jturek
Posts: 14
Joined: Fri Jul 20, 2018 6:17 pm

Re: Not able to set the Existing Profile before Create Order

Post by jturek »

Yes It works.
Post Reply