1

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.

7
  • 3
    You'll need to know which account or contact the asset belongs to. This means you'll need to either query for an account or contact (or both), or create new records using some type of information from the payload.
    – sfdcfox
    Commented Jan 3, 2020 at 0:58
  • @sfdcfox can you give me an example? Would I be getting the 'accountId' from the 'order' sObject? What would be the best practice for this?
    – Richard
    Commented Jan 3, 2020 at 21:34
  • Yes, that would be an acceptable account to attach the asset to.
    – sfdcfox
    Commented Jan 3, 2020 at 21:39
  • @sfdcfox got the id but am having trouble pulling it from the List 'account_id: (Order:{AccountId=0018A00000KvRm8QAF, Id=8018A0000002fIpQAI})' I've tried using 'get', for example ' List<Object> order_account_id = (List<Object>)account_id.get('AccountId');' and continue getting errors such as 'Method does not exist or incorrect signature: void get(String) from the type List<Object>' What method should I use to extract the value? Am I going about this the wrong way?
    – Richard
    Commented Jan 3, 2020 at 22:40
  • You need a single object from the list to use the get method. You'd want to get the AccountId from the Order that you apparently queried. Could you edit your question to include the latest revision of your code?
    – sfdcfox
    Commented Jan 5, 2020 at 15:12

2 Answers 2

2

You need to change the Sharing Settings for Asset.

  1. Enable Asset Sharing (Setup > Asset Settings, from Classic),
  2. Go to Sharing and change the OWD to something other than Controlled by Parent. Whilst it is Controlled by Parent it needs to have an Account/Contact

enter image description here

enter image description here

0

You don't really need to change anything in the object This is probably because you have not mapped the account ID with the right name. I was using Salesforce inspector and was getting the same error just because my account field was as account ID, it should be accountid. After doing that, it worked.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .