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