0

I have a Visualforce page with a page block section which contains multiple rows from different object (Result from query in controller from custom object called "Location__c"). I need to pass the id's of selected rows to controller and perform insert operation to be stored in another custom object called "Desiredlocation__c" with the id's that i received, "Desireclocation__c" is parenting to custom object called "TLead__c". Can anyone help? enter image description here

My VF

<apex:page docType="html-5.0" controller="incDesiredLocationsController">
<apex:pageMessages />
<apex:form >
    <apex:pageBlock title="New Desired Location">
    <div align="center">   
    <apex:commandButton value="Save" action="{!save}"/>
    </div>     
    <apex:pageblockSection title="Select Location" columns="1">      
            <apex:selectCheckboxes value="{!locationIds}" layout="PageDirection">
                <apex:selectOptions value="{!locationSelect}"/>
            </apex:selectCheckboxes><br/>
    </apex:pageblockSection>         
      </apex:pageBlock>
</apex:form>

My controller

public class incDesiredLocationsController {

//add multiple desired location

public List<SelectOption> getlocationSelect(){

List<SelectOption> locationSelect = new List<SelectOption>();
locationIds = new List<Id>();

List<Location__c> dloc = [SELECT City__c,Id, Region__c FROM Location__c where Country__c = 'ID' ORDER BY City__c asc];

for(Location__c loc:dloc)
    locationSelect.add(new SelectOption(loc.Id, loc.City__c));  

return locationSelect;

}

String[] locationIds = new String[]{};
public String[] getlocationIds () {
    return locationIds;
}

public void setlocationIds(String[] locationIds) {
    this.locationIds = locationIds;
}

public PageReference save(){
         //code block here
}

}

0

Remove desiredLocationList and implement below method:

--edited--

public List<SelectOption> getlocationSelect(){

    List<SelectOption> locationSelect = new List<SelectOption>();

    List<Location__c> dloc = [SELECT City__c,Id Region__c FROM Location__c where Country__c = 'ID' ORDER BY City__c asc];

    for(Location__c loc:dloc)
        locationSelect.add(new SelectOption(loc.City__c, loc.City__c));  

    return locationSelect;
}

and remove public List<SelectOption> locationSelect {get; set;}.

Now, implement below getter and setter:

String[] locationIds = new String[]{};
public String[] getlocationIds () {
    return locationIds;
}

public void setlocationIds(String[] locationIds) {
    this.locationIds = locationIds;
}

and remove public List<Id> locationIds{get; set;}. Above setter (set method) will automatically get the selected location ids.

Now,

public PageReference save(){
    System.debug('locationIds => '+locationIds);
    /*
    if(tleadName == null || tleadName == ''){
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'T Lead is Required!'));
    }else{
        Location__c loc1 = new Location__c();
        loc1.Name = tleadName1;
        loc1.Id = tleadId1;
        insert loc1;
    }*/
    return null;
}

Here System.debug('locationIds => '+locationIds); will print the selected location ids in log. You can process locationIds further.

  • Hi, salesforcesas I got error when saving the class Error: Compile Error: Variable does not exist: locationIds at line 54 column 5 , could you help me with this? and also would be very helpful if you can give reference or hint on how to perform the insert operation once the ID's are retrieved in the controller, should I perform for loop to iterate each ID then do something? – Adnan Zahry Jul 29 at 5:03
  • can you update code in question – salesforce-sas Jul 29 at 5:08
  • I have updated the code with your suggestions included, I also edited my question to make it more clear – Adnan Zahry Jul 29 at 6:34
  • added return type for getlocationSelect method and also declared String[] locationIds = new String[]{};. Please do changes in both places – salesforce-sas Jul 29 at 6:52
  • Sorry but I dont understand on your last comments, getlocationSelect has return type return locationSelect; ? – Adnan Zahry Jul 29 at 8:17

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.