You need to sign in to do that
Don't have an account?
Sunnyalex
How to write test class for this apex class for lightning component
public class AllocationListController {
@AuraEnabled
public static Allocation__c getAllocation(Id Allocation) {
System.debug('Allocationid'+Allocation);
List<Allocation__c> AllocationList = [SELECT id,Name,Amount_Allocated__c,Amount_Pending_Allocation__c FROM Allocation__c where Id=:Allocation];
System.debug('AllocationList'+AllocationList);
return AllocationList [0];
}
@AuraEnabled
public static List<npsp__General_Accounting_Unit__c> getGau() {
List<npsp__General_Accounting_Unit__c> GauList = [SELECT Id,Name FROM npsp__General_Accounting_Unit__c];
System.debug('General Accounting Units'+GauList);
return GauList;
}
@AuraEnabled
public static string doAllocate(List<npsp__Allocation__c> lstSelectedRecords, Allocation__c allocationrecord){
//list<npsp__Allocation__c>= new list<npsp__Allocation__c>();
// List<npsp__Allocation__c> npspList = new List<npsp__Allocation__c>();
system.debug('lstSelectedRecords ' +lstSelectedRecords + 'allocationrecord ' +allocationrecord);
decimal total=0;
list<Fund_Allocation__c> lstFundAllocation= new list<Fund_Allocation__c>();
for(npsp__Allocation__c var: lstselectedrecords ){
lstFundAllocation.add(new Fund_Allocation__c(
Amount_Donated__c = var.Amount_Pending_Allocation__c,
GAU_Allocation__c = var.id,
Patient_Id__c = allocationrecord.id,
Name = allocationrecord.Name
) );
var.Amount_Allocated__c+=var.Amount_Pending_Allocation__c;
total+= var.Amount_Pending_Allocation__c;
}
if(!lstselectedrecords.isempty()){
update lstselectedrecords;
insert lstFundAllocation;
system.debug('total'+total);
if (total <> null){
allocationrecord.Amount_Allocated__c += total;
}
// allocationrecord.Amount_Allocated__c+=total;
//update allocationrecord;
try{
update allocationrecord;
}
catch (Exception e){
// throw new AuraHandledException('Amount Pending Allocation Should not be greater than Amount'+e.getMessage());
//throw new AuraHandledException( ' Amount Pending Allocation Should not be greater than Amount ==>');
throw new AuraHandledException(e.getMessage().substringBetween('FIELD_CUSTOM_VALIDATION_EXCEPTION, ',': ') );
}
}
return null;
}
@AuraEnabled
public static Fund_Allocation__c getfunding (Id funding) {
System.debug('fundingid'+funding);
List<Fund_Allocation__c> fundingList = [SELECT Id,Name,Amount_Required__c FROM Fund_Allocation__c where Id=:funding];
System.debug('fundingList'+fundingList);
return fundingList[0];
}
@AuraEnabled
public static List<npsp__Allocation__c> getgauallocations(String Gau) {
system.debug(Gau);
Gau= Gau.remove('.');
system.debug(Gau);
List<npsp__Allocation__c> allList = [SELECT Id,npsp__General_Accounting_Unit__c,Amount_Allocated__c,Donor_Name__c,
Name,Amount_Pending_Allocation__c,npsp__Amount__c
FROM npsp__Allocation__c
where Amount_Pending_Allocation__c>0
and npsp__General_Accounting_Unit__c =:Gau
ORDER BY
Amount_Pending_Allocation__c ASC];
system.debug(allList);
return allList;
}
}
@AuraEnabled
public static Allocation__c getAllocation(Id Allocation) {
System.debug('Allocationid'+Allocation);
List<Allocation__c> AllocationList = [SELECT id,Name,Amount_Allocated__c,Amount_Pending_Allocation__c FROM Allocation__c where Id=:Allocation];
System.debug('AllocationList'+AllocationList);
return AllocationList [0];
}
@AuraEnabled
public static List<npsp__General_Accounting_Unit__c> getGau() {
List<npsp__General_Accounting_Unit__c> GauList = [SELECT Id,Name FROM npsp__General_Accounting_Unit__c];
System.debug('General Accounting Units'+GauList);
return GauList;
}
@AuraEnabled
public static string doAllocate(List<npsp__Allocation__c> lstSelectedRecords, Allocation__c allocationrecord){
//list<npsp__Allocation__c>= new list<npsp__Allocation__c>();
// List<npsp__Allocation__c> npspList = new List<npsp__Allocation__c>();
system.debug('lstSelectedRecords ' +lstSelectedRecords + 'allocationrecord ' +allocationrecord);
decimal total=0;
list<Fund_Allocation__c> lstFundAllocation= new list<Fund_Allocation__c>();
for(npsp__Allocation__c var: lstselectedrecords ){
lstFundAllocation.add(new Fund_Allocation__c(
Amount_Donated__c = var.Amount_Pending_Allocation__c,
GAU_Allocation__c = var.id,
Patient_Id__c = allocationrecord.id,
Name = allocationrecord.Name
) );
var.Amount_Allocated__c+=var.Amount_Pending_Allocation__c;
total+= var.Amount_Pending_Allocation__c;
}
if(!lstselectedrecords.isempty()){
update lstselectedrecords;
insert lstFundAllocation;
system.debug('total'+total);
if (total <> null){
allocationrecord.Amount_Allocated__c += total;
}
// allocationrecord.Amount_Allocated__c+=total;
//update allocationrecord;
try{
update allocationrecord;
}
catch (Exception e){
// throw new AuraHandledException('Amount Pending Allocation Should not be greater than Amount'+e.getMessage());
//throw new AuraHandledException( ' Amount Pending Allocation Should not be greater than Amount ==>');
throw new AuraHandledException(e.getMessage().substringBetween('FIELD_CUSTOM_VALIDATION_EXCEPTION, ',': ') );
}
}
return null;
}
@AuraEnabled
public static Fund_Allocation__c getfunding (Id funding) {
System.debug('fundingid'+funding);
List<Fund_Allocation__c> fundingList = [SELECT Id,Name,Amount_Required__c FROM Fund_Allocation__c where Id=:funding];
System.debug('fundingList'+fundingList);
return fundingList[0];
}
@AuraEnabled
public static List<npsp__Allocation__c> getgauallocations(String Gau) {
system.debug(Gau);
Gau= Gau.remove('.');
system.debug(Gau);
List<npsp__Allocation__c> allList = [SELECT Id,npsp__General_Accounting_Unit__c,Amount_Allocated__c,Donor_Name__c,
Name,Amount_Pending_Allocation__c,npsp__Amount__c
FROM npsp__Allocation__c
where Amount_Pending_Allocation__c>0
and npsp__General_Accounting_Unit__c =:Gau
ORDER BY
Amount_Pending_Allocation__c ASC];
system.debug(allList);
return allList;
}
}
>> https://salesforce.stackexchange.com/questions/181448/salesforce-lightning-component-controller-apex-test-class/183588
The above has an implementation that you can try checking and you can use it to create a test class for your custom use case.
Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks.