function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
smita bhargavasmita bhargava 

object design for the json data

Hello
I have json data as follows.

I need help in creating an object design for this json data so when I deserialize the json then I want to store in backend.

Please help me in creating the object design.
{
  "invoiceList": [
    {
      "totalPrice": 5.5,
      "statementDate": "2011-10-04T16:58:54.858Z",
      "lineItems": [
        {
          "UnitPrice": 1,
          "Quantity": 5,
          "ProductName": "Pencil"
        },
        {
          "UnitPrice": 0.5,
          "Quantity": 1,
          "ProductName": "Eraser"
        }
      ],
      "invoiceNumber": 1
    },
    {
      "totalPrice": 11.5,
      "statementDate": "2011-10-04T16:58:54.858Z",
      "lineItems": [
        {
          "UnitPrice": 6,
          "Quantity": 1,
          "ProductName": "Notebook"
        },
        {
          "UnitPrice": 2.5,
          "Quantity": 1,
          "ProductName": "Ruler"
        },
        {
          "UnitPrice": 1.5,
          "Quantity": 2,
          "ProductName": "Pen"
        }
      ],
      "invoiceNumber": 2
    }
  ]
}
Thanks
smitaB
 
Best Answer chosen by smita bhargava
Sandeep WaliaSandeep Walia
Hi Smita,

I got your point and this is what I would suggest
Create a master detail relationship between Invoice and Invoice Line Item and the field and field types will be as follow
Invoice:
  • totalPrice :Roll Up Summary field on the price field of Invoice
  • statementDate: Date time field
  • invoiceNumber:Number type Field
Invoice Line Item:
  • UnitPrice: Currency or Number Field
  • Quantity: Number Field
  • ProductName:String
Hope this helps,
Sandeep

All Answers

Sandeep WaliaSandeep Walia
Hi Smita,

You would need to create a Wrapper class. I have created this wrapper using the JSON provided by you:
public class fromJSON{
	public cls_invoiceList[] invoiceList;
	class cls_invoiceList {
		public Double totalPrice;	//5.5
		public String statementDate;	//2011-10-04T16:58:54.858Z
		public cls_lineItems[] lineItems;
		public Integer invoiceNumber;	//1
	}
	class cls_lineItems {
		public Integer UnitPrice;	//1
		public Integer Quantity;	//5
		public String ProductName;	//Pencil
	}
}

The comments describe the the data that was provided. Please note that you can use online tools such as JSON to Apex (https://www.adminbooster.com/tool/json2apex) to create Apex classes from JSON data in future.
Please follow this example (https://salesforce.stackexchange.com/questions/9328/how-to-parse-the-json-object-in-salesforce) to further deserialse your JSON.

Hope this helps,
Sandeep
smita bhargavasmita bhargava
Hi sandeep
I understand the wrapper class, but I am purely looking at object design.
for ex: I consider invoice to be an object having three fields i.e totalprice , statementdate and invoicenumber.

so in backend , for every invoice nunber I want to have the line items i,e Unitprice,Quantity,ProductName

so should I design lineitems as a separate object having a lookup to Invoice .

 I hope I am clear.

Thanks
smita
 
JohnHernandez.ax1746JohnHernandez.ax1746
The following will work:

Run the following:
String jsonString = JSON.serialize(new distraction());
After instantiating something like the following:
public class distraction {
    public class lineItem{
		public Decimal UnitPrice {get;set;}
		public Integer Quantity {get;set;}
		public String ProductName {get;set;}
        
        public lineItem(){}
        public lineItem(String name, Integer qty, Decimal price ){
            ProductName = name;
            Quantity= qty;
            UnitPrice = price;
        }
    }

    public class invoice{
       public Integer invoiceNumber {get;set;}
       public Decimal totalPrice {get;set;}
       public DateTime statementDate {get;set;}
       public List<lineItem> lineItems {get;set;} 
        
        public invoice(){}
        public invoice(Integer invNo, DateTime sDate, Decimal tPrice){
            invoiceNumber = invNo;
            statementDate = sDate;
            totalPrice = tPrice;      
            List<lineItem> lineItems = new List<lineItem>();
        }
    }

    List<invoice> invoiceList {get;set;}   
    
    public distraction(){
        invoice i1 = new invoice(1, DateTime.newInstance(2011, 10, 4, 4, 16, 58), 5.5);
        i1.lineItems = new List<lineItem>{new lineItem('Pencil', 5, 1), new lineItem('Eraser', 1, 0.5)};
        invoice i2 = new invoice(2, DateTime.newInstance(2011, 10, 4, 4, 16, 58), 11.5) ;
        i2.lineItems = new List<lineItem>{new lineItem('Notebook', 1, 6), new lineItem('Ruler', 1, 2.5),new lineItem('Pen', 2, 1.5)};
        invoiceList = new List<invoice>{i1, i2};
    }  
}

Hope this helps.

John

 
smita bhargavasmita bhargava
Hi John

I am new to salesforce so I take some time to understand.

so invoice is one object  and  lineitems is another object.

for every invoice number , I need the line items at the object level.

then how to relate both objects?

Thanks
smita

 
Sandeep WaliaSandeep Walia
Hi Smita,

I got your point and this is what I would suggest
Create a master detail relationship between Invoice and Invoice Line Item and the field and field types will be as follow
Invoice:
  • totalPrice :Roll Up Summary field on the price field of Invoice
  • statementDate: Date time field
  • invoiceNumber:Number type Field
Invoice Line Item:
  • UnitPrice: Currency or Number Field
  • Quantity: Number Field
  • ProductName:String
Hope this helps,
Sandeep
This was selected as the best answer
JohnHernandez.ax1746JohnHernandez.ax1746
Smita,

You have a few options.  You can activate quotes and use them as invoices or create a custom invoice object and a line item detail object with a master detail relationship between the invoice and line item detail.  The master detail field ties them together.

Regards,

John
smita bhargavasmita bhargava
Hi sandeep

Thanks I got the picture. Actually this is what I wanted.

Thanks
smita
smita bhargavasmita bhargava
Hi John
Thanks for the input.
I will try with quotes also.

Thanks
smitaB