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
Ragnar Lothbrok 7Ragnar Lothbrok 7 

How to insert the Array of objects into OpportunityLineItem object in apex?

Hi there,
I wanted to create the OpportunityLineItem record from the the data that is passed from js. I have data in Array of object form.
Please anyone guide me in this, and how i can achieve this,
Below is the required code:
Js code:
let jsonString =[
{
"orderQuantity":"1",
"molexPrice":"1200",
"targetCost":"1150",
"perUnit":"1",
"resalePrice":"1250",
"competitor":"NoPlex",
"ProductName":"LEGEND PLATE, BLANK",
"ProductId":"01t0q000003s2nPAAQ"
},
{
"orderQuantity":"2",
"molexPrice":"1000",
"targetCost":"10000",
"perUnit":"1000",
"resalePrice":"1100",
"competitor":"MyMolex",
"ProductName":"CABLE HARNESS EMERGENCY HAMMER",
"ProductId":"01t0q0000023INwAAM"
}
]
let jsonString = JSON.stringify(this.temp);
        console.log('OUTPUT ::: ',jsonString);
        opportunityProductCreation({oppId : this.recordId, oliData : jsonString})
        .then(result=>{
            console.log(result);
            if(result){
                this.data = result;
            }
        }).catch(error => {
               this.data = undefined;
     });
I am new to the salesforce, anyone help is appreciated :D
Thank you.
:
HARILAKSHMI M 8HARILAKSHMI M 8
Hi,
As you are using json, we need to create one wrapper class where we can deserialize json and store the values. Then you can refer the deserialized json in the controller after which you can insert the record. If you need detailed solution can you post full code so that i can understand the exact flow.
Also please refer https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_Json.htm to get additional details.