1

I have a custom object that will hold data like column, operator and values. The user interface will allow a user to build a sql query online. The user can then Add/Delete conditions. I have an extension that has a Save method. I have a debug statement in my save condition. I am unable to invoke this method. Can someone please help?

My apex page is as follows:

    <apex:pageblock id="pb1">

    <apex:repeat value="{!lstInner}" var="e1" id="therepeat">
            <apex:panelGrid columns="5">

            <apex:panelGrid headerClass="Name">
                <apex:facet name="header">Del</apex:facet>
                <apex:commandButton value="X" action="{!Del}" rerender="pb1">
                    <apex:param name="rowToBeDeleted" value="{!e1.recCount}" assignTo="{!selectedRowIndex}"></apex:param>
                </apex:commandButton>
            </apex:panelGrid>   

            <apex:panelGrid title="SPD" >
                <apex:facet name="header">Condition</apex:facet> 
                      <apex:selectList value="{!e1.acct.Condition__c}" multiselect="false" size="1">
                            <apex:selectOptions value="{!conditions}"/>
                      </apex:selectList>                                                                         
            </apex:panelGrid>


            <apex:panelGrid >
                <apex:facet name="header">Field</apex:facet>                    
                 <apex:selectList value="{!e1.acct.Field__c}" multiselect="false" size="1">
                            <apex:selectOptions value="{!items}"/>
                      </apex:selectList>            
            </apex:panelGrid>

            <apex:panelGrid >
                <apex:facet name="header">Comparison</apex:facet>
                <apex:selectList value="{!e1.acct.Comparison__c}" multiselect="false" size="1">
                            <apex:selectOptions value="{!comparisons}"/>
                </apex:selectList>                       
            </apex:panelGrid> 

            <apex:panelGrid >
                <apex:facet name="header">Value</apex:facet>
                <apex:inputfield value="{!e1.acct.Value__c}"/>
            </apex:panelGrid>                               

        </apex:panelgrid>
    </apex:repeat>
</apex:pageBlock>

public with sharing class MultiAdd
{

public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
public String selectedObject {get; set;}
public String selectedField {get; set;}    
public String inputValue{get; set;}
public String selectedCondition{get; set;}
public String selectedComparison{get; set;}

public SelectOption[] conditionOptions { //this is where we're going to pull the list
    public get;
    private set;
}

public SelectOption[] comparisonOptions { //this is where we're going to pull the list
    public get;
    private set;
}

public List<SelectOption> getConditions() 
{
        List<SelectOption> options = new List<SelectOption>();
        String[] conditions= new String[]{'AND','OR','NOT'};
        for (String c: conditions) 
        {
           options.add( new SelectOption(c,c));
           }
        return options;
}

public List<SelectOption> getComparisons() 
{
        List<SelectOption> options = new List<SelectOption>();
        String[] comparisons= new String[]{'equals','not equal','greater than','less than','greater than or equal','less than or equal','starts with'}; 
        for (String c: comparisons) 
        {
           options.add( new SelectOption(c,c));
           }
        return options;
}


public List<SelectOption> getItems() {

        List<SelectOption> options = new List<SelectOption>();
        //options.add(new SelectOption('US','US'));
        //options.add(new SelectOption('CANADA','Canada'));
        //options.add(new SelectOption('MEXICO','Mexico'));
        List<Schema.SObjectField> optionsList = Schema.SObjectType.Contact.fields.getMap().values();
        for( SObjectField s : optionsList )
        {
            options.add( new SelectOption(s.getDescribe().getLabel(), s.getDescribe().getLabel()) );
        }

        return options;
    }

public List<SelectOption> getObjectFields() 
 {
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Schema.SObjectType ObjectSchema = schemaMap.get(selectedObject);
        Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap();
        List<SelectOption> fieldNames = new List<SelectOption>();
        for (String fieldName: fieldMap.keySet()) 
        {  
            fieldNames.add(new SelectOption(fieldName,fieldName));
          //fieldMap.get(fieldName).getDescribe().getLabel();//It provides to get the object fields label.
        }
        return fieldNames;
  }                 






//will hold the SOQLQuery__c records to be saved
public List<SOQLQuery__c>lstAcct  = new List<SOQLQuery__c>();

//list of the inner class
public List<innerClass> lstInner 
{   get;set;    }

//will indicate the row to be deleted
public String selectedRowIndex
{get;set;}  

//no. of rows added/records in the inner class list
public Integer count = 1;
//{get;set;}


//add one more row
public void Add()
{   
    count = count+1;
    addMore();      
}

/*Begin addMore*/
public void addMore()
{
    //call to the iner class constructor
    innerClass objInnerClass = new innerClass(count);

    //add the record to the inner class list
    lstInner.add(objInnerClass);    
    system.debug('lstInner---->'+lstInner);            
}/* end addMore*/

/* begin delete */
public void Del()
{
    system.debug('selected row index---->'+selectedRowIndex);
    lstInner.remove(Integer.valueOf(selectedRowIndex)-1);
    count = count - 1;

}/*End del*/



/*Constructor*/
public MultiAdd(ApexPages.StandardController ctlr)
{
System.debug('clicked');
    lstInner = new List<innerClass>();
    addMore();
    selectedRowIndex = '0';

}/*End Constructor*/ 

  ////save the records by adding the elements in the inner class list to lstAcct,return to the same page
public PageReference saveCustom()
{
    System.debug('clicked');
    PageReference pr = new PageReference('/apex/MultiAdd');

    for(Integer j = 0;j<lstInner.size();j++)
    {
        lstAcct.add(lstInner[j].acct);

        System.debug(lstInner[j].acct.Condition__c);
    } 
    insert lstAcct;
    pr.setRedirect(True);
    return pr;
}

  ////save the records by adding the elements in the inner class list to lstAcct,return to the same page
public PageReference Save()
{
    System.debug('clicked');
    PageReference pr = new PageReference('/apex/MultiAdd');

    for(Integer j = 0;j<lstInner.size();j++)
    {
        lstAcct.add(lstInner[j].acct);

        System.debug(lstInner[j].acct.Condition__c);
    } 
    insert lstAcct;
    pr.setRedirect(True);
    return pr;
}    


/*Inner Class*/
public class innerClass
{       
    /*recCount acts as a index for a row. This will be helpful to identify the row to be deleted */
    public String recCount
    {get;set;}


    public SOQLQuery__c acct 
    {get;set;}

    /*Inner Class Constructor*/
    public innerClass(Integer intCount)
    {
        recCount = String.valueOf(intCount);        

        /*create a new SOQLQuery__c*/
        acct = new SOQLQuery__c();

    }/*End Inner class Constructor*/    
}/*End inner Class*/
}/*End Class*/
1
  • where is your save button? Nov 25 '16 at 3:29
0

Everywhere you are using pr.setRedirect(True); this will aways disregard the page state and give you fresh page again. You should maintain the page state so use:

public PageReference Save()
{
    System.debug('clicked');
    PageReference pr = ApexPages.currentPage(); // redirect to same page with all params (if any)

    for(Integer j = 0;j<lstInner.size();j++)
    {
        lstAcct.add(lstInner[j].acct);

        System.debug(lstInner[j].acct.Condition__c);
    } 
    insert lstAcct;
    pr.setRedirect(false); // keeps the state.
    return pr;
}  

However, it is not necessary to write pr.setRedirect(false); as false this is the default value.

Apply above approach to other methods. I assume that you have set render attributes correctly.

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.