Announcement

Collapse
No announcement yet.

Workflow to create Sales order from Opportunity.

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

  • Workflow to create Sales order from Opportunity.

    I am implementing an automation to create a "Sales Order" directly from an "Opportunity" once the "Opportunity" is marked as Closed Won. Currently, I manually create the "Sales Order" from the "Opportunity's" Bottom Panel, where "Opportunity Items" successfully map to "Sales Order Items" without any issues. However, in the automated workflow I've created, although the "Sales Order" is being generated, the "Opportunity Items" are not mapping to the "Sales Order Items" as expected. Could anyone help me resolve this mapping issue in the automation?​

    Relationships :-
    Opportunity opportunity One-to-Many salesOrders Dispatch
    ​Opportunity Item cOpportunityItem One-to-Many cSalesOrderItemsSales Order Item​
    Last edited by Ashif Malayil; 10-30-2024, 09:54 AM.

  • #2
    You don't need additional custom relationships in this case. There is already a solution to your request: https://forum.espocrm.com/forum/gene...267#post102267.
    Only in the code, replace everything related to Quote and Quote Items with Sales Order and Sales Order Items.

    Comment


    • #3
      The shared formula is for Quote to Invoice (hope Quote items to Invoice items), In my scenario It's should be (Opportunity Items to Sales Order items). I have modified the formula according to that.

      Created workflow under the condition of Closed Won from (Opportunity). Action is Create a Related record (Sales Order) and mapped required fields. So should i add this formula there itself?

      Formula:-

      $opportunityId = workflow\targetEntity\attribute('id');
      $itemIds = record\findMany('OpportunityItem', 100, 'order', null, 'opportunityId', $opportunityId);

      $newItemList = list();
      $i = 0;

      while ($i < array\length($itemIds)) {
      $itemId = array\at($itemIds, $i);

      $item = record\fetch('OpportunityItem', $itemId);

      if (!$item) {
      $i = $i + 1;
      continue;
      }

      $quantity = object\get($item, 'quantity');
      $productId = object\get($item, 'productId');
      $name = object\get($item, 'name');
      $unitPrice = object\get($item, 'unitPrice');
      $listPrice = object\get($item, 'listPrice');
      $unitPriceCurrency = object\get($item, 'unitPriceCurrency');
      $listPriceCurrency = object\get($item, 'listPriceCurrency');
      $taxRate = object\get($item, 'taxRate');

      $newItem = object\create();

      object\set($newItem, 'quantity', $quantity);
      object\set($newItem, 'productId', $productId);
      object\set($newItem, 'name', $name);
      object\set($newItem, 'unitPrice', $unitPrice);
      object\set($newItem, 'listPrice', $listPrice);
      object\set($newItem, 'unitPriceCurrency', $unitPriceCurrency);
      object\set($newItem, 'listPriceCurrency', $listPriceCurrency);
      object\set($newItem, 'taxRate', $taxRate);

      $newItemList = array\push($newItemList, $newItem);

      $i = $i + 1;
      }

      itemList = $newItemList;

      Comment


      • #4
        Created workflow under the condition of Closed Won from (Opportunity). Action is Create a Related record (Sales Order) and mapped required fields
        First part of Workflow:

        Click image for larger version  Name:	image.png Views:	0 Size:	24.2 KB ID:	111877

        Second part with code:

        Click image for larger version  Name:	image.png Views:	0 Size:	82.5 KB ID:	111878

        Code:
        $opportunityId = workflow\targetEntity\attribute('id');
        
        $itemIds = record\findMany('OpportunityItem', 100, 'order', null, 'opportunityId', $opportunityId);
        
        $newItemList = list();
        $i = 0;
        
        while ($i < array\length($itemIds)) {
            $itemId = array\at($itemIds, $i);
            
            $item = record\fetch('OpportunityItem', $itemId);
            
            if (!$item) {
                continue;
            }
            
            $quantity = object\get($item, 'quantity');
            $productId = object\get($item, 'productId');
            $name = object\get($item, 'name');
            $unitPrice = object\get($item, 'unitPrice');
            $listPrice = object\get($item, 'listPrice');
            $unitPriceCurrency = object\get($item, 'unitPriceCurrency');
            $listPriceCurrency = object\get($item, 'listPriceCurrency');
            $taxRate = object\get($item, 'taxRate');
            
            $newItem = object\create();
            
            object\set($newItem, 'quantity', $quantity);
            object\set($newItem, 'productId', $productId);
            object\set($newItem, 'name', $name);
            object\set($newItem, 'unitPrice', $unitPrice);
            object\set($newItem, 'listPrice', $listPrice);
            object\set($newItem, 'unitPriceCurrency', $unitPriceCurrency);
            object\set($newItem, 'listPriceCurrency', $listPriceCurrency);
            object\set($newItem, 'taxRate', $taxRate);
            
            $newItemList = array\push($newItemList, $newItem);
            
            $i = $i + 1;
        }
        
        itemList = $newItemList;​

        Comment


        • #5
          Thank you, victor! This was a great help. The formula is working perfectly, I modified it with some of my custom fields as well, and it works fine.

          Comment

          Working...
          X