Announcement

Collapse
No announcement yet.

Creating Tasks in Espo CRM upon Sales Order Creation

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

  • Creating Tasks in Espo CRM upon Sales Order Creation

    Hello team,

    I hope this message finds you well. I am currently using Espo CRM with the Sales Pack and Advance Pack extensions. I have encountered a requirement where I need to create a task in the system whenever a sales order is created

    To clarify, when we receive a new order and create a sales order in the system, I would like to automatically generate a task associated with that order items . Furthermore, if the sales order contains multiple items, I would like to create multiple tasks accordingly.

    The tasks should be assigned to a specific user within the system. Could you please guide me on how to implement this functionality within Espo CRM?

    Thank you for your assistance.

  • #2
    Hi sapyantuk,

    To achieve this functionality, you should create a Workflow with:
    • Target Entity: Sales Order
    • Trigger Type: After record created
    • Action: Execute Formula Script​ (insert the formula below)
    Code:
    $itemsIDs = record\findRelatedMany('SalesOrder', id, 'items', 1000);
    
    $i = 0;
    
    while(
        $i < array\length($itemsIDs),
        (
            record\create(
                'Task',
                'name', record\attribute('SalesOrderItem', array\at($itemsIDs, $i), 'name'),
                'assignedUserId', '1' // instaed of 1 insert the ID of the specific user
            );
            $i = $i + 1;
        )
    );
    This formula script can be supplemented by adding certain fields to the created Tasks. You can describe in more detail how the Tasks should look like, so that I can finalize this formula script for you.
    Last edited by lazovic; 06-30-2023, 07:47 AM.

    Comment


    • #3
      hey @yubrajkafle​,

      As lazovic explained this could be achieve using formula, however i noticed in your comment that you want each task to be related to each sales order item, by default there no link between the two entities, in order to achieve this you need to do the steps below:

      1 - Go to Administration > Entity Manager > Task > Fields (access parent field and add the sales order item to the list)
      2 - Go to Administration > Entity Manager > Task > Relationships (Edit Children-to-Parent​ and check the sales order item as foreign link)
      3 - Go to Administration > Entity Manager > Sales Order Item > Layout > Bottom Panels (Add the Task panel to be displayed on the bottom panel by dragging the Task from disabled to enabled)

      Now you are done with the steps above, the task will have Sales Order Item as a parent and also Sales Order Item will have a Task panel on the detail view, you can now follow the steps provide by lazovic to achieve this, one addition i would add there is as below :

      PHP Code:
      // inject count automatically to cover all items
      $count entity\countRelated('items');

      $itemsIds record\findRelatedMany('SalesOrder'id'items'$count);

      $i 0;

      while(
          
      $i array\length($itemsIds),
          (
              
      record\create(
                  
      'Task',
                  
      'parentId'array\at($itemsIds$i),
                  
      'parentName''Sales Order Item',
                  
      'name'record\attribute('SalesOrderItem'array\at($itemsIds$i), 'name'),
                  
      'assignedUserId''1' // insert the ID of the specific user
              
      );
              
      $i $i 1;
          )
      );
      ​ 

      Using while new syntax

      PHP Code:
      // inject count automatically to cover all items
      $count entity\countRelated('items');

      $itemsIds record\findRelatedMany('SalesOrder'id'items'$count);

      $i 0;

      while(
      $i array\length($itemsIds)) {
          
      record\create(
              
      'Task',
              
      'parentId'array\at($itemsIds$i),
              
      'parentName''Sales Order Item',
              
      'name'record\attribute('SalesOrderItem'array\at($itemsIds$i), 'name'),
              
      'assignedUserId''1' // insert the ID of the specific user
          
      );
          
          
      $i $i 1;
      }
      ​ 
      Hope this helps
      Last edited by rabii; 06-30-2023, 01:04 PM.
      Rabii
      Web Dev

      Comment


      • lazovic
        lazovic commented
        Editing a comment
        Thanks for your additions.

      • rabii
        rabii commented
        Editing a comment
        you are welcome mate

    • #4
      I encourage all to use the new while syntax instead of old one.


      Good:
      Code:
      while ($i < $length) {
          doSomething();
        
          $i = $i + 1;
      }
      Bad:
      Code:
      while(
          $i < $length,
          (
              doSomething();
              $i = $i + 1;
          )
      );​  ​

      Comment


      • yuri
        yuri commented
        Editing a comment
        No way to define functions (within formula). Just an example.

        The new "while" is the same as in many programming languages.

      • rabii
        rabii commented
        Editing a comment
        got it
        yeah i like the new syntax for existing function like (while - if etc...)
        I have seen that you added a record\findMany which is very useful (will take it out of my custom functions now) hope you will consider entity\setAttributes(ATT1, VALUE1, ATT2, VALUE2...) this is very useful when trying to set multiple attributes of a record. been using it as a custom function and helped a lot.

        thanks

      • esforim
        esforim commented
        Editing a comment
        Need to scroll through forum for more $i and while forum. Can't wrap my head around how to use this. I understand it some sort of +1 counter and keep doing this until it count no more?

    • #5
      Yeah esforim

      The idea is that $i will be initialed with 0 and the while loop will keep executing while condition is true. e.g below formula for opportunity that has contacts you can use while in sandbox:

      PHP Code:
      while ($i array\length(contactsIds)) {
          
      output\printLine('Looping...');
          
      output\printLine($i);
          
      $i $i 1;
      }
      ​ 
      Rabii
      Web Dev

      Comment


      • esforim
        esforim commented
        Editing a comment
        Thank you. Will give this a try to upgrade my formula level

    • #6
      rabii Finally got a chance to play with this formula, would you be able to help? I don't know where I'm doing it wrong. It telling me it should be a int, but formula I reference: Post #3 and it should be correct. I obviously doing something wrong because copy/paste of your formula work fine.

      Copy of formula:
      Click image for larger version

Name:	image.png
Views:	264
Size:	13.1 KB
ID:	98215

      Hello team, I hope this message finds you well. I am currently using Espo CRM with the Sales Pack and Advance Pack extensions. I have encountered a requirement where I need to create a task in the system whenever a sales order is created To clarify, when we receive a new order and create a sales order in the system, I would


      Click image for larger version

Name:	image.png
Views:	301
Size:	19.3 KB
ID:	98213

      It cannot read the "$i", if I put a number manually it seem to work.

      Click image for larger version

Name:	image.png
Views:	257
Size:	23.8 KB
ID:	98214

      Comment


      • #7
        Hey esforim

        First thing you need to initialise the variable $i as an integer with value 0, see code below (that is why array/index give you an error because it is expecting integer but the $i is not defined):

        PHP Code:
        $i 0;

        while (
        $i array\length(contactsIds)) {
            
        output\printLine(record\attribute('Contact'array\at(contactsIds$i), 'name'));
            
        output\printLine(contactsIds);
            
        output\printLine(array\length(contactsIds));
            
        $i $i 1;
        }
        ​ 

        I have tested this code on opportunity and it works. Also please notice that record/attribute should have a singular name of the entityType like (Contact) instead of Contacts.

        I hope this helps
        Last edited by rabii; 10-04-2023, 10:49 AM.
        Rabii
        Web Dev

        Comment


        • esforim
          esforim commented
          Editing a comment
          Copy and paste without needing to do anything! Well except for some hidden text in the forum [PHP code] tag I think. It highlight in red so easily fixable after copying.

          THANK YOU.
          Last edited by esforim; 10-04-2023, 11:29 AM.

      • #8
        Originally posted by rabii View Post
        Am I being greedy or is there a better way to multiple field/value? Or is the only way is to create record\attribute per field. I'm being 'smart' with these failure of idea:

        Code:
         $i = 0;
         $m = list('name');
        // https://docs.espocrm.com/administration/formula/record/#recordfetch
        // https://docs.espocrm.com/administration/formula/object/#objectget
        
        while ($i < array\length(contactsIds)) {
            //output\printLine(record\attribute('Contact', array\at(contactsIds, $i), $m)); // error 500
            //output\printLine(record\attribute('Contact', array\at(contactsIds, $i), 'name' && 'name')); // wishful thinking, used name twice because it easier to test formula as oppose to 'firstName'
            output\printLine(record\fetch('Contact', array\at(contactsIds, $i))); // this might be the answer??? but how to combine with if it is object/get()
            $i = $i + 1;
        }​
        Reference:
        // https://docs.espocrm.com/administrat...d/#recordfetch
        // https://docs.espocrm.com/administration/formula/object/#objectget​

        Comment


        • rabii
          rabii commented
          Editing a comment
          what do you want to achieve ?

      • #9
        Try this

        PHP Code:
        $i 0;

        while (
        $i array\length(contactsIds)) {
            
            
        $o record\fetch('Contact'array\at(contactsIds$i));

            if (
        $o) {
                
        $name object\get($o'name');
            }
            
            
        output\printLine($name);
            
            
        $i $i 1;
        }
        ​ 
        Rabii
        Web Dev

        Comment


        • #10
          Originally posted by rabii View Post
          what do you want to achieve ?
          Code:
          while ($i < array\length(contactsIds)) {
          output\printLine(record\attribute('Contact', array\at(contactsIds, $i), 'name'));
          output\printLine(record\attribute('Contact', array\at(contactsIds, $i), 'firstName'));​
          output\printLine(record\attribute('Contact', array\at(contactsIds, $i), 'lastName'));​​
          output\printLine(record\attribute('Contact', array\at(contactsIds, $i), 'address'));​​​
          $i = $i + 1;
          }​​
          Basically something like this without having to create one line per field.

          Comment


          • #11
            try this, you have to fetch each attribute alone, you can't fetch them all at once:

            PHP Code:
            $i 0;

            while (
            $i array\length(contactsIds)) {
                
                
            $o record\fetch('Contact'array\at(contactsIds$i));

                if (
            $o) {
                    
            $name object\get($o'name');
                    
            $firstname object\get($o'firstName');
                    
            $lastname object\get($o'lastName');
                    
            $address object\get($o'address');
                }
                
                
            output\printLine($name);
                
            output\printLine($firstname);
                
            output\printLine($lastname);
                
            output\printLine($address);
                
                
            $i $i 1;
            }
            ​ 
            Rabii
            Web Dev

            Comment


            • esforim
              esforim commented
              Editing a comment
              I think this is the golden ticket!

              Just a discussion/chat; you reckon if there any better "speed" or benefit in using record\fetch method vs record\attribute?

              My brain thinking: record\fetch get everything so it take longer as it try to get all data.
              Whereas record\attribute it take less data because you only get what you ask for.

              Where it might be an issue is it idea of "DDOS".
              (theory speaking here)
              1 record\fetch might take lots of data but it only take 3kb of data
              3 record\attribute might take less data, e.g. 1kb but 3 people try to get the data at once (DDOS)

              That a single user, imagine 100 user, or 1000 user.

          • #12
            Originally posted by rabii View Post
            Try this

            PHP Code:
            $i 0;​ 
            Thank you! Will give this a try and maybe finally an update to the learning thread. I'm having trouble with using string\concatenate. Creating a variable from result doesn't help:

            Code:
            $1 = 'concatenante test';
            $i = 0;
            
            while ($i < array\length(contactsIds)) {
            $result = record\attribute('Contact', array\at(contactsIds, $i), 'name');
            $i = $i + 1;
            };
            
            output\printLine($result);
            
            output\printLine(
            string\concatenate($result,' ', $1)
            )​
            Click image for larger version

Name:	image.png
Views:	297
Size:	57.8 KB
ID:	98267

            Desire result is to be something like this:

            Test Contact 1 concatenate test // result of Array Index 1
            Test Contact 2 concatenate test // result of Array Index 2

            At the moment, I'm guessing I don't it possible to concatenate it.

            Comment

            Working...
            X