+ Start a Discussion

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

 how can i create multiple records using this?



Best Answer chosen by Admin (Salesforce Developers) 



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

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