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
sp13sp13 

how to create mutiple records using selected ids

hi. i have a problem in saving the selected ids.

this is the id of the selected records:

for (integer i=0 ;i<selectedStudents.size();i++){
    //other codes
    selectedStudents.get(i).id
}

 how can i create multiple records using this?

 

 

Best Answer chosen by Admin (Salesforce Developers) 
magicforce9magicforce9

Hi,

 

You can to create a object record using that id and then you can save any further changes you make to that record. You can see the sample code below.

 

 

//Use a list to capture the records that needs to updated, so you can execute one DML to update all records
List<Student__c> students = new List<Student__c>();

for (integer i=0 ;i<selectedStudents.size();i++){
    //other codes
    
    //Create an object record of whom the ID belongs to 
    Student__c s = new Student__c(id = selectedStudents.get(i).id);

    //You can add values few other fields
    s.some_field__c = 'Sample Value';

    //Add the record to a list so it can be updated later outside of for loop
    students.add(s);

}
//this DML statement will update all the student records in your object
update students;