You need to sign in to do that
Don't have an account?
Sailaja N 4
Field is not writeable: OpportunityLineItem.Total_w_Tax__c
OpportunityLineItem.Total_w_Tax__c -this is formula field
getting above error in test class
please see the test class below
@isTest
private class ItemizedReceiptTest {
static testMethod void testMethodPostive(){
Product2 prod = new Product2(
Name = 'Test',
IsActive = true
);
insert prod;
Account acc = new Account(Name ='Test');
insert acc;
Opportunity testOpp = new Opportunity();
testOpp.Name='Test Opportunity' ;
testOpp.AccountId=acc.id;
testOpp.StageName='Order Placed';
testOpp.CloseDate= Date.newInstance(2019,03,28);
insert testOpp;
Id pricebookId = Test.getStandardPricebookId();
//Create your pricebook entry
PricebookEntry pbEntry = new PricebookEntry(
Pricebook2Id = pricebookId,
Product2Id = prod.Id,
UnitPrice = 100.00,
IsActive = true
);
insert pbEntry;
//create your opportunity line item. This assumes you already have an opportunity created, called opp
OpportunityLineItem oli = new OpportunityLineItem(
OpportunityId = testOpp.Id,
Quantity = 5,
PricebookEntryId = pbEntry.Id,
TotalPrice = pbEntry.UnitPrice,
GST__c = 7,
PST__c = 7,
Total_w_Tax__c = 8 // getting error here
);
insert oli;
ApexPages.StandardController stdSetController = new ApexPages.StandardController(testOpp);
ItemizedReceipt obj = new ItemizedReceipt(stdSetController);
obj.total();
}
}
class
public with sharing class ItemizedReceipt {
public List<OpportunityLineItem> oppLst { set; get; } //List of Products in a Opportunity
public Double totalPrice_cal { set; get; } //Variable to calculate Total Price of all the products
public Double totalGST_cal { set; get; } //Variable to calculate GST of all the products
public Double totalPST_cal { set; get; } //Variable to calculate PST of all the products
public Double totalAmt_cal { set; get; } //Variable to calculate Total Amount of all the products
public DateTime getCloseDate { set; get; } //Variable to get the Created Date
public String dayOfWeek { set; get; } //Variable to store the Day from the Created Date
public String accName { set; get; } //Variable to store the Customer Name
public ItemizedReceipt(ApexPages.StandardController controller) {
if ( controller.getId() != null ) {
oppLst = [SELECT Product2.Name, Opportunity.Id, Opportunity.CloseDate, TotalPrice, GST__c, PST__c,
Total_w_Tax__c, Opportunity.Account.Name
FROM OpportunityLineItem
WHERE Opportunity.Id=:controller.getId()
];
getCloseDate = oppLst[0].Opportunity.CloseDate;
dayOfWeek = getCloseDate.format('EEEE');
accName = (string)oppLst[0].Opportunity.Account.Name;
totalPrice_cal = 0.0;
totalGST_cal = 0.0;
totalPST_cal = 0.0;
totalAmt_cal = 0.0;
total();
}
}
//Method calculates all the totals
public void total() {
for ( integer i = 0; i < oppLst.size(); i++ ) {
totalPrice_cal += oppLst[i].TotalPrice;
totalGST_cal += oppLst[i].GST__c;
totalPST_cal += oppLst[i].PST__c;
totalAmt_cal += oppLst[i].Total_w_Tax__c;
}
}
}
getting above error in test class
please see the test class below
@isTest
private class ItemizedReceiptTest {
static testMethod void testMethodPostive(){
Product2 prod = new Product2(
Name = 'Test',
IsActive = true
);
insert prod;
Account acc = new Account(Name ='Test');
insert acc;
Opportunity testOpp = new Opportunity();
testOpp.Name='Test Opportunity' ;
testOpp.AccountId=acc.id;
testOpp.StageName='Order Placed';
testOpp.CloseDate= Date.newInstance(2019,03,28);
insert testOpp;
Id pricebookId = Test.getStandardPricebookId();
//Create your pricebook entry
PricebookEntry pbEntry = new PricebookEntry(
Pricebook2Id = pricebookId,
Product2Id = prod.Id,
UnitPrice = 100.00,
IsActive = true
);
insert pbEntry;
//create your opportunity line item. This assumes you already have an opportunity created, called opp
OpportunityLineItem oli = new OpportunityLineItem(
OpportunityId = testOpp.Id,
Quantity = 5,
PricebookEntryId = pbEntry.Id,
TotalPrice = pbEntry.UnitPrice,
GST__c = 7,
PST__c = 7,
Total_w_Tax__c = 8 // getting error here
);
insert oli;
ApexPages.StandardController stdSetController = new ApexPages.StandardController(testOpp);
ItemizedReceipt obj = new ItemizedReceipt(stdSetController);
obj.total();
}
}
class
public with sharing class ItemizedReceipt {
public List<OpportunityLineItem> oppLst { set; get; } //List of Products in a Opportunity
public Double totalPrice_cal { set; get; } //Variable to calculate Total Price of all the products
public Double totalGST_cal { set; get; } //Variable to calculate GST of all the products
public Double totalPST_cal { set; get; } //Variable to calculate PST of all the products
public Double totalAmt_cal { set; get; } //Variable to calculate Total Amount of all the products
public DateTime getCloseDate { set; get; } //Variable to get the Created Date
public String dayOfWeek { set; get; } //Variable to store the Day from the Created Date
public String accName { set; get; } //Variable to store the Customer Name
public ItemizedReceipt(ApexPages.StandardController controller) {
if ( controller.getId() != null ) {
oppLst = [SELECT Product2.Name, Opportunity.Id, Opportunity.CloseDate, TotalPrice, GST__c, PST__c,
Total_w_Tax__c, Opportunity.Account.Name
FROM OpportunityLineItem
WHERE Opportunity.Id=:controller.getId()
];
getCloseDate = oppLst[0].Opportunity.CloseDate;
dayOfWeek = getCloseDate.format('EEEE');
accName = (string)oppLst[0].Opportunity.Account.Name;
totalPrice_cal = 0.0;
totalGST_cal = 0.0;
totalPST_cal = 0.0;
totalAmt_cal = 0.0;
total();
}
}
//Method calculates all the totals
public void total() {
for ( integer i = 0; i < oppLst.size(); i++ ) {
totalPrice_cal += oppLst[i].TotalPrice;
totalGST_cal += oppLst[i].GST__c;
totalPST_cal += oppLst[i].PST__c;
totalAmt_cal += oppLst[i].Total_w_Tax__c;
}
}
}
It might be calculating the data from other objects(Rollup summary field) so please remove that field and try to insert the data.Let me know if you are still getting any errors then i am very happy to help you
Thanks&Regards
Brahmanaidu
Tha nk you for your reply
if i remove that field getting below error
System.NullPointerException: Argument cannot be null.
error in below lines in class:
total();
totalGST_cal += oppLst[i].GST__c;
error in below lines inTest class:
ItemizedReceipt obj = new ItemizedReceipt(stdSetController);
You cannot perform write action on formula field. You can only read them, thats why the error. You are performing write opertaion on it.
Please remove it.
And don't call the total() method directly from test class. That method will automatically get called when you call the ItemizedReceipt() constructor, and at that time the formula field have it value populated.
Hope this helps!
i removed formua fields in test class, also removed total() method from test class.
still it is showing null exception
Please check the mandatory fields.
Thanks&Regards
Brahmanaidu
https://sfdcscenarios.blogspot.com/