I have been trying to insert
new assets to the asset table and use the serial_number
to check if the asset already exist before inserting it to avoid duplicates. I'm at the point where I am trying to insert
the assets into the asset table. My code can be found below.
@RestResource(urlMapping='/assets')
global with sharing class REST_asset {
@HttpPost
global static void doPost(String incomingPayload) {
String ord_num;
String product_code;
String product_description;
String serial_number;
String sub_start_date_raw;
Datetime sub_start_date_time;
Date sub_start_date;
List<Object> asset_info;
Map<String, Object> deserialized_response =
(Map<String, Object>)JSON.deserializeUntyped(incomingPayload);
ord_num =
(String)deserialized_response.get('ord_num');
System.debug(incomingPayload);
sub_start_date_raw =
(String)deserialized_response.get('sub_start_date');
sub_start_date_time =
(Datetime)JSON.deserialize('"' + sub_start_date_raw + '"', DateTime.class);
sub_start_date = date.newInstance( sub_start_date_time.year(),
sub_start_date_time.month(),
sub_start_date_time.day());
asset_info = (List<Object>)deserialized_response.get('asset_info');
List<asset_data> assets = new List<asset_data>();
asset_data data = new asset_data();
for(Object assetObject : asset_info) {
Map<String, Object> asset = (Map<String, Object>)assetObject;
//asset_data data = new asset_data();
data.product_code = (String)asset.get('product_code');
System.debug('product_code: ' + data.product_code);
data.product_description = (String)asset.get('product_description');
System.debug('product_description: ' + data.product_description);
data.serial_number = (String)asset.get('serial_number');
System.debug('serial_number: ' + data.serial_number);
assets.add(data);
}
System.debug('assets_list: ' + assets);
List <Asset> current_asset_table = [
ProductCode, ProductDescription,
SerialNumber
From Asset
Where Order_number__c = :ord_num];
System.debug('current_asset_table:' + current_asset_table);
List<Object> asset_account_id = ([Select Account.Id
From Order
Where OrderNumber = :ord_num]);
String account_id = (String) asset_account_id.get('AccountId');
Asset this_asset = new Asset(
Order_number__c = data.ord_num,
ProductCode = data.product_code,
ProductDescription = data.product_description,
SerialNumber = data.serial_number
);
System.debug(this_asset);
insert this_asset;
}
}
Separate class where I'm storing my values
public class asset_data {
public String ord_num;
public String product_code;
public String product_description;
public String serial_number;
}
I keep getting the error:
FIELD_INTEGRITY_EXCEPTION, Every asset needs an account, a contact, or both.: Account ID, Contact ID: [AccountId, ContactId]
I am unsure how to port this information over seeing as the fields, account and contact are set as lookup fields in my org
I am also a bit unsure as to how to perform the query and compare the serial number in the query to the serial numbers coming from the incomingPayload
to prevent any duplicates.