Help with Workflows/Business Rules

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • animax
    Senior Member
    • Jun 2018
    • 155

    Help with Workflows/Business Rules

    Hello,

    I want to set up a workflow / Business Rule that does the following:

    I have entity A, in this entity there are entries that contain the field X, among other things.

    Entity A's entries are linked to entity B in the form that one entry from A can have multiple entries from B.

    Now the following should happen:
    If I set the X field to a specific value in entity A's entry, then all entity B's entries that have a specific status should be set to a new value Y.

    Example:
    Entry in Entity A has status field "Active", I set it to Inactive. Then the status of the related entries of entity B that have a certain status should be automatically set to a new value.

    How can I set this up in the workflow? The Advanced Pack is available.​

    Thank you for your help.
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    #2
    you can create a workflow as below: i will use existing entities Account (A) => Opportunity (B) they already have a one to many

    Target entity Account => after record updated

    Condition (status changed And status = 'Active')

    Trigger => Formula script task and then loop through the related entries of opportunities and update their status or any other fields you wish to change as below:

    PHP Code:
    // count all related entities (B)
    $count = record\count('Opportunity', 'accountId=', id);
    
    // get list of all the related entities B given the count above and also specifying only one specific stage
    $opps = record\findRelatedMany('Account', id, 'oppotunities', $count, 'createdAt', 'desc', 'stage=', 'Prospecting');
    
    $i = 0;
    
    // loop through them and update their status (if status is not update then try record\update('Meeting', array\at($opps, $i), 'status', list('New Status')) it should work
    while(
        $i < array\length($opps),
        (
            record\update('Opportunity', array\at($opps, $i), 'status', 'New Status');
            $i = $i + 1;
        )
    );
    Hope this helps

    It is easy what you want to achieve and you can do it even using only formula.
    Rabii
    Web Dev

    Comment

    • animax
      Senior Member
      • Jun 2018
      • 155

      #3
      Hi rabii... thank you for your help!

      However, I only want to change the entries in Entity B that have the status "Y". This should be changed to "Z" for all entries that it affects. Only the entries that belong to A's entry, of course. Everyone else doesn't.

      Can you add that to the script? Thanks very much.​

      Comment


      • rabii
        rabii commented
        Editing a comment
        if it would be helpful to provide the name of the entities and fields if that is ok with you. i am happy to provide you with code as for the example i provided above it does only change 'Opportunity' which represent entity B in your case.
    • animax
      Senior Member
      • Jun 2018
      • 155

      #4
      Ok, No Problem!

      Entity A we call "Domizilservice".
      The Status Field in A we call "status".
      There is an "Active" status and an "Inactive" status​.

      Each entry in "Domizilservice" has several entries of entity B. Entity B we call "Posteingang".
      There is also a field called "status" in each entry.

      Now my feature request:
      If I change the "status" field of an entry in A from "Inactive" to "Active", then I want all related entries of entity B - that currently have the status "Completed (Inactive)" (no other status, only these!) - to be changed to the status "recorded".

      I hope this makes the requirement clear.

      Comment

      • rabii
        Active Community Member
        • Jun 2016
        • 1250

        #5
        here is the code, just review it and give it a go. i suggest to try it out on the sandbox - this version consider that the code will be used on formula if you want to use it in workflow just remove the condition and add condition by workflow:

        Please note that i assumed the relations between the two entities is called posteingangs - make sure to correct this ti reflect the correct relationship name.
        PHP Code:
        ifThen(!entity\isNew() && entity\isAttributeChanged('status') && status == 'Active',
        
            $count = entity\countRelated('posteingangs');
            
            // get list of all the related entities Posteingang given the count above and also specifying only one specific stage
            $posteingangs = record\findRelatedMany('Domizilservice', id, 'posteingangs', $count, 'createdAt', 'desc', 'status=', list('Completed', 'Inactive'));
            
            $i = 0;
            
            // loop through collection and update their status
            while(
                $i < array\length($posteingangs),
                (
                    record\update('Posteingang', array\at($posteingangs, $i), 'status', 'recorded');
                    $i = $i + 1;
                )
            );
        )
        Rabii
        Web Dev

        Comment

        • animax
          Senior Member
          • Jun 2018
          • 155

          #6
          Hi rabii... something is wrong with that. By the way... the entity and the entries are actually both named "Posteingang", without s on the end, as usual.

          The status of Posteingang to search for is called (filter) called
          "erledigt (Kunde Inaktiv)"

          Please take a look on the screenshot and the customized code
          Code:
          ifThen(!entity\isNew() && entity\isAttributeChanged('status') && status == 'Activ',
          
              $count = entity\countRelated('posteingang');
              
              // get list of all the related entities Posteingang given the count above and also specifying only one specific stage
              $posteingang = record\findRelatedMany('Domizilservice', id, 'posteingang', $count, 'createdAt', 'desc', 'status=', list('erledigt (Kunde Inaktiv)'));
              
              $i = 0;
              
              // loop through collection and update their status
              while(
                  $i < array\length($posteingang),
                  (
                      record\update('Posteingang', array\at($posteingang, $i), 'status', 'erfasst');
                      $i = $i + 1;
                  )
              );
          )​

          Comment

          • animax
            Senior Member
            • Jun 2018
            • 155

            #7
            I accidentally added the script to the conditions. Silly me. Now I have correctly stored it in the actions and the workflow starts.

            BUT: Unfortunately, no status of the "Posteingang" is updated for the entries.

            Comment


            • rabii
              rabii commented
              Editing a comment
              you have removed the condition please add condition.
          • rabii
            Active Community Member
            • Jun 2016
            • 1250

            #8
            hey as mentioned before you should not include the if condition if you are using workflow hence the condition is checked by the workflow which means you should use only the code below:

            PHP Code:
            $count = entity\countRelated('posteingang');
            
            // get list of all the related entities Posteingang given the count above and also specifying only one specific stage
            $posteingang = record\findRelatedMany('Domizilservice', id, 'posteingang', $count, 'createdAt', 'desc', 'status=', list('erledigt (Kunde Inaktiv)'));
            
            $i = 0;
            
            // loop through collection and update their status
            while(
            $i < array\length($posteingang),
            (
            record\update('Posteingang', array\at($posteingang, $i), 'status', 'erfasst');
            $i = $i + 1;
            )
            );
            try this instead. let me know if it works.
            Rabii
            Web Dev

            Comment

            • animax
              Senior Member
              • Jun 2018
              • 155

              #9
              The workflow is running, but unfortunately still no entry is updated in "Posteingang". The status "erledigt (Kunde Inaktiv)" remains unchanged. It should actually read "erfasst".

              What could that be?

              Comment

              • rabii
                Active Community Member
                • Jun 2016
                • 1250

                #10
                you have to add condition to when the workflow triggers, now you have no condition and workflow triggers on update. add condition as per screenshot.
                Attached Files
                Rabii
                Web Dev

                Comment

                • animax
                  Senior Member
                  • Jun 2018
                  • 155

                  #11
                  I did that, but the entries in "Posteingang" are not updated.

                  Code:
                  $count = entity\countRelated('posteingang');
                  
                  // get list of all the related entities Posteingang given the count above and also specifying only one specific stage
                  $posteingang = record\findRelatedMany('Domizilservice', id, 'posteingang', $count, 'createdAt', 'desc', 'status=', list('erledigt (Kunde Inaktiv)'));
                  
                  $i = 0;
                  
                  // loop through collection and update their status
                  while(
                  $i < array\length($posteingang),
                  (
                  record\update('Posteingang', array\at($posteingang, $i), 'status', 'erfasst');
                  $i = $i + 1;
                  )
                  );​

                  Comment

                  • rabii
                    Active Community Member
                    • Jun 2016
                    • 1250

                    #12
                    please check if posteingang this is the correct name for the relationship / foreign link. it could be just some name is not correct. i have tested code and works just changed entities
                    Rabii
                    Web Dev

                    Comment

                    • animax
                      Senior Member
                      • Jun 2018
                      • 155

                      #13
                      Here are screenshots of the names of "Posteingang" and the "Status" field with its entries.​

                      Comment


                      • rabii
                        rabii commented
                        Editing a comment
                        what is the relationship name between Domizilservice and Posteingang??
                    • rabii
                      Active Community Member
                      • Jun 2016
                      • 1250

                      #14
                      ok best way to debug is to try out this in sandbox, please go to sandbox and test out the code then you can print the result of each query to see what is wrong. do you know how to use the sandbox ?
                      Rabii
                      Web Dev

                      Comment

                      • animax
                        Senior Member
                        • Jun 2018
                        • 155

                        #15
                        And here from Domizilservice

                        Comment

                        Working...