You need to sign in to do that
Don't have an account?
Juan Miguel Francia
Insert nested records using rest class with existing ID of parent
Hi I need to insert a `Subscription_Order__c` and `OrderProduct__c` record in Salesforce using Rest Api. `Subscription_Order__c` has a lookup to `Account` and `OrderProduct__c`has a master detail relationship to `Product__c` and a lookup to `Account`.
I need to be the one who input the Id of Account to `Subscription_Order__c` and the Product__c id to `OrderProduct__c`
This is my sample code. I was able to do this with newly created Account but I need to use an id of an existing account.
I already tried this
`global static string doPost(id AccountID){`
and then
`wso.subscriptionOrder.Account__c = AccountId ;`
But it's throwing an `Unexpected parameter encountered during deserialization` error.
Your help is much appreaciated
This is my request body when I am having the `Unexpected parameter encountered during deserialization` error
I need to be the one who input the Id of Account to `Subscription_Order__c` and the Product__c id to `OrderProduct__c`
This is my sample code. I was able to do this with newly created Account but I need to use an id of an existing account.
I already tried this
`global static string doPost(id AccountID){`
and then
`wso.subscriptionOrder.Account__c = AccountId ;`
But it's throwing an `Unexpected parameter encountered during deserialization` error.
Your help is much appreaciated
@restResource(urlMapping='/buyproduct/*') global with sharing class buyProduct{ private class OrderWrapper{ Account account; List<subscriptionOrderWrapper> so; } private class subscriptionOrderWrapper{ Subscription_Order__c subscriptionOrder; List<OrderProduct__c> orderProductList; } @HttpPost global static string doPost(){ OrderWrapper container = (OrderWrapper)System.JSON.deserialize( RestContext.request.requestBody.tostring(), OrderWrapper.class); System.debug('#####1' + container); Account acc = container.account; insert acc; System.debug('#####2' + acc); List<Subscription_Order__c> soInsert = new List<Subscription_Order__c>(); for(subscriptionOrderWrapper wso : container.so){ //wso.subscriptionOrder.Account__c = acc.id; //Account__c = Inputted id of existing account id request body soInsert.add(wso.subscriptionOrder); } insert soInsert; System.debug('#####3' + soInsert); list<OrderProduct__c> opInsert = new List<OrderProduct__c>(); for(subscriptionOrderWrapper wso : container.so){ for(OrderProduct__c orderProduct : wso.orderProductList){ orderProduct.Subscription_Order__c = wso.subscriptionOrder.Id; //subcription_Product__c = Inputted id of an existing product__c in request Body opInsert.add(orderProduct); } } insert opInsert; system.debug('#####4' + opInsert); return acc.Id; } }
This is my request body when I am having the `Unexpected parameter encountered during deserialization` error
{ "so": [{ "subscriptionOrder":{ "Account" : 0016F00001sZtG0QAK } }] }
Juan Miguel Francia
Correction. OrderProduct__c has a master detail relationship with Product__c and a lookup to Subscription_Order__c