function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jasril Dane CaliwagJasril Dane Caliwag 

hi all, Can anyone help me in writting the test class for the below class.

public class AddPriceModalController {
    @AuraEnabled
    public static List<String> getRegionValues() {
        List<String> lstGeography = new List<String>(); //new list for holding all of the picklist options
        
        Schema.DescribeFieldResult fieldResult = PriceDetail__c.Geography__c.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        
        for( Schema.PicklistEntry f : ple)
        {
            lstGeography.add(f.getValue());
        }
        
        return lstGeography;
    }
    
    @AuraEnabled
    public static List<String> getDeliveryMethodValues() {
        List<String> lstSourceOfSupply = new List<String>(); //new list for holding all of the picklist options
        
        Schema.DescribeFieldResult fieldResult = PriceDetail__c.Source_of_Supply__c.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        
        for( Schema.PicklistEntry f : ple)
        {
            lstSourceOfSupply.add(f.getLabel() + '---' + f.getValue());            
        }
        
        return lstSourceOfSupply;
    }
    //Method to Save the Price Detail Change Request
    @AuraEnabled
    public static String savePriceDetails(Id PDID,PriceDetail__c newPriceDetails) {
        system.debug('savePriceDetails');
        system.debug('newPriceDetails'+newPriceDetails);
        Decimal invoiceCost = 0;
        Decimal netCost = 0;
        Decimal netProfit = 0;
        Decimal retailPrice = 0;
        string resultId;
        //Webservice Call to SAP to get the InvoiceCost and NetCost
        /*invoiceCost = PriceDetailsWebService.getInvoiceCost(PDID);
netCost = PriceDetailsWebService.getNetCost(PDID);*/
        
        String PDSOQL = SObjectUtil.getSOQLStringFullFields(PriceDetail__c.getSObjectType())
            +' WHERE ' + ' Id = :PDID LIMIT 1';
        //List<PriceDetail__c> listPD = Database.query(PDSOQL);
        PriceDetail__c PDRec;
        try{
            PDRec = Database.query(PDSOQL);   
        }catch(Exception e){
            ApplicationLogUtility.logError('SubmitActionController', 'getParentId', e, e.getMessage(), '', 0);
            ApplicationLogUtility.commitLog();
            PDRec = null; 
        }   
        if(PDRec!=null){
            PriceDetail__c clonedPD = PDRec.clone(false, true, false, false);
            clonedPD.InvoiceCost__c = invoiceCost;
            clonedPD.NetCost__c = netCost;
            clonedPD.ClonedFrom__c = PDRec.Id;
            clonedPD.Status__c = Label.Draft;
            clonedPD.RecordTypeId = Schema.SObjectType.PriceDetail__c.getRecordTypeInfosByName().get(Label.PriceDetail_Submit).getRecordTypeId();
            Schema.DescribeSObjectResult objResult = PriceDetail__c.sObjectType.getDescribe();
            for(string apiName : objResult.fields.getMap().keySet()){
                String feildName = apiName;
                if(feildName.endsWith('__c')){
                    if((clonedPD.get(apiName) != newPriceDetails.get(apiName)) && (feildName!= 'ClonedFrom__c')){
                        system.debug('feildName***'+feildName);
                        clonedPD.put(apiName, newPriceDetails.get(apiName));
                    }
                }
            }
            system.debug('clonedPD**'+clonedPD);
            if(clonedPD!=null){
                insert clonedPD;    
            }
            
            resultId = Id.valueOf(clonedPD.Id);
            system.debug('resultId'+resultId);
            
        }
        
        return resultId;
    }
    
    //Method to get the exsisting data from the record
    @AuraEnabled
    public static PriceDetail__c getPriceDetails(Id recordId) {
        system.debug('recordId**'+recordId);
        PriceDetail__c pdRecord = (PriceDetail__c) SObjectUtil.getFullSObject(PriceDetail__c.getSObjectType(), recordId);
        return pdRecord;
        
    }
    
    
    
    
}
Steven NsubugaSteven Nsubuga
@isTest
private class AddPriceModalControllerTest {

	@testSetup 
	static void createData() {
		PriceDetail__c pD = PDRec.clone(false, true, false, false);
            pD.InvoiceCost__c = 999;
            pD.NetCost__c = 577;
            pD.Status__c = Label.Draft;
            pD.RecordTypeId = Schema.SObjectType.PriceDetail__c.getRecordTypeInfosByName().get(Label.PriceDetail_Submit).getRecordTypeId();
			insert pD;
	}
		
	@isTest static void getRegionValuesTest(){
		System.assertNotEquals(0, AddPriceModalController.getRegionValues());
	}
	
	@isTest static void getDeliveryMethodValuesTest(){
		System.assertNotEquals(0, AddPriceModalController.getDeliveryMethodValues());
	}
	
	@isTest static void savePriceDetailsTest(){
		List<PriceDetail__c> pD = [Select Id, InvoiceCost__c, NetCost__c, RecordTypeId  FROM PriceDetail__c];
		System.assertNotEquals(null, AddPriceModalController.savePriceDetails(pD[0].Id, pD[0]));
	}
	
	@isTest static void getgetPriceDetailsTest(){
		List<PriceDetail__c> pD = [Select Id, InvoiceCost__c, NetCost__c, RecordTypeId  FROM PriceDetail__c];
		System.assertNotEquals(null, AddPriceModalController.getPriceDetails(pD[0].Id));
	}
}

 
Jasril Dane CaliwagJasril Dane Caliwag
hello steven Nsubuga, thanks for the code but im getting an error in
line 6, (Variable does not exist: PDRec)
line 17, (Comparison arguments must be compatible types: Integer, List<String>)
and line 21, (Comparison arguments must be compatible types: Integer, List<String>)
Steven NsubugaSteven Nsubuga
@isTest
private class AddPriceModalControllerTest {

	@testSetup 
	static void createData() {
		PriceDetail__c pD = new PriceDetail__c();
            pD.InvoiceCost__c = 999;
            pD.NetCost__c = 577;
            pD.Status__c = Label.Draft;
            pD.RecordTypeId = Schema.SObjectType.PriceDetail__c.getRecordTypeInfosByName().get(Label.PriceDetail_Submit).getRecordTypeId();
			insert pD;
	}
		
	@isTest static void getRegionValuesTest(){
		System.assertNotEquals(0, AddPriceModalController.getRegionValues().size());
	}
	
	@isTest static void getDeliveryMethodValuesTest(){
		System.assertNotEquals(0, AddPriceModalController.getDeliveryMethodValues().size());
	}
	
	@isTest static void savePriceDetailsTest(){
		List<PriceDetail__c> pD = [Select Id, InvoiceCost__c, NetCost__c, RecordTypeId  FROM PriceDetail__c];
		System.assertNotEquals(null, AddPriceModalController.savePriceDetails(pD[0].Id, pD[0]));
	}
	
	@isTest static void getgetPriceDetailsTest(){
		List<PriceDetail__c> pD = [Select Id, InvoiceCost__c, NetCost__c, RecordTypeId  FROM PriceDetail__c];
		System.assertNotEquals(null, AddPriceModalController.getPriceDetails(pD[0].Id));
	}
}