0

I have a table that displays the ChildObject of the current object, when I click on a button it sends me to a VF where i can see the ChildObject in a table and I have a checkBox in every row, It works just as I want, but I need to get the all the values of the selected rows, then I need to insert this values in a different Object.

public void cloneSelectedObjects(){
   for (relatedObjects relatedObject : objectChildren){
       for (relatedObjectRow row : relatedObject.objectRows){
           if (row.selected){
               System.debug(row);
           }
       }
   }
}

Here is the debug:

DEBUG|relatedObjectRow:[obj=Purchase_Order_Details__c:{Account__c=Norduac Naturals Test, Name=PO-45488, NRCode__c=105810000, Id=a0cf0000003bHHuAAM, Description__c=test, QTY__c=0}, selected=true]

I need to store these values some how so they can be insert in different object.

Thank you in advance!

  • Are you having issues accessing the data or just wondering the best approach to hold the information temporarily? I'm just thinking since you already have the record at the point of your if statement, make a call to another method that does your mapping. – Double A Mar 4 '14 at 20:35
1

If you are asking how to map your selected rows via apex, I would do something like this and do an insert once at the end.

public void cloneSelectedObjects(){
   List<MyCloneRecord> cloneRecords = new List<MyCloneRecord>();
   for (relatedObjects relatedObject : objectChildren){
       for (relatedObjectRow row : relatedObject.objectRows){
           if (row.selected){
               cloneRecords.add(mapMyRecord(row));
           }
       }
   }
   if(cloneRecords.size() > 0)
      insert cloneRecords;
} 

public MyNewCloneRecord mapMyRecord(relatedObjectRow mySelectedRecord){
   MyNewCloneRecord cloneRecord = new MyCloneRecord();
   cloneRecord.Account__c = mySelectedRecord.Account__c;
   ... etc
   return cloneRecord;
}

Example if you are mapping from 1 sobject to another seperate sobject with the exact same api field names dynamicly

// this is defined somewhere above to prevent multiple calls to get the same information
// get sobject meta data 
Map<String, Schema.SObjectField> fieldMapping = new Map<String, Schema.SObjectField>();     
SObjectType sot = Schema.getGlobalDescribe().get(relatedObjects);
fieldMapping = sot.getDescribe().fields.getMap();
Set<String> fieldAPINames = fieldMapping.keySet()


public void cloneSelectedObjects()
    for (relatedObjects relatedObject : objectChildren) { 
        List<String> lista= new List<String>(); 
        for (relatedObjectRow row : relatedObject.objectRows) { 
            if (row.selected) { 
                MyNewCloneRecord cloneRecord = new MyNewCloneRecord();
                // loop through field api name
                for(String s :fieldAPINames){   
                    cloneRecord.put(s,row.Purchase_Order_Details__c.get(s));
                }
                cloneRecords.add(cloneRecord);
            }
        }
    }
}
|improve this answer|||||
  • Thank you for your answer, I was able to get those values in a array[], but I just want to get the values without the labels. As you see in the debug USER_DEBUG [143]|DEBUG| Name=PO-45488 NRCode__c=105810000 Id=a0cf0000003bHHuAAM Description__c=test QTY__c=0} bellow is the code I used: – Carlos Mar 4 '14 at 20:51
  • public void cloneSelectedObjects()for (relatedObjects relatedObject : objectChildren) { List<String> lista= new List<String>(); for (relatedObjectRow row : relatedObject.objectRows) { if (row.selected) { lista.add(row.toString()); //here for loop to get values for (Integer i=0;i<lista.size();i++) { String[] inputvalues = new String[]{}; inputvalues = lista[i].split(','); – Carlos Mar 4 '14 at 20:57
  • Hello Double A, I am trying to use your code but I am getting the following error: Invalid type: MyCloneRecord at line 125 column 47 ( List<MyCloneRecord> cloneRecord = new List<MyCloneRecord>();) any ideas? – Carlos Mar 4 '14 at 21:11
  • Well you need to replace MyCloneRecord with the real Sobject API name. Also you are currently creating a string out of your record. If you want to get the values out staticly, my example should suffice, if you want to do this dynamicly, i would recommend doing something like what I added to my answer – Double A Mar 4 '14 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.