Code Not working in Api Before Save Script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sapyantuk
    Senior Member
    • Oct 2020
    • 265

    Code Not working in Api Before Save Script

    Hello Team,

    I have written a code to validate whether the selected opportunity, billing contacts, and shipping contacts are linked with an account or not. If any of these records are not linked with an account, an error is thrown. I have implemented this code for sales orders, invoices, and quotes, and to facilitate this, I have made the account field readonly. This ensures that when we select an opportunity, the linked account is automatically populated.

    However, I am currently facing difficulties in validating this for sales orders. I would greatly appreciate your assistance in resolving this issue. we are using this as before save api


    $opportunityAccountId = record\attribute('Opportunity', opportunityId, 'accountId');

    if (opportunityId && (!$opportunityAccountId || $opportunityAccountId != accountId)) {

    recordService\throwBadRequest('Forbiden you can not link an opportunity to this record which is not linked to any account');
    }

    if (accountId || $opportunityAccountId) {

    $billingContactAccountId = record\attribute('Contact', billingContactId, 'accountId');

    if (!$billingContactAccountId || $billingContactAccountId != accountId || $billingContactAccountId != $opportunityAccountId) {

    recordService\throwBadRequest('Sorry you can not link a billing contact who is not linked to current selected account');
    }

    $shippingContactAccountId = record\attribute('Contact', shippingContactId, 'accountId');

    if (!$shippingContactAccountId || $shippingContactAccountId != accountId || $shippingContactAccountId != $opportunityAccountId) {

    recordService\throwBadRequest('Sorry you can not link a shipping contact who is not linked to current selected account');
    }
    }​
  • sapyantuk
    Senior Member
    • Oct 2020
    • 265

    #2
    lazovic yuri .Can you please suggest ?

    Comment

    • yuri
      Member
      • Mar 2014
      • 8528

      #3
      I formatted your code so it became readable.

      I'm a developer, not a support person. We don't provide free support that implies writing code. We just released the new version, we are busy. We've been helping too much for free, our support is constantly overwhelmed. Maybe someone else could help.

      Code:
      $opportunityAccountId = record\attribute('Opportunity', opportunityId, 'accountId');
      
      if (
          opportunityId &&
          (!$opportunityAccountId || $opportunityAccountId != accountId)
      ) {
          recordService\throwBadRequest(
              'Forbiden you can not link an opportunity to this record which is not linked to any account');
      }
      
      if (accountId || $opportunityAccountId) {
          $billingContactAccountId = record\attribute('Contact', billingContactId, 'accountId');
          
          if (
              !$billingContactAccountId ||
              $billingContactAccountId != accountId ||
              $billingContactAccountId != $opportunityAccountId
          ) {
              recordService\throwBadRequest(
                  'Sorry you can not link a billing contact who is not linked to current selected account');
          }
              
          $shippingContactAccountId = record\attribute('Contact', shippingContactId, 'accountId');
              
          if (
              !$shippingContactAccountId ||
              $shippingContactAccountId != accountId ||
              $shippingContactAccountId != $opportunityAccountId
          ) {
              recordService\throwBadRequest(
                  'Sorry you can not link a shipping contact who is not linked to current selected account');
          }
      }​
      Last edited by yuri; 09-09-2023, 10:50 AM.
      If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

      Comment

      Working...