You need to sign in to do that
Don't have an account?
TinaC
How to insert and update the nested custom object by using Apex?
I try to build a REST API to receive JSON from another server.
The JSON sample like this.
And I have created the apex structure like this.
It can receive the data successfully, but the question is occurred on inserting or updating data.
In SFDC, I created 3 objects: Order, OrderItem, TravelCom.
Order is the parent of OrderItem, and OrderItem is the parent of TravelCom.
The API can create these 3 objects well and insert the responding value on Order, OrderItem but TravelCom is always null.
This is what I wrote.
Can anyone correct me?
Thank you so much.
The JSON sample like this.
{ "token":"1234", "od_order_id_str":"12345, "od_status":"test", "od_product_code":"test, "od_rate":"741", "od_deliver_status":"NOT_YET", "od_archive_policy_doc":"false", "od_contract_status":"NONE", "od_source":"B2B", "od_b2b_invalid_data":[ "U01", "U02", "U06" ], "ap_first_name":"jack", "ap_last_name":"wang", "ap_mobile":"0987654321", "ap_pid":"test", "ap_birthday":"1980-01-01", "ap_email”:”test@email", "ap_address_city_id":"19", "ap_address_area_id":"10", "ap_address":"test", "tr_transport":"Airplane", "tr_country_code":"JP", "tr_city_id":"0", "tr_purpose":"Tourism", "tr_start_date":"2017-09-18", "tr_start_hour":"00", "tr_start_min":"00", "tr_end_date":"2017-09-29", "tr_end_hour":"00", "tr_end_min":"00", "tr_period_days":"11", "pay_order_id":"8600000023", "policy_list":[ { "po_ins_company_code":"TaiAn", "po_policy_id_str":"", "po_rate":"741", "insured_list":[ { "in_relationship":"SELF", "in_last_name":"wang", "in_first_name":"jack", "in_pid":"test", "in_birthday":"1980-01-01", "in_rate":"741", "pa_package_type":"PACKAGE", "pa_package_name":"test", "in_taian_package":"B", "item_list":[ { "it_is_primary":"true", "it_ins_item_code":"ITEM_RESPONSE", "it_ins_company_code":"", "it_amount_code":"100", "it_amount_name":"100", "it_amount":"1000000", "it_rate":"0", "it_item_name":"test1" }, { "it_is_primary":"true", "it_ins_item_code":"ITEM_SAFE", "it_ins_company_code":"TaiAn", "it_amount_code":"200", "it_amount_name":"200", "it_amount":"2000000", "it_rate":"184", "it_item_name":"test2" }, { "it_is_primary":"false", "it_ins_item_code":"ITEM_MEDICAL_BILL", "it_ins_company_code":"TaiAn", "it_amount_code":"5萬", "it_amount_name":"5萬", "it_amount":"50000", "it_rate":"12", "it_item_name":"test3" }, { "it_is_primary":"false", "it_ins_item_code":"ITEM_MEDICAL_DAY", "it_ins_company_code":"TaiAn", "it_amount_code":"1000", "it_amount_name":"1000", "it_amount":"0", "it_rate":"33", "it_item_name":"test4" }, { "it_is_primary":"false", "it_ins_item_code":"ITEM_INCONVENIENT", "it_ins_company_code":"TaiAn", "it_amount_code":"商務", "it_amount_name":"商務", "it_amount":"0", "it_rate":"429", "it_item_name":"test5" }, { "it_is_primary":"false", "it_ins_item_code":"ITEM_SUDDEN_SICK", "it_ins_company_code":"TaiAn", "it_amount_code":"20萬", "it_amount_name":"20萬", "it_amount":"200000", "it_rate":"83", "it_item_name":"test6" } ], "benef_list":[ { "be_last_name":"wang", "be_first_name":"father", "be_pid":"test", "be_relationship":"father" }, { "be_last_name":"wang", "be_first_name":"mother", "be_pid":"test", "be_relationship":"mother" } ] } ] } ] }
And I have created the apex structure like this.
public class Benef_list { public String be_last_name; public String be_first_name; public String be_pid; public String be_relationship; } public class Policy_list { public String po_ins_company_code; public String po_policy_id_str; public String po_rate; public List<Insured_list> insured_list; } //Request Model public class RequestModel { public String token; public String od_order_id_str; public String od_status; public String od_product_code; public String od_rate; public String od_deliver_status; public String od_archive_policy_doc; public String od_contract_status; public String od_source; public List<String> od_b2b_invalid_data; public String ap_first_name; public String ap_last_name; public String ap_mobile; public String ap_pid; public String ap_birthday; public String ap_email; public String ap_address_city_id; public String ap_address_area_id; public String ap_address; public String ap_update_info; public String tr_transport; public String tr_country_code; public String tr_city_id; public String tr_purpose; public String tr_start_date; public String tr_start_hour; public String tr_start_min; public String tr_end_date; public String tr_end_hour; public String tr_end_min; public String tr_period_days; public String pay_order_id; public List<Policy_list> policy_list; } public class Item_list { public String it_is_primary; public String it_ins_item_code; public String it_ins_company_code; public String it_amount_code; public String it_amount_name; public String it_amount; public String it_rate; public String it_item_name; } public class Insured_list { public String in_relationship; public String in_last_name; public String in_first_name; public String in_pid; public String in_birthday; public String in_rate; public String pa_package_type; public String pa_package_name; public String in_taian_package; public List<Item_list> item_list; public List<Benef_list> benef_list; }
It can receive the data successfully, but the question is occurred on inserting or updating data.
In SFDC, I created 3 objects: Order, OrderItem, TravelCom.
Order is the parent of OrderItem, and OrderItem is the parent of TravelCom.
List<Order__c> orderList = [SELECT Id, Name, Account__c, (SELECT Id, Name, Order__c, relationship__c, FROM OrderItems__r), (SELECT Id, Name ,Order__c, OrderItem__c, FROM TravelCom__r) FROM Order__c WHERE RecordTypeId = :orderRTList[0].Id AND Name = :tmpRequestModel.od_order_id_str];
The API can create these 3 objects well and insert the responding value on Order, OrderItem but TravelCom is always null.
This is what I wrote.
for (Policy_list objPolicy : tmpRequestModel.policy_list) { for (Insured_list objInsured : objPolicy.insured_list) { tmpOrderItem.Insurance_No__c = objPolicy.po_policy_id_str; upsert tmpOrderItem; for(Item_list objItem : objInsured.item_list) { tmpTravelCom.Orderitem__c = tmpOrderItem.Id; if(objItem.it_ins_item_code !=null) { if(objItem.it_ins_item_code == 'test1) { tmpTravelCom.TA001_amount__c = integer.valueOf(objItem.it_amount); tmpTravelCom.TA001_Price__c = integer.valueOf(objItem.it_rate); } if(objItem.it_ins_item_code == 'test2' && tmpTravelCom.TA001_amount__c != null && tmpTravelCom.TA001_amount__c != null) { tmpTravelCom.TA002_amount__c = integer.valueOf(objItem.it_amount); tmpTravelCom.TA002_Price__c = integer.valueOf(objItem.it_rate); } if(objItem.it_ins_item_code == 'test3') { tmpTravelCom.TA003_amount__c = integer.valueOf(objItem.it_amount); tmpTravelCom.TA003_Price__c = integer.valueOf(objItem.it_rate); } } } else { tmpResponseModel.status = '01'; tmpResponseModel.traceError = 'No Insured Item value!'; } } upsert tmpTravelCom; } }
Can anyone correct me?
Thank you so much.
Hello TinaC,
1. upsert tmpOrderItem; on 10th line you have DML statement inside for loop which is very bad practice ,soon you will face issues(errors) related to governor limits .
2. You are inserting values in a record in your code which may be overwriting it .
3. please check whether it is returning the list everytime or any single Sobject also, and apply checks and code related to it.
I can further help you only if you can provide me full code of what you are doing and link of JSON .
If these above steps have solved your problem then Please choose it as the Best answer .