Example #1
0
 public function create($productID, array $formData, $serialNumber)
 {
     if (!Framework::isValidID($productID)) {
         return $this->getResponseObject(null, null, Config::getMessage(INVALID_ID_SALES_PRODUCT_COLON_LBL));
     }
     //$resultSet = Connect\ROQL::queryObject(sprintf("SELECT SalesProduct FROM SalesProduct WHERE ID = %d And Disabled != 1 And Attributes.IsServiceProduct = 1 And Attributes.HasSerialNumber != 1 And AdminVisibleInterfaces.ID = curInterface()", $productID))->next();
     $resultSet = Connect\ROQL::queryObject(sprintf("SELECT SalesProduct FROM SalesProduct WHERE ID = %d And Disabled != 1 And Attributes.IsServiceProduct = 1 And AdminVisibleInterfaces.ID = curInterface()", $productID))->next();
     if (!($salesProduct = $resultSet->next())) {
         return $this->getResponseObject(null, null, Config::getMessage(INVALID_ID_SALES_PRODUCT_COLON_LBL));
     }
     $asset = $this->getBlank()->result;
     if ($contact = $this->getContact()) {
         $asset->Contact = $contact->ID;
     } else {
         return $this->getResponseObject(null, null, Config::getMessage(CONTACT_IS_NOT_LOGGED_IN_MSG));
     }
     if ($asset->Contact->Organization) {
         $asset->Organization = $asset->Contact->Organization->ID;
     }
     $errors = $warnings = array();
     foreach ($formData as $name => $field) {
         if (!\RightNow\Utils\Text::beginsWith($name, 'Asset')) {
             continue;
         }
         $fieldName = explode('.', $name);
         try {
             //Get the metadata about the field we're trying to set. In order to do that we have to
             //populate some of the sub-objects on the record. We don't want to touch the existing
             //record at all, so instead we'll just pass in a dummy instance.
             list(, $fieldMetaData) = ConnectUtil::getObjectField($fieldName, $this->getBlank()->result);
         } catch (\Exception $e) {
             $warnings[] = $e->getMessage();
             continue;
         }
         if (\RightNow\Utils\Validation::validate($field, $name, $fieldMetaData, $errors)) {
             $field->value = ConnectUtil::castValue($field->value, $fieldMetaData);
             if ($setFieldError = $this->setFieldValue($asset, $name, $field->value)) {
                 $errors[] = $setFieldError;
             }
         }
     }
     if ($productID !== null && ($setFieldError = $this->setFieldValue($asset, "Asset.Product", $productID))) {
         $errors[] = $setFieldError;
     }
     if ($serialNumber !== null && ($setFieldError = $this->setFieldValue($asset, "Asset.SerialNumber", $serialNumber))) {
         $errors[] = $setFieldError;
     }
     if ($errors) {
         return $this->getResponseObject(null, null, $errors);
     }
     try {
         $asset = parent::createObject($asset, SRC2_EU_ASSET);
     } catch (\Exception $e) {
         $asset = $e->getMessage();
     }
     if (!is_object($asset)) {
         return $this->getResponseObject(null, null, $asset);
     }
     return $this->getResponseObject($asset, 'is_object', null, $warnings);
 }