How to make Child Items read-only base on a field value of the Parent Record?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Triggerz
    Member
    • May 2024
    • 62

    How to make Child Items read-only base on a field value of the Parent Record?

    Hi,

    How can I restrict the editing and adding of Child Items base on a field value of the Parent Record?
    The reason is that if the Parent Record status field value is updated to "Posted", I don't want any user to be able to modify or add child items anymore.

    Thanks in advance...
  • a.slyzhko
    Senior Member
    • Oct 2023
    • 101

    #2
    Create API before save script for you child entity type.
    PHP Code:
    if (parentId && parentType) {
        $parentStatus = record\attribute(parentType, parentId, 'status');
        if ($parentStatus == 'Posted') {
            recordService\throwForbidden('Parent status is set to Posted. Adding new items and modifying existing is forbidden.')
        }
    } 
    

    Comment

    • Triggerz
      Member
      • May 2024
      • 62

      #3
      Hi a.slyzhko, thank yo so much for your response. I am fairly new with EspoCRM and have little knowledge about scripting. Could you help me out on how to construct my script?

      I have the parent entity CCashar and the child entity CCasharitems. I tried the script below in the formula sandbox to get the status attribute value of the parent entity but it is not returning the correct value. Not sure how I can get the value of the parent entity's ID value properly.

      $parentStatus = record\attribute('CCashar', 'ID', 'status');
      output\printLine ($parentStatus);


      Thanks in advance...

      Comment

      • a.slyzhko
        Senior Member
        • Oct 2023
        • 101

        #4
        Watch this video

        Comment

        • Triggerz
          Member
          • May 2024
          • 62

          #5
          Hi a.slyzhko, many thanks for your help. I was able to make it work using your suggestion but I am having some error when adding new Casharitems.

          My Cashar and Casharitems entities have a Many-To-One relationship. Please see below

          Click image for larger version

Name:	image.png
Views:	50
Size:	45.8 KB
ID:	115173

          I took the value of CCashar.status and use this as the condition as below. This works fine if the CCashar.status is equal to 'Approved'

          Code:
          if (cashar.status == 'Approved') {
          recordService\throwForbidden('Parent status is set to Approved. Adding new items and modifying existing is forbidden.')
          }​

          However, if the cashar.status is not equal to 'Approved', I can still edit the casharitems but when I try to add a new casharitem, it is throwing an Error 500 as shown below.

          Click image for larger version

Name:	image.png
Views:	42
Size:	55.6 KB
ID:	115174

          Is there an additional condition that I need to add in the script to allow adding and editing of new casharitems if the status is not equal to 'Approved'?

          Thanks & Regards...

          Comment

          • Triggerz
            Member
            • May 2024
            • 62

            #6
            Hi,

            Just an additional info. I checked the Error Log and found the error below. Seems like when a new Casharitem is created, it doesn't have an ID. Same with the related Cashar ID.

            Code:
            [2025-02-17 00:08:00] CRITICAL: (0) Can't use an entity w/o ID. :: POST /CCasharitems :: /home/folder-crm/htdocs/crm.mysite.com/application/Espo/ORM/Repository/RDBRelation.php(72)
            Thanks in advance...

            Comment

            • Triggerz
              Member
              • May 2024
              • 62

              #7
              Hi a.slyzhko, I found the solution to my problem. Many thanks for your guidance. Below is the code that I implemented and works great for my need. I hope this will also help others.

              Code:
              $recstat = record\attribute('CCashar', casharId, 'status');
              if($recstat == 'Approved' || $recstat == 'Released' || $recstat == 'Liquidated') {
                recordService\throwForbidden(string\concatenate('Parent status is set to ', $recstat, '. Adding or Editing of record is not allowed.'));
              }
              Thanks again

              Comment

              Working...