[SalesForce] List view button to pass selected record ids on to new record for custom object

I am trying to create an easy way to mark attendance. I have a custom object "students" as well as a custom object "attendance." The attendance object is related to the student object by a lookup field (student ID).

I would like to use the list view of my student object to select each student present. Then use a button to create an attendance object record for each student selected and have it related to each student by the lookup field.

I am able to get the button to create an attendance object record for each student selected, but it will not populate the Student Id look up field.

Best Answer

Not tested, but something like this should work

    {!REQUIRESCRIPT("/soap/ajax/27.0/connection.js")}     
    var url = parent.location.href;       
    var records = {!GETRECORDIDS($ObjectType.Student__c)}; 
    var newRecords = [];  

    if (records[0] == null) { 
        alert("Please select at least one record to update.");     
    } else { 
        for (var a=0; a<records.length; a++) {              
            var newAttendance = new sforce.SObject("Attendance__c");
            newAttendance.Student__c = records[a]; 
            newRecords.push(newAttendance);  
        }    
        result = sforce.connection.insert(newRecords); 
        parent.location.href = url; //refresh the page 
    }
Related Topic