Announcement
Collapse
No announcement yet.
Creating Sales Orders from Leads
Collapse
X
-
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));
}
}
});
});
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
}
}
}
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);
}
}
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.
Leave a comment:
-
i understand but what kind of data that you need to be copied from the lead to the new sales order ?
Leave a comment:
-
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
Leave a comment:
-
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 ?
Leave a comment:
-
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 ?
Leave a comment:
-
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,
MatejTags: None
Leave a comment: