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
Rakesh SRakesh S 

how to insert new records using SOQL

Hi All,

this is my query: its working well.
Merchandise__c mer = new Merchandise__c(Name='Book1',Description__c='New Book1',
                                        Price__c=400, Total_Inventory__c=50000);
// insert merchandise using DML
insert mer;
but i want to insert in another way for that one code here: its getting error like variable doent exit: Name.
List<Merchandise__c> mer = new List<Merchandise__c>{Name='Book1',Description__c='New Book1',Price__c=500, Total_Inventory__c=10000};
    upsert mer;
Could you pls tell me anyone, what is the different b/w them. And when i should use collection.

Thank you.
 
BalajiRanganathanBalajiRanganathan
Your list syntax is not correct here. you have create the list and then add elemetns
 
Merchandise__c mer = new Merchandise__c(Name='Book1',Description__c='New Book1',
                                        Price__c=400, Total_Inventory__c=50000);
List<Merchandise__c> merList = new List<Merchandise__c>();
merList.add(mer);

// insert merchandise using DML
upsert merList;

list is used to Optimize your code.

Refert Best Practice #4: Using Collections in the link below
https://developer.salesforce.com/page/Apex_Code_Best_Practices
Rakesh SRakesh S
Thank you balaji.. its woriking. but i want to pass new records fields while creating the instance of class. is it not possible...
BalajiRanganathanBalajiRanganathan
You can but it will not be readable
 
List<Merchandise__c> merList = new List<Merchandise__c>{
new Merchandise__c(Name='Book1',Description__c='New Book1', Price__c=400, Total_Inventory__c=50000),
new Merchandise__c(Name='Book2',Description__c='New Book2', Price__c=400, Total_Inventory__c=50000)};

// insert merchandise using DML
upsert merList;

another example
List<String> myStrings = new List<String> { 'one', 'two' };