Announcement

Collapse
No announcement yet.

Code Not working in Api Before Save Script

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

  • 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');
    }
    }​

  • #2
    lazovic yuri .Can you please suggest ?

    Comment


    • #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.

      Comment

      Working...
      X