Announcement

Collapse
No announcement yet.

Creating Invoice record using BPM does not copy Item List field.

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

  • Creating Invoice record using BPM does not copy Item List field.

    I am trying to create a new Invoice record using fields from another Invoice. All fields seem to copy properly except for the Invoice Item list.

    1 thing to note is I do have custom fields and calculations for the Invoice Items. I've attached a screenshot of my config for reference.

    Is there a way to copy all items from one Invoice to another Invoice using Formula Script instead?

    Thank you.

    Click image for larger version

Name:	Screenshot 2024-01-31 160430.png
Views:	156
Size:	61.6 KB
ID:	102238


  • #2
    itemList is a list of Item so it won't be copied as it is an object. you can use the formula instead and loop through the itemList of current invoice and create new itemList for nex invoice based on data in the first one.

    Comment


    • jaykay89
      jaykay89 commented
      Editing a comment
      Ok how would I access the itemList in formula and also assign a new itemlist for a new record I am creating?

      I have the basic code below but am unsure how to access certain fields and to also assign related fields such as Invoice Items.

      PHP Code:

      //Not sure this is how I access and itemList from the the target entity(Invoice).
      $itemList bpm\targetEntity\attribute('itemList');
      $count array\length($itemList);

      ifThen(
          
      $count 0,
          (
              
      $i=0;
              while
                  
      $i $count,
                  (
                      
      $item array\at($itemList$i);

                      
      //Add new item to newly created Invoice.
                      //???

                      
      $i $i+1;
                  )    
              )
          )

      ); 

  • #3
    Copying items from Quote to Invoice.

    PHP Code:
    $quoteId workflow\targetEntity\attribute('id');
    $itemIds record\findMany('QuoteItem'100'order'null'quoteId'$quoteId);

    $newItemList = list();
    $i 0;

    while (
    $i array\length($itemIds)) {
        
    $itemId array\at($itemIds$i);
        
        
    $item record\fetch('QuoteItem'$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


    • jaykay89
      jaykay89 commented
      Editing a comment
      Thank you Yuri. This formula worked for me. Cheers!
      Last edited by jaykay89; 02-02-2024, 02:43 AM.

  • #4
    Hey,

    First you should know that you can't do both at the sametime meaning, add first action (Create Record) choose all fields as per image above except the itemList don't add it. Then add another action (Update Created Record) choose the newly created invoice and in the formula field use the code below, this code will get all the invoiceItem list of the previous invoice and then create similar to the new created invoice.

    PHP Code:
    $count record\count('InvoiceItem''invoiceId='id);
    $itemList record\findRelatedMany('Invoice'id'items'$count'createdAt''desc');

    $i 0;

    while (
    $i array\length($itemList)) {
        
        
    $item record\fetch('InvoiceItem'array\at($itemList$i));

        if (
    $item) {
            
            
    record\create('InvoiceItem',
                
    // replace theCreatedUnvoice with the id of the createdinvoice task (if you click on it, you will find an id)
                
    'invoiceId'id,
                
    'name'object\get($item'name'),
                
    'productId'object\get($item'productId'),
                
    'quantity'object\get($item'quantity'),
                
    'taxRate'object\get($item'taxRate'),
                
    'listPrice'object\get($item'listPrice'),
                
    'unitPrice'object\get($item'unitPrice'),
                
    'discount'object\get($item'discount'),
                
    'amount'object\get($item'amount'),
                
    'unitWeight'object\get($item'unitWeight'),
                
    'weight'object\get($item'weight'),
                
    'description'object\get($item'description')
            );
        }
        
        
    $i $i 1;

    Comment


    • yuri
      yuri commented
      Editing a comment
      I wouldn't recommend creating invoice items separately, but setting them all at once as I did in my example. Otherwise, hooks and total calculations will be bypassed

      Sorting by 'order' will preserve an item order.

    • rabii
      rabii commented
      Editing a comment
      thanks for sharing
Working...
X