0

Hi everyone I have a scenario where I can select the fields and that can be either multiplied/subtracted/added/divided and that result should be updated in another field.

Using describeobject class i'm getting all the fields from the object but when i try to query the fields which i have selected from visualforce page i'm getting query exception because the query string is taking the selected value as string but not as api.

Ultimately i want to pass field api from visualforce page to controller to query that particular field.

Here is what i have done

This is how it looks picklist are my fields in object I select value1 , value 2 and which field to be updated and finally i select the operator and vl click on update

VF Page

<apex:page standardController="My_Expense__c" extensions="calculator1" >
<apex:form >

    <apex:pageBlock >
        <apex:pageBlockSection columns="1" >

            <apex:pageBlockSectionItem >
                    <apex:outputLabel >Value 1</apex:outputLabel>
                    <apex:selectList multiselect="false" size="1" value="{!field1}">
                    <apex:actionsupport event="onchange" action="{!init}"/>                                        
                    <apex:selectOptions value="{!seopt}" />
                    </apex:selectList>
            </apex:pageBlockSectionItem>

            <apex:pageBlockSectionItem >
                    <apex:outputLabel >Value 2</apex:outputLabel>
                    <apex:selectList multiselect="false" size="1" value="{!field1}">
                        <apex:selectOptions value="{!seopt}" />
                    </apex:selectList>
            </apex:pageBlockSectionItem>

            <apex:pageBlockSectionItem >
                    <apex:outputLabel >Update</apex:outputLabel>
                    <apex:selectList multiselect="false" size="1" value="{!f_update}">
                        <apex:selectOptions value="{!seopt}" />
                    </apex:selectList>
            </apex:pageBlockSectionItem>

            <apex:pageBlockSectionItem >
                <apex:selectRadio value="{!func}" layout="pageDirection">
                    <apex:selectOption itemValue="add" itemLabel="Add"/>
                    <apex:selectOption itemValue="sub" itemLabel="Subtract"/>
                    <apex:selectOption itemValue="Mul" itemLabel="Multiply"/>
                    <apex:selectOption itemValue="div" itemLabel="Division"/>
                    <apex:selectOption itemValue="mod" itemLabel="Modulo Division"/>
                </apex:selectRadio>
            </apex:pageBlockSectionItem>

        </apex:pageBlockSection>    
        <apex:pageBlockButtons >

            <apex:commandButton value="Update" action="{!init}"/>

        </apex:pageBlockButtons>
    </apex:pageBlock>

</apex:form>
</apex:page> 

Controller

![public class calculator1 {

    public List<My_Expense__c> ex = new List<My_Expense__c>();
    public List<My_Expense__c> ex1 = new List<My_Expense__c>();

    public List<My_Expense__c> ex2 = new List<My_Expense__c>();

    public List<My_Expense__c> upd = new List<My_Expense__c>();

    public string field1{get;set;}
    public string field2{get;set;}
    public integer f_update{get;set;}

    public string qry;
    public string qry1;
    public string qry2;

    public calculator1(ApexPages.StandardController controller) {

        String commaSepratedFields = '';
        String SobjectApiName = 'Sharat_Sushma__My_Expense__c';
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();

        for(String fieldName : fieldMap.keyset()){
            if(commaSepratedFields == null || commaSepratedFields == ''){
                commaSepratedFields = fieldName;
            }else{
                commaSepratedFields = commaSepratedFields + ', ' + fieldName;
            }
        }

        qry1 = 'select ' + commaSepratedFields + ' from ' + SobjectApiName ;
        ex2 = database.query(qry1);

        qry2 = 'select ' + field2 + ' from ' + SobjectApiName ;

        system.debug('AAAAAAAAAAAAAAAAAAAAAAAAAA'+qry2);

        for(Sharat_Sushma__My_Expense__c exp : ex2){

           /* field2 = string.valueof(exp.Rate__c);
            f_update = string.valueof(exp.Rate1__c);

            exp.Amount__c = integer.valueof(field2) * integer.valueof(f_update);

            upd.add(exp);*/

        }


    }

       public void init(){

        update upd;

    }

    public List<SelectOption> sel = new List<SelectOption>();

    String SobjectApiName = 'Sharat_Sushma__My_Expense__c';
    Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();

    public list<SelectOption> getseopt(){
        qry = 'Select ';
        for(String fieldName : fieldMap.keyset()){
            sel.add(new selectoption(string.valueof(fieldname),string.valueof(fieldname)));

        }

        return sel;
    }


    public Double result {get;set;}
    public String func {get;set;}


    public void find()
    {
        system.debug('!!!!!!!!!!!!!!!!!!!!'+ex1);

        if(func == 'add')
        {
            f_update= integer.valueof(field1)+ integer.valueof(field2);
        }
        else if(func == 'sub')
        {
             f_update= integer.valueof(field1)- integer.valueof(field2);
        }
        else if(func == 'div')
        {
             f_update= integer.valueof(field1)/ integer.valueof(field2);
        }
        else if(func == 'Mul')
        {
            f_update= integer.valueof(field1)* integer.valueof(field2);
        }
        else
        {
             //Integer temp =  math.mod(Integer.valueOf(val1) , Integer.valueOf(val2));
             //result = Double.valueOf(temp);
        }
    }



}][2]
  • I just can't understand how you want to multiply two API field names??? Status__c * Status__c Why? What is the catch? – Sergej Utko Mar 11 '15 at 7:20
  • no no that's a list of fields in object i'm getting as picklist. I have one fields called rate1__c and rate__c will be my value1 and value2. There is another field called Amount__c that will be my field where result will be updated. – Sharat Mar 11 '15 at 7:23
  • 1
    where is the error? What is the error? Put a few debugs before it happens and log all variables that can cause that error – Novarg Mar 11 '15 at 7:34

Your Answer

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

Browse other questions tagged or ask your own question.