0

I want to add actTotalAmount and purTotalAmount and show it on the visualforce page.

Right now I have another fuction to show actTotal and purTotal on visualforce page, but I couldn't find a way to add two values. so I had to come up with apex code...

<apex:variable var="actTotal" value="{!actTotal+activity.Unit_Price__c}"/>
<apex:variable var="purTotal" value="{!purTotal+purchase.Unit_Price__c}"/>

So I had to come up with apex code that calculates total. actTotalAmount and purTotalAmount are the variable that I get the total values. but having a problem adding them together and writing a test code....

public static List <Pricing_Lookup__c> getPurTotal(){
    //get activity total. 
    List<Pricing_Lookup__c> calculatePurTotal = [Select Name, (Select Name, Unit_Price__c From Purchase_Lookup__r) From Pricing_Lookup__c ORDER BY Name ASC];
        for ( Pricing_Lookup__c activityTotal : calculatePurTotal){
            Decimal  purTotalAmount = 0;
            for(Purchase_Lookup__c a :activityTotal.Purchase_Lookup__r ){
                system.debug(activityTotal);
                for(Purchase_Lookup__c lookupObj : activityTotal.Purchase_Lookup__r){
            purTotalAmount = purTotalAmount + lookupObj.Unit_Price__c;
                }
                system.debug(purTotalAmount);
            }
        }
return calculatePurTotal;
}


public void calculateTotal(){


    Integer total = actTotalAmount + purTotalAmount;


}

test code.....

static testMethod void validategetGrandTotal(){

    Pricing_Lookup__c pricingLookup = new Pricing_Lookup__c();
    pricingLookup.Name = 'pricingLookup';
    pricingLookup.Unit_Price__c = 100;
    insert pricingLookup;

    Id pId = pricingLookup.Id;

    Activity_Lookup__c activityLookup = new Activity_Lookup__c();
    activityLookup.Name = 'activityLookupName';
    activityLookup.Unit_Price__c = 99;
    activityLookup.Pricing_Lookup__c = pId;
    insert activityLookup;

    Activity_Lookup__c activityLookup2 = new Activity_Lookup__c();
    activityLookup2.Name = 'activityLookupName2';
    activityLookup2.Unit_Price__c = 98;
    activityLookup2.Pricing_Lookup__c = pId;
    insert activityLookup2;

    Activity_Lookup__c activityLookup3 = new Activity_Lookup__c();
    activityLookup3.Name = 'activityLookupName3';
    activityLookup3.Unit_Price__c = 97;
    activityLookup3.Pricing_Lookup__c = pId;
    insert activityLookup3;



    Purchase_Lookup__c purchaseLookup = new Purchase_Lookup__c();
    purchaseLookup.Name = 'testSample';
    purchaseLookup.Unit_Price__c = 100;
    purchaseLookup.Pricing_Lookup__c = pId;
    insert purchaseLookup;

    Integer actTotalAmount = 0;
    Integer purTotalAmount = 0;

    test.startTest();
    PackageSummaryController.getActTotal();
    system.debug(actTotalAmount);
    PackageSummaryController.getPurTotal();
    system.debug(purTotalAmount);
    test.stopTest();

Thank you in advance.

0

If you use static like this public static void calculateTotal(), you can directly call the method using PackageSummaryController.calculateTotal().

In the question above you are not using static, so the test can be like this instead:

PackageSummaryController ctrl = new PackageSummaryController();
ctrl.actTotalAmount =1 ;
ctrl.purTotalAmount =2 ;
ctrl.calculateTotal();
0

You don't need to use <apex:variable> to define values you can reference in your Visualforce page, and I don't think that's doing what you want here. Instead, just create accessors for the values in your Apex controller, and place the logic to derive those values within the get accessor.

For example, your controller can define the total property as follows:

Integer total { get { return actTotalAmount + purTotalAmount } }

Your Visualforce page can then refer to {! total } to bind to this value from the controller. Note, of course, that your controller must ensure the appropriate values of actTotalAmount and purTotalAmount are populated when the reference to {! total } is rendered in the page. You can populate them in your controller, or implement getter functions for those properties as well.

Your test class should ensure you appropriately cover this logic by accessing the value of this property and asserting that its value is what is expected based on the test data you insert.

// .... insert test data ....

myController c = new myController();
System.assertEquals(EXPECTED_TOTAL, c.total, 'total calculated 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.