I have a relatively simple script which is triggered by a user action on an item fulfillment.
For testing purposes, and occasionally during day to day operations, the fulfillment record needs to be deleted. Before I added my SS I would get a screen saying that the record was deleted and a button to take me back to the sales order. Now I get a notice from SuiteScript saying that the record doesn't exist, and the "go back" button takes me to the item fulfillment page.
Now if I go back to the originating Sales Order, the fulfillment is gone from the related records.
Is there a "best practice" to check for the existence of a record to use for conditional running of the script?
My current script below:
function copy_shipping_carrier_to_so_record() { /* * * TODO - Make a conditional test to stop the script from running if there is no record and/or the itemfulfillment record is being deleted. * */ // After submit of Item Fulfillment Record // Load Item fulfillment record to access the fields var record = nlapiLoadRecord('itemfulfillment', nlapiGetRecordId()); if (Boolean(record) == false){ return } else { var carrier = record.getFieldValue('custbody_ozlink_shipping_carrier'); // Get carrier value var related_so = record.getFieldValue('createdfrom'); // Get related record Sales order internal ID var so_record = nlapiLoadRecord('salesorder', related_so); // Load Sales Order record - based on the related record via NS internal ID so_record.setFieldValue('custbody_ozlink_shipping_carrier', carrier); // Set Sales Order record value nlapiSubmitRecord(so_record, null, true); // Submit/save the record var customer = so_record.getFieldValue('entity'); // get the internal ID of the customer record var tracking_email = nlapiCreateEmailMerger(4); // Start the email merger and set the template tracking_email.setTransaction(related_so); // retrieve the sales order to merge with the template var send_ready = tracking_email.merge(); // generate the merger var tracking_body = send_ready.getBody(); // merge the body var tracking_sub = send_ready.getSubject(); // merge the subject var tracking_email_send = nlapiSendEmail(19585, customer, tracking_sub, tracking_body, null, 'caleb@calpetrx.com', null, null, null, false, 19585); // Send the email! } }