0

In constructor, for string, we assign value to variable this way ' '

for decimal/double, how to assign value to variable in constructor?

I tried 0, but, in database, 0 is stored rather than user input in VF page. If I dont declare price/quantity, the child record is not created. wrapper class is used to create child record & price/quantity are the fields of child record. Say, user enter 2 as price, 4 as quantity, then 2,4 should be stored in database,, but 0,0 (constructor value is stored in the database.)

custom controller

public class JournalMC_class{
SObject record;

public Journal__c objJour {get;set;}
public String objCompany {get;set;}
public General_Ledge_Account__c objGLA {get;set;}
//  public Item__c objItem {get; set;}
// public Line_Item__c objLine {get; set;}//  public Product_Inv__c objProduct {get; set;}    public Attachment attachment {get; set;}  public boolean pb1Rendered{get;set;} // 
public boolean pb2Rendered{get;set;} // 
public boolean pb3Rendered{get;set;} // true -> show pb3
public String selectedRT{get;set;} // selected value of selectList
public double unitPrice{get; set;}
public double quantity{get; set;}//  public double unitPrice2{get; set;}//  public double quantity2{get; set;}   // public double outputField{get; set;}
    public integer selectedId{get;set;} // wrapperClass id selected line item to delete row. 
public double Total_value{get; set;}  public List<Product_Inv__c> pricePerUnit {get; set;}
   public List<wrapperClass> listWrapper {get; set;}
public class wrapperClass{
      public decimal price {get; set;}
     public decimal quantity1 {get; set;}
    public String product {get; set;}
    public Line_Item__c objLine {get; set; }
 //   public Product_Inv__c objProduct {get; set; }
    public Double outputFieldWrapper {get; set;}
    public Integer index{get;set;} // for index line item for delete item
    public wrapperClass(){
   //     price = decimal.valueOf(' ');
     //   quantity1 = decimal.valueOf(' ');
        product = ''; 
        objLine = new Line_Item__c();

    }
}   
//soql
//  List<General_ledge_Account__c> GLA_filtered_Category = [SELECT id, name FROM General_ledge_Account__c WHERE Operational_Category__c = true];
private final Journal__c journal;
// Constructor
public JournalMC_class() { //ApexPages.StandardController controller
    List<Journal__c> journal = [SELECT Id, Name FROM Journal__c]; //       WHERE Id = :ApexPages.currentPage().getParameters().get('id')
    if (journal.size() > 0)
    {
        Journal__c j = journal.get(0);
    }
    pricePerUnit = [Select Price_Unit__c from Product_Inv__c ];
    //   
      attachment = new Attachment();

    pb1Rendered = true;
    pb2Rendered = false;
    pb3Rendered = false;
    selectedRT = '';
    listWrapper = new List<wrapperClass>();
    wrapperClass wc=new wrapperClass();
    wc.index=0; // index 0 for first element 
    listWrapper.add(wc);
    //listWrapper.add(new wrapperClass());
    // listWrapper = new List<wrapperClass>();
  //  listWrapper.add(new wrapperClass());


    objJour = new Journal__c();
    objGLA = new General_Ledge_Account__c();

    Date myDate = Date.today(); // Define a variable to store today's date
    objJour.Journal_Date__c = myDate; // Assign default value for inputField
    //  unitPrice = 0;
    // record = controller.getRecord();  // not sure about this line
    Total_value=0;
    selectedId=listWrapper.size()-1; // for deleting last line item

}
// Function for uploading files from VF page
/*  public void upload(Id ParentId) {Journal__c objJour = new Journal__c(); Attachment objA = new Attachment();objA.OwnerId = UserInfo.getUserId();objA.ParentId = ParentId; // the record the file is attached to objA.IsPrivate = true;objA.name = attachment.Name; objA.Body = attachment.Body; if(objA.Body != null){ insert objA; ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));}}*/  public void upload(Id ParentId) {
    Attachment objA = new Attachment();
    objA.OwnerId = UserInfo.getUserId();
    objA.ParentId = ParentId; // the record the file is attached to
    objA.IsPrivate = true;
    objA.name = attachment.Name;
    objA.Body = attachment.Body;
    if(objA.Body != null){
        objA.Description = attachment.Description;
        insert objA;
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    }
}
public String selectedValue{    get;    set;
                           }
// Picklist 
public void onChangeSelectRadio(){
    if(selectedRT=='INCOME'){
        pb1Rendered = true;
        pb2Rendered = false;
        pb3Rendered = false;
        objJour.Type__c = 'INCOME';
    }
    else if(selectedRT=='EXPENSE'){
        pb1Rendered = false;
        pb2Rendered = true;
        pb3Rendered = false;
        objJour.Type__c = 'EXPENSE';
    }
    else if(selectedRT=='TRANSFER'){
        pb1Rendered = false;
        pb2Rendered = false;
        pb3Rendered = true;
        objJour.Type__c = 'TRANSFER';
    }
}
public PageReference save_custom(){
    List<Line_Item__c> objLine = new List<Line_Item__c>();
    objJour.Journal_Date__c=System.today(); 
    //   objJour.General_Ledger_Account__c = objGLA.id;
    //    objJour.GLA_cash_source__c = objGLA.id;
    if(objJour.Type__c == null )
    {
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Journal type is required.');
        ApexPages.addMessage(myMsg);
        return null;
    }
    if(objJour.Journal_Date__c == null )
    {
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Journal Date is required.');
        ApexPages.addMessage(myMsg);
        return null;
    } 
    if(objCompany != null && objCompany !='')
    {
        objJour.Company__c = objCompany; 
    }   
    insert objJour;

    for(wrapperClass L:listWrapper){
        Boolean doCreate = false;

        Line_Item__c obj = L.objLine;
        obj.Journal__c = objJour.id; 
        if(L.quantity1 != null ){ // && L.quantity1.trim() != ''
            obj.quantity__c = L.quantity1 ;
            doCreate = true;
        } 
           if(L.price != null){  // && L.price.trim() != ''
            obj.Price_Per_Unit__c = L.price;
            doCreate = true;
        }   

         if(L.product != null && L.product.trim() != ''){
            obj.Product_Inv__c = L.product ;
            doCreate = true;
        }      
         if(doCreate){
            objLine.add(obj);
        } 

    }
    if(objLine.size() > 0){
        Insert objLine; 
    //    objJour.Total_Value__c = Total_Value;
      //  update objJour; 
    }
    System.Debug('message');
    upload(objJour.Id); // edited
    PageReference pr = new PageReference('/' +objJour.Id);  // redirect the page into detail page

    return pr;
}
public PageReference cancel_custom(){
    PageReference pr = new PageReference('/apex/Journal_Money_Control');  
    pr.setRedirect(true);     
    return pr;
}
// Add Child object record e.g. Line Item
public void addLineItem(){

    wrapperClass objW = new wrapperClass();
    objW.index=listWrapper.size();
    listWrapper.add(objW);    
    selectedId=listWrapper.size()-1; // for deleting last line item                                                                                                                                                                                    

}
// Delete Child object record
public void deleteLineItem(){
    //system.debug('enter into deleteLineItem-->selectedId-->'+selectedId + '--listWrapper.size()---'+listWrapper.size());

    listWrapper.remove(selectedId ); // delete button as dynamic 
    for(integer i=0;i<listWrapper.size();i++)
    {
        listWrapper.get(i).index=i;
    }
    selectedId=listWrapper.size()-1; // for deleting last line item
}  
public void recalculateFormulas() {
    record.recalculateFormulas();
}
public void calculateRoomCharge()
{
    unitPrice = 0;
    quantity = 0;
    Total_Value =0;
    for(wrapperClass obj : listWrapper)
    {
        SObject record = (Line_Item__c)obj.objLine;
        record.recalculateFormulas();
        if(obj.objLine.Price_Per_Unit__c != null && obj.objLine.Quantity__c != null){unitPrice = obj.objLine.Price_Per_Unit__c;  
            quantity = obj.objLine.Quantity__c;    }    
        obj.outputFieldWrapper = unitPrice*quantity ;
        Total_Value += obj.outputFieldWrapper ;  
    }
}
    // Fetch the Price /unit for a certain product/* public void calculateRoomCharge1(){system.debug('enter calculateRoomCharge1');List<Product_Inv__c> productInvList = [SELECT id, name, Price_Unit__c FROM Product_Inv__c ];Map<id,Product_Inv__c> productInvMap=new Map<id,Product_Inv__c>(); // get price/unit for selected product for(Product_Inv__c p:productInvList)      { productInvMap.put(p.id,p);

}unitPrice = 0;quantity = 0; Total_Value =0; for(wrapperClass obj : listWrapper) { SObject record = (Line_Item__c)obj.objLine; id rid=(Id )record.get('Product_Inv__c'); obj.objLine.Price_Per_Unit__c=productInvMap.get(rid).Price_Unit__c; record.recalculateFormulas(); if(obj.objLine.Price_Per_Unit__c != null && obj.objLine.Quantity__c != null){unitPrice = obj.objLine.Price_Per_Unit__c; quantity = obj.objLine.Quantity__c; }
obj.outputFieldWrapper = unitPrice*quantity ; Total_Value += obj.outputFieldWrapper ;
} } / / Controller: */ //public class dataTableCon { List journals; public List getJournals() { if(journals == null) journals = [SELECT name, Type__c, Total_Value__c, Journal_Date__c, Journal_Description__c, (SELECT Name, Product_Inv__c, Quantity__c FROM Line_Items__r) FROM Journal__c ORDER BY Journal__c.CreatedDate DESC LIMIT 20 ]; return journals; }

//} }

Part of the code

public List<wrapperClass> listWrapper {get; set;}
public class wrapperClass{
      public decimal price {get; set;}
     public decimal quantity1 {get; set;}
    public String product {get; set;}
    public Line_Item__c objLine {get; set; }
 //   public Product_Inv__c objProduct {get; set; }
    public Double outputFieldWrapper {get; set;}
    public Integer index{get;set;} // for index line item for delete item
    public wrapperClass(){
        price = ; //decimal.valueOf(' ')
        quantity1 = decimal.valueOf(' ');
        product = ''; 
        objLine = new Line_Item__c();

    }
2
  • 1
    Looks like your wrapper class variable are not binding priprtly or you might be doing something wrong in save method. Can you also add some more relevant code.
    – Raul
    Commented Feb 11, 2017 at 10:16
  • save method is working good for all fields EXCEPT price,quantity
    – Tanmoy Das
    Commented Feb 11, 2017 at 16:16

1 Answer 1

2

Apex handles the type conversion of numeric constants well for Decimals so you can initialize like this if you don't care about the number of decimal places (the scale):

Decimal price;
...
price = 0;

or like this if you want a specific number of decimal places (2 in this case):

Decimal price;
...
price = 0.00;

As Rahul suggests, your problem most likely lies in other code.

2
  • it is working good for string (lookup field), but for number, constructor value is stored in database. I tried NOT assigning any value in constructor, in that case, NO child object is created
    – Tanmoy Das
    Commented Feb 11, 2017 at 16:15
  • @tanmoy Suggest you compare the code (that you have not posted) that touches the string properties with the code that touches the decimal properties and look for differences.
    – Keith C
    Commented Feb 12, 2017 at 11:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .