Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Supply chain | Supply Chain Management, Commerce
Answered

How to find employee record in custom control

(0) ShareShare
ReportReport
Posted on by 1,447

I have a custom control that is on the transaction page. It needs to show the name of the person that is logged in.

I saw in FiscalizationManager.ts which triggers to use. The StaffID I pass in has a value and I can confirm that it is the ID I logged in with, but it returns a null for the Employee record.

Here is my typescript file:

***

import {
    CartViewCustomControlBase,
    ICartViewCustomControlState,
    ICartViewCustomControlContext,
    CartChangedData
} from "PosApi/Extend/Views/CartView";
import {
    GetLoggedOnEmployeeClientRequest, GetLoggedOnEmployeeClientResponse
} from "PosApi/Consume/Employees";
import { IExtensionContext } from "PosApi/Framework/ExtensionContext";
import { IRuntime } from "PosApi/Framework/Runtime";
import { ObjectExtensions } from "PosApi/TypeExtensions";
import { StringExtensions } from "PosApi/TypeExtensions";
import { ProxyEntities, ClientEntities } from "PosApi/Entities";
export default class CartCustDetailsControl extends CartViewCustomControlBase {
    private extensionContext: IExtensionContext;
        /* ExtCartCustDetailControl is the script ID in CartCustDetailsControl.html */
    private static readonly TEMPLATE_ID: string = "ExtCartCustDetailControl";
    public readonly isCustomerSelected: Computed<boolean>;
    public readonly cGrpHolder: Computed<string>;
    public readonly pGrpHolder: Computed<string>;
    public readonly salesTakerHolder: Computed<string>;
    private readonly _customer: Observable<ProxyEntities.Customer>;
    private readonly _employee: Observable<ProxyEntities.Employee>;
   
   
    public readonly title: Observable<string>;
    constructor(id: string, context: ICartViewCustomControlContext) {
        super(id, context);
        this.extensionContext = context;
        this._customer = ko.observable(null);
        this._employee = ko.observable(null);
        this.isCustomerSelected = ko.computed(() => !ObjectExtensions.isNullOrUndefined(this._customer()));
        //this.isVisible = true;
        this.title = ko.observable("Details");
        this.cGrpHolder = ko.computed(() => {
            if (this.isCustomerSelected()) {
                return this._customer().CustomerGroup;
            }
            return StringExtensions.EMPTY;
        });
        this.pGrpHolder = ko.computed(() => {
            if (this.isCustomerSelected()) {
                return this._customer().PriceGroup;
            }
            return StringExtensions.EMPTY;
        });
        this.salesTakerHolder = ko.computed(() =>{
                return ObjectExtensions.isNullOrUndefined(this._employee()) ? StringExtensions.EMPTY : this._employee().Name;
            });
        this.cartChangedHandler = (data: CartChangedData) => {
            this._customer(data.customer);
        };
    }
    public init(state: ICartViewCustomControlState): void {
        this._customer(state.customer);
        this._employee(this.GetEmployee(state.cart.StaffId)[0]);
    }

    public onReady(element: HTMLElement): void {
        ko.applyBindingsToNode(element, {
            template: {
                name: CartCustDetailsControl.TEMPLATE_ID,
                data: this
            }
        });
    }
    // Retrieve the employee record for the passed in staffId
    private GetEmployee(_StaffId: string): Promise <ProxyEntities.Employee[]>{
        let runtime: IRuntime = this.extensionContext.runtime;
        return runtime.executeAsync(new GetLoggedOnEmployeeClientRequest(_StaffId))
            .then((response: ClientEntities.ICancelableDataResult<GetLoggedOnEmployeeClientResponse>): ProxyEntities.Employee => {
                return response.data.result;
            })
            .catch(() => {
                return null;
            });
    }
}

*** 

Can anybody help me figure out why GetEmployee is returning a null employee record instead of the actual record?

Any help is much appreciated

  • Retha Profile Picture
    1,447 on at
    RE: How to find employee record in custom control

    This worked, thank you

  • Verified answer
    Oksana Kovaliova Profile Picture
    3,597 on at
    RE: How to find employee record in custom control

    Hi, method GetEmployee returns Promise, not Employee.

    So what code is doing is assigning _employee variable of type Employee a value of type Promise<Employee>... not sure if it really can work this way.  

    Try changing this._employee(this.GetEmployee(state.cart.StaffId)[0]);

    to

    this.GetEmployee(state.cart.StaffId)

        .then((_returnedEmployee: ProxyEntities.Employee) => {

             this._employee(_returnedEmployee);

        });

    If you still have null - check if code goes inside Catch statement by any reason: .catch(() => {

                   return null;

               });

  • Retha Profile Picture
    1,447 on at
    RE: How to find employee record in custom control

    I don't know Fiddler much but when I ran fiddler it found GetLoggedOnEmployeeClientRequest when I looked for a session but there was no entry for GetLoggedOnEmployeeClientResponse.

    I in the end gave up on the POS side and created a post trigger extension on the CRT side for  GetCartServiceRequest and then call the standard view "EMPLOYEESVIEW" with the cart's staffID and retrieve the name and added it to an extension property and used then the latter on the POS side as the value for the salestakerHolder:

    I changed the code to this:

    ****

    import {

       CartViewCustomControlBase,

       ICartViewCustomControlState,

       ICartViewCustomControlContext,

       CartChangedData

    } from "PosApi/Extend/Views/CartView";

    import { ObjectExtensions } from "PosApi/TypeExtensions";

    import { StringExtensions } from "PosApi/TypeExtensions";

    import { ProxyEntities } from "PosApi/Entities";

    export default class CartCustDetailsControl extends CartViewCustomControlBase {

       /* TMCExtensionCartCustDetailControl is the script ID in CartCustDetailsControl.html */

       private static readonly TEMPLATE_ID: string = "TMCExtensionCartCustDetailControl";

       public readonly isCustomerSelected: Computed<boolean>;

       public readonly cGrpHolder: Computed<string>;

       public readonly pGrpHolder: Computed<string>;

       public readonly salesTakerHolder: Computed<string>;

       private readonly _customer: Observable<ProxyEntities.Customer>;

       private readonly _cart: Observable<ProxyEntities.Cart>;

       public readonly title: Observable<string>;

       constructor(id: string, context: ICartViewCustomControlContext) {

           super(id, context);

           this._customer = ko.observable(null);

           this._cart = ko.observable(null);

           this.isCustomerSelected = ko.computed(() => !ObjectExtensions.isNullOrUndefined(this._customer()));

           this.title = ko.observable("Details");

           this.cGrpHolder = ko.computed(() => {

               if (this.isCustomerSelected()) {

                   return this._customer().CustomerGroup;

               }

               return StringExtensions.EMPTY;

           });

           this.pGrpHolder = ko.computed(() => {

               if (this.isCustomerSelected()) {

                   return this._customer().PriceGroup;

               }

               return StringExtensions.EMPTY;

           });

           this.salesTakerHolder = ko.computed(() => {

               if (!ObjectExtensions.isNullOrUndefined(this._cart())) {

                   let cartProperties: ProxyEntities.CommerceProperty[] = this._cart().ExtensionProperties.filter(

                       (value: ProxyEntities.CommerceProperty): boolean => { return value.Key === "StaffName"; });

                       return cartProperties.length > 0 ? cartProperties[0].Value.StringValue : StringExtensions.EMPTY;

                   }

                   return StringExtensions.EMPTY;

           });

           this.cartChangedHandler = (data: CartChangedData) => {

               this._customer(data.customer);

               this._cart(data.cart);

           };

       }

       public init(state: ICartViewCustomControlState): void {

           this._customer(state.customer);

           this._cart(state.cart);

       }

       public onReady(element: HTMLElement): void {

           ko.applyBindingsToNode(element, {

               template: {

                   name: CartCustDetailsControl.TEMPLATE_ID,

                   data: this

               }

           });

       }

    }

    ****

  • ram shenkar Profile Picture
    515 on at
    RE: How to find employee record in custom control

    The code looks good and should not have any issues. You can run this code in any other file or check-in fiddler what GetEmployee API is returning.  

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Supply chain | Supply Chain Management, Commerce

#1
Zain Mehmood Profile Picture

Zain Mehmood 462 Moderator

#2
Danny Bilodeau Profile Picture

Danny Bilodeau 156 Moderator

#3
Laurens vd Tang Profile Picture

Laurens vd Tang 120 Super User 2025 Season 1

Featured topics

Product updates

Dynamics 365 release plans