+ Start a Discussion
VivoVivo 

Saving a list

Hi,

 

I have implemented a case in which a user can select a list of Feature object to add to a Car object. Right now ,the apex code generates a dynamic list of feature objects that the user can select and add to a custom list. My problem is that I would like this list to be tied to the Car object permanently after the user makes it (right now if the user refreshes the page, everything is reset and the list is emptied). How do I go about making this apex variable persistant to the custom object? Essentially it would be a List variable that gets saved into the database with the Car Object, kind of how the custom fields are.

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
jungleeejungleee

Hi Vivo,

 

Please find code with the below assumptions;

car object:- car__c

feature obect: feature__c

junctionObject :- yourJunctionoObject__c(replace this with your junction object name)

 

all you to do is create a class and then the VF page with the below code and then open this link /apex/youVfpagename?carId=(Id of an existing Car record),

 

public class AddFeaturesToCars{	
    
    //Variable Declaration
    public list<featureWrapperClass> featureWrapperList{get;set;}
    public id carId;
    
	//constructor
    public AddFeaturesToCar(){
        carId = ApexPages.currentPage().getParameters().get('carId');
    }
    
    public list<featureWrapperClass> getFeatureList(){
        featureWrapperList = new list<featureWrapperClass>();
        set<id> removeIdSet = new set<id>();
        for(yourJunctionoObject__c junc:[SELECT cars__c, feature__c 
                                                  FROM yourJunctionoObject__c 
                                                  WHERE cars__c =:carId]){
            removeIdSet.add(junc.feature__c);
        }
        for(feature__c feat:[SELECT id, name
                                               FROM feature__c 
                                               WHERE id NOT IN: removeIdSet]){
            featureWrapperList.add(new featureWrapperClass(feat));
        }
       return featureWrapperList;
    }
    
    
    public pageReference Add(){
        list<yourJunctionoObject__c> juncList = new list<yourJunctionoObject__c>();
        for(featureWrapperClass featSelected :featureWrapperList){
			//get only the selected records
            if(featSelected.selected){
			//create new junction record for every selected Feature.
                yourJunctionoObject__c junctionRecord = new yourJunctionoObject__c(car__c = carId,
                                                                        feature__c    = featSelected.feat.id);
                juncList.add(junctionRecord);
            }
        }
        if(juncList.size()>0){
			//this will create a recor din your junction object
            insert juncList;
            pageReference pageRef = new PageReference('/'+CarId);
            return pageRef;
        }
        else{
            Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please_select_a_record'));
            return null;
        }
    }
    
    
    public pageReference cancel(){
        pageReference pageRef = new PageReference('/'+carId);
        return pageRef;
    }
    //A wrapper class
    public class featureWrapperClass{
        public boolean selected{get;set;}
        public Feature__c feat{get;set;}
        public featureWrapperClass(Feature__c f){
            this.feat = f;
            this.selected= false;     
        }       
    }
}

 

<apex:page controller="AddFeaturesToCars">
    <apex:form >
        <apex:pageBlock >
        <apex:pageMessages />
        <apex:pageBlockButtons >
            <apex:commandButton action="{!Add}" disabled="{!IF((featureList.size==0),true,false)}" value="add"/>
            <apex:commandButton action="{!cancel}" value="cancel"/>
        </apex:pageBlockButtons>
        <apex:pageBlockTable rendered="{!IF((featureList.size!=0),true,false)}" value="{!featureList}" var="ft">
            <apex:column headerValue="select">
                <apex:inputCheckbox value="{!ft.selected}"/>
            </apex:column>
            <apex:column headerValue="name">{!ft.feat.Name}</apex:column>
        </apex:pageBlockTable>
        <apex:outputText rendered="{!IF((featureList.size==0),true,false)}"><b>No_records_found_</b></apex:outputText>
        </apex:pageblock>
    </apex:form>
</apex:page>

 

 

 

I hope this help!

 

-ಸಮಿರ್

 

All Answers

ForcepowerForcepower
Vivo, One way to do this is to have a custom field for each feature and have a checkbox field or a picklist field or may be other types of fields store the configuration as chosen by the user. I'm not sure if you already have created a Car__c custom object. If you have, all you need to do is to instantiate a record of that type in your controller, gather the data in your VF page and then insert into the database. Then the configuration selected for that instance is store away "permanently".
Ram
jungleeejungleee

Hi,

 

From what you have mentioned I gather that there is a Many -to- Many relationship between Car and feature object i.e., one car can be associated to many features and similarly one feature can be associated to many Cars. In this case you would need a junction object like Car_Feature_Association. In this junction object create 2 master detail relations one to cars and another to features. This junction object is seen as a related list both the objects.

 

Now you display the list of features for a particular car ,users selects only 2 features out of the list and clicks on save. Now You have to add 2 records in the junction object where you populate the 2 master detail relationship fields one with Car Id and other with the feature Id.

 

Hope this helps!

 

-ಸಮಿರ್

VivoVivo
I have already created the Car__c custom object with a many-to-many relationship(Junction Object) with the Features__c object. The issue I run into having a custom field for each feature is that the features list is changed rapidly, so it is not static. One solution I was hoping to implement is a multiple picklist that pulls up all of the different features currently available, and then lets users pick them. Currently my visualforce page displays all of the features by pulling them from the database and iterating, and it also allows users to select them, and then I add the selected ones to the page. But I need a way to save those selected features, whether it is by storing an array of names that is tied to the Car__c object instance or to directly add the junction object to the Car__c object (Basically a visualforce command that adds elements in the related list). Any thoughts on this?

Thanks for your help so far!
VivoVivo
Hi,

I already have the junction object set up actually. I am looking for a visualforce code that will directly add the junction object to the related list. I basically need a way to access the individual elements in the related list for the Car__c object, that allows me to remove/add Feature__c objects to it. Any thoughts on this?

Thanks again for your help
ForcepowerForcepower
Vivo, I think what you would need to do is to create a Feature__c record for each feature that was chosen and associate the master/lookup Car__c id for it. The following pseudocode just does it on one feature - but you should be able to do it in a loop to get all your features in into a List<Feature__c> and then insert them all at the same time.

Car__c car = new Car__c();
// set up car and insert it
insert car;


Feature__c f = new Feature__c();
f.car__c = car.id;
f.field1 = some user input
//assign other fields to feature

insert f;
Hope this helps.

Ram
jungleeejungleee

Hi Vivo,

 

Please find code with the below assumptions;

car object:- car__c

feature obect: feature__c

junctionObject :- yourJunctionoObject__c(replace this with your junction object name)

 

all you to do is create a class and then the VF page with the below code and then open this link /apex/youVfpagename?carId=(Id of an existing Car record),

 

public class AddFeaturesToCars{	
    
    //Variable Declaration
    public list<featureWrapperClass> featureWrapperList{get;set;}
    public id carId;
    
	//constructor
    public AddFeaturesToCar(){
        carId = ApexPages.currentPage().getParameters().get('carId');
    }
    
    public list<featureWrapperClass> getFeatureList(){
        featureWrapperList = new list<featureWrapperClass>();
        set<id> removeIdSet = new set<id>();
        for(yourJunctionoObject__c junc:[SELECT cars__c, feature__c 
                                                  FROM yourJunctionoObject__c 
                                                  WHERE cars__c =:carId]){
            removeIdSet.add(junc.feature__c);
        }
        for(feature__c feat:[SELECT id, name
                                               FROM feature__c 
                                               WHERE id NOT IN: removeIdSet]){
            featureWrapperList.add(new featureWrapperClass(feat));
        }
       return featureWrapperList;
    }
    
    
    public pageReference Add(){
        list<yourJunctionoObject__c> juncList = new list<yourJunctionoObject__c>();
        for(featureWrapperClass featSelected :featureWrapperList){
			//get only the selected records
            if(featSelected.selected){
			//create new junction record for every selected Feature.
                yourJunctionoObject__c junctionRecord = new yourJunctionoObject__c(car__c = carId,
                                                                        feature__c    = featSelected.feat.id);
                juncList.add(junctionRecord);
            }
        }
        if(juncList.size()>0){
			//this will create a recor din your junction object
            insert juncList;
            pageReference pageRef = new PageReference('/'+CarId);
            return pageRef;
        }
        else{
            Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please_select_a_record'));
            return null;
        }
    }
    
    
    public pageReference cancel(){
        pageReference pageRef = new PageReference('/'+carId);
        return pageRef;
    }
    //A wrapper class
    public class featureWrapperClass{
        public boolean selected{get;set;}
        public Feature__c feat{get;set;}
        public featureWrapperClass(Feature__c f){
            this.feat = f;
            this.selected= false;     
        }       
    }
}

 

<apex:page controller="AddFeaturesToCars">
    <apex:form >
        <apex:pageBlock >
        <apex:pageMessages />
        <apex:pageBlockButtons >
            <apex:commandButton action="{!Add}" disabled="{!IF((featureList.size==0),true,false)}" value="add"/>
            <apex:commandButton action="{!cancel}" value="cancel"/>
        </apex:pageBlockButtons>
        <apex:pageBlockTable rendered="{!IF((featureList.size!=0),true,false)}" value="{!featureList}" var="ft">
            <apex:column headerValue="select">
                <apex:inputCheckbox value="{!ft.selected}"/>
            </apex:column>
            <apex:column headerValue="name">{!ft.feat.Name}</apex:column>
        </apex:pageBlockTable>
        <apex:outputText rendered="{!IF((featureList.size==0),true,false)}"><b>No_records_found_</b></apex:outputText>
        </apex:pageblock>
    </apex:form>
</apex:page>

 

 

 

I hope this help!

 

-ಸಮಿರ್

 

This was selected as the best answer