Announcement

Collapse
No announcement yet.

Data Validation for Contacts and Opportunities in Espo CRM

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

  • Data Validation for Contacts and Opportunities in Espo CRM

    Hello developers,

    Within Espo CRM, I have established relations with multiple entities. Specifically, I need to establish links between entities when creating new records. To illustrate, let's consider the scenario where I have contacts without an associated email address or phone number. In this context, my objective is to enforce a requirement for a contact to be associated when creating a new opportunity. Additionally, I aim to implement a validation mechanism that prevents the creation of an opportunity linked to a contact lacking an email address or phone number. The idea is that the information in the contact should be completed before it can be used.

    I have attempted to address this by using the following code snippet:
    if (contact.emailAddress == null ) { recordService\throwBadRequest("Missing Email in contacts."); }


    However, I am uncertain about the effectiveness of this code in achieving the desired outcome. Could you please provide guidance on how to achieve the specified functionality in a more effective manner? Your assistance would be greatly appreciated.

  • #2
    ​I have a scenario where I've established a link between a contact and an opportunity. My objective is to implement a validation check. Specifically, I need to verify whether the email field of the linked contact is empty. If it is indeed empty, I intend to prevent the data from being saved and raise an error. How can I go about accomplishing this?

    Click image for larger version

Name:	image.png
Views:	244
Size:	67.4 KB
ID:	96887

    Comment


    • #3
      $a = record\attribute('Contact', $contactId, 'emailAddress'); if ($a == null ) { recordService\throwConflict("You have already Made a Attendance today."); }


      i tried this also. but its also not working

      Comment


      • #4
        Hi sapyantuk,

        Just checked, this formula script should work fine:
        Code:
        if (record\attribute('Contact', contactId, 'emailAddress') == null) {
            recordService\throwBadRequest("Missing Email in contacts.");
        }

        Comment


        • #5
          lazovic Is there any way we can hide Error 400: Bad request from throw bad request and only show Missing Email in contacts

          One more question: In the context of contacts and opportunities, there exists a default many-to-many relationship.
          However, there is a situation I'd like to address regarding the provided code snippet:

          if (record\attribute('Contact', contactId, 'emailAddress') == null) { recordService\throwBadRequest("Missing Email in contacts."); }


          The code functions correctly when only a single contact is selected. However, in a different scenario, let's consider having multiple contacts, labeled as Contact A and Contact B. While Contact A possesses an email address, Contact B does not. Even in this scenario, the above code permits the record to be saved. I am seeking guidance on how to enhance the code to inspect multiple contacts. If any of the associated or linked contacts lack an email address, an error should be triggered to prevent record saving.



          Click image for larger version

Name:	image.png
Views:	240
Size:	51.9 KB
ID:	96898
          Last edited by sapyantuk; 08-28-2023, 04:25 PM.

          Comment


          • #6
            sapyantuk,

            Try this formula script:
            Code:
            $target = list();
            
            $i = 0;
            
            while ($i < array\length(contactsIds)) {
            
               if (record\attribute('Contact', array\at(contactsIds, $i), 'emailAddress') == null) {
                 $target = array\push($target, '0')
               } else {
                 $target = array\push($target, '1')
               };
            
               $i = $i + 1;
            }
            
            if (array\includes($target, '0')) {
               recordService\throwBadRequest("Missing Email in contacts.");
            }

            As for removing the error code, I doubt it's possible. You can only choose which code is displayed using different functions (conflict, duplicate, bad request, etc.).

            Comment

            Working...
            X