Creating Sales Orders from Leads

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Espire
    Junior Member
    • Jun 2023
    • 17

    Creating Sales Orders from Leads

    Hello,
    Is it please somehow possible to create Sales Orders directly from Leads?
    I've created Many-to-One​ relationship, added it to the Detail layout but after selecting the lead from the list nothing happens unlike when opportunity is selected(everything is filled out). Is it possible to somehow copy or create logic for it so it will behave like selected opportunity?

    Thanks,
    Matej
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    #2
    what do you mean by select the lead ? as far as i know how it works is when you are on opportunity detail and create a related sales order then the system will automatically copy details from opportunity to the new sales order. is this what you want to achieve ?
    Rabii
    Web Dev

    Comment

    • Espire
      Junior Member
      • Jun 2023
      • 17

      #3
      Yes. After selecting opportunity, everything is filled out but when lead is selected from the list as seen below on the screenshots, nothing happens.
      Last edited by Espire; 06-22-2023, 10:25 AM.

      Comment

      • rabii
        Active Community Member
        • Jun 2016
        • 1250

        #4
        i see what you mean. what data you want to be copied from the lead to the sales quote ? there are no items in the leads so what is the purpose of your request ?
        Rabii
        Web Dev

        Comment

        • Espire
          Junior Member
          • Jun 2023
          • 17

          #5
          We need same data to be filled in as if Opportunity is selected because we are using only Leads and Contacts but not Opportunities because it's not needed.
          Kind regards

          Comment

          • rabii
            Active Community Member
            • Jun 2016
            • 1250

            #6
            i understand but what kind of data that you need to be copied from the lead to the new sales order ?
            Rabii
            Web Dev

            Comment

            • Espire
              Junior Member
              • Jun 2023
              • 17

              #7
              Billing and Shipping Address, Amount and Name.

              Comment


              • rabii
                rabii commented
                Editing a comment
                do you have these fields set in your lead entity ?
            • Espire
              Junior Member
              • Jun 2023
              • 17

              #8
              Originally posted by Espire
              Billing and Shipping Address, Amount and Name.
              Yes, tried to select a Lead where address, name and amount is filled but it is still not working.

              Comment

              • rabii
                Active Community Member
                • Jun 2016
                • 1250

                #9
                Alright, here is what you need to do to get to work with Lead selection follow steps below: Please make sure you created the folders in the paths given below if they don't exist.

                1 - Create a custom view for the lead.js field on the sales order like below under custom:views/sales-order/fields (lead.js)

                PHP Code:
                /*********************************************************************************
                 * Code by Rabii
                 ***********************************************************************************/
                
                define('custom:views/sales-order/fields/lead', 'views/fields/link', function (Dep) {
                
                    return Dep.extend({
                
                        select: function (model) {
                            Dep.prototype.select.call(this, model);
                
                            if (this.model.isNew()) {
                                this.ajaxGetRequest('SalesOrder/action/getAttributesFromLead', {
                                    leadId: model.id
                                }).then(function (attributes) {
                                    var a = {};
                
                                    for (var item in attributes) {
                                        if (~['amountCurrency'].indexOf(item)) {
                                            continue;
                                        }
                
                                        if (~['name'].indexOf(item)) {
                                            if (this.model.get(item)) {
                                                continue;
                                            }
                                        }
                
                                        a[item] = attributes[item];
                                    }
                
                                    this.model.set(a);
                
                                    this.model.set('amountCurrency', attributes.amountCurrency, {ui: true});
                                }.bind(this));
                            }
                        }
                    });
                }); 
                
                2 - Now assign the new custom view to the lead field in entityDefs of the salesOrder under custom\Espo\Custom\Resources\metadata\entityDefs (SalesOrder.json)

                PHP Code:
                {
                    "fields": {
                        "lead": {
                            "type": "link",
                            "view": "custom:views/sales-order/fields/lead"
                        }
                    },
                    "links": {
                        "lead": {
                            "type": "belongsTo",
                            "foreign": "salesOrders",
                            "entity": "Lead",
                            "audited": false,
                            "isCustom": true
                        }
                    }
                }
                3 - Create a custom controller SalesOrder which extends the parent controller made by the extension under custom\Espo\Custom\Controllers (SalesOrder.php) and paste in teh code below:

                PHP Code:
                <?php
                /*********************************************************************************
                 * Code by Rabii
                 ***********************************************************************************/
                
                namespace Espo\Custom\Controllers;
                
                use \Espo\Core\Exceptions\BadRequest;
                use \Espo\Modules\Sales\Controllers\SalesOrder as ParentSalesOrderController;
                
                class SalesOrder extends ParentSalesOrderController
                {
                    public function actionGetAttributesFromLead($params, $data, $request)
                    {
                        $leadId = $request->get('leadId');
                        if (empty($leadId)) {
                            throw new BadRequest();
                        }
                
                        return $this->getRecordService()->getAttributesFromLead($leadId);
                    }
                
                }
                4 - Last step is to create a custom service which extends the parent service made by the extension under custom\Espo\Custom\Services (SalesOrder.php) and paste the code below
                PHP Code:
                <?php
                /*********************************************************************************
                 * Code by Rabii
                 ***********************************************************************************/
                
                namespace Espo\Custom\Services;
                
                use Espo\Core\Exceptions\NotFound;
                use Espo\Core\Exceptions\Forbidden;
                
                use \Espo\Modules\Sales\Services\SalesOrder as ParentSalesOrderService;
                
                
                class SalesOrder extends ParentSalesOrderService
                {
                
                    public function getAttributesFromLead($leadId)
                    {        
                        $source = $this->getEntityManager()->getEntity('Lead', $leadId);
                
                        $idAttribute = 'leadId';
                
                        if (!$source) {
                            throw new NotFound();
                        }
                
                        if (!$this->getAcl()->check($source, 'read')) {
                            throw new Forbidden();
                        }
                
                        $source->loadLinkMultipleField('teams');
                
                        $attributes = [
                            'name' => $source->get('name'),
                            'teamsIds' => $source->get('teamsIds'),
                            'teamsNames' => $source->get('teamsNames'),
                            $idAttribute => $leadId,
                            'amount' => $source->get('amount'),
                            'amountCurrency' => $source->get('amountCurrency'),
                            'billingAddressStreet' => $source->get('addressStreet'),
                            'billingAddressCity' => $source->get('addressCity'),
                            'billingAddressState' => $source->get('addressState'),
                            'billingAddressCountry' => $source->get('addressCountry'),
                            'billingAddressPostalCode' => $source->get('addressPostalCode'),
                            'shippingAddressStreet' => $source->get('addressStreet'),
                            'shippingAddressCity' => $source->get('addressCity'),
                            'shippingAddressState' => $source->get('addressState'),
                            'shippingAddressCountry' => $source->get('addressCountry'),
                            'shippingAddressPostalCode' => $source->get('addressPostalCode'),
                        ];
                
                        $amount = $source->get('amount');
                
                        if (empty($amount)) {
                            $amount = 0;
                        }
                
                        if ($source->hasAttribute('createdAccountId')) {
                            $attributes['accountId'] = $source->get('createdAccountId');
                            $attributes['accountName'] = $source->get('createdAccountName');
                        }
                
                        return $attributes;
                    }
                }

                As you may noticed we are extending all existing class so that these changes will be future upgrade safe and won't cause any issues. i have also assumed that you have a new field called (amount) in the lead entity. the address on the lead will be used for both billing and shipping address hence the lead have only one main address.

                I hope this helps.
                Rabii
                Web Dev

                Comment

                • Espire
                  Junior Member
                  • Jun 2023
                  • 17

                  #10
                  Thank you very much! Will test it soon.

                  Comment

                  Working...