0

I have created a select list which identifies the list of sales reps which are present in User table but not present in User_Stats__c table. Based on the selection in select list, I want to add record for that sales rep in User_Stats__c table. In particular, I want to add following fields: rep id (Sales_rep_c), Month_c, Year_c, Goal_c. What is the method to do this on visualforce page? Please help. I am stuck on this since some time now..

Controller code used for creating the select list:

public list <SelectOption> getAddosr()
    {
        List <selectoption> lsosradd = new List<selectoption> ();
        lsosradd.add(new SelectOption('', '--None--'));
        Integer regnInt = (regn!=null) ? Integer.valueOf(regn) : -1;
        List<String> newString = new List<String>();
        for(User_Stats__c ust : [SELECT Sales_Rep__c FROM User_Stats__c where Region__c =: regnInt and District__c =: dist and District__c != null and Region__c != null and Region__c != 1 and Region__c != 0 and Sales_Rep__c != null])
        {
            newString.add(ust.Sales_Rep__c);
        }
        Set<String> repSet1 = new Set<String>();
        for (User srep :  [SELECT Name FROM User where Region__c =: regnInt and District__c =: dist and District__c != null and Region__c != null and Region__c != 1 and Region__c != 0 and Sales_Rep__c != null and Sales_Rep__c NOT IN: newString])
        {
            if(!repSet1.contains(srep.Name)) 
                    lsosradd.add(new selectoption(srep.Name, srep.Name));
            repSet1.add(srep.Name);
        }
        return lsosradd;
    }

Thanks in advance.

1

To add new record for the SalesRep you will need some reference to it. For this you have created SelectOption list getAddosr(). Here are basic steps to save record on visualforce page. Read about each component used here for clear understanding :-

Reference SelectOption list on visualforce pagelike this (Read about using SelectList here):

<apex:selectList value="{!SalesRepId}" multiselect="true">
     <apex:selectOptions value="{!Addosr}"/>
</apex:selectList>

Now add <apex:commandbutton> on visualforce page and define action like this:

<apex:commandButton action="{!saveNewRecord}" value="Save"/>

Now create same method in controller class which is defined in above action attribute. like this:

public String salesRepId {get; set;} // initialise it in constructor

public void saveNewRecord()
{
    // Save logic using variable "salesRepId"
    // Ex: List<User> rep = [SELECT .. from User Where SalesRep__c in :salesRepId]
    // DML etc..
}

Also change line lsosradd.add(new selectoption(srep.Name, srep.Name)); with lsosradd.add(new selectoption(srep.Id, srep.Name));

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.