Announcement

Collapse
No announcement yet.

Restriction of account selection based on attribute

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Restriction of account selection based on attribute

    Hello!

    I would need some help with the following situation by the EspoCrm community :-)

    We have bought the Advanced Sales Pack. We would like to use EspoCRM as CRM and also for simple SCM Tasks. So, we are differencing Accounts in supplier and customer:

    Account: Has an attribute "ACCOUNTTYPE" as single choice, with possible values "customer" and "supplier". An account can be customer and supplier
    simultaneously.

    Quote: has an n:1 Relationship with Account (default).

    For the creation of new quotes, choosing an related account is necessary (see first picture). How would it be possible to restrict the selection of accounts in "new quote" for our employees only to Accounts with "ACCOUNTTYPE"="customer"?



    Click image for larger version  Name:	createnewquote.PNG Views:	0 Size:	24.8 KB ID:	74627Click image for larger version  Name:	selection_account.jpg Views:	0 Size:	52.3 KB ID:	74625
    Attached Files

  • #2
    You could create filter for account field, but employee still be able to search for other accounts. You can also write a hook which will show error when employee try to assign customer with different type than customer.

    Comment


    • #3
      Hi there, basically what you're referring to is a type of a validation before creating the quote - great way how to do validations is via beforeSave() method in PHP file (Quote.php for example).

      What you would do is basically fetch the Customer record from New Quote form and check the value of accounttype - and the simply just do condition check that if accounttype is not equal to customer, then throw new exception with a message "Cannot create quote for a supplier" for example.

      Have a nice day

      Last edited by alter; 09-20-2021, 02:53 PM.

      Comment


      • #4
        Here is an simple example of a logic I would probably use for achieving this:

        (I hope there are no errors since I wrote it in php fiddle and not in PhpStorm )


        PHP Code:

        protected function beforeSave(Entity $quote, array $options = [])
        {
        $account $quote->get('account');

        if (
        $account != null && $this->isAccountTypeCustomer($account) === false)
        {
        throw new 
        Error('You cannot create Quote for this Account. Creating quotes is allowed only for Customer type Accounts.');
        }
        }

        private function 
        isAccountTypeCustomer(Entity $account): bool

        {
        $accountType $account->get('type');

        $isAccountCustomer false;

        if (
        $accountType === 'Customer')
        {
        $isAccountCustomer true;
        }

        return 
        $isAccountCustomer;


        Comment


        • #5
          I can help you, write me privately

          Comment

          Working...
          X