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
Rahul Sharma 390Rahul Sharma 390 

how to get json of inserted record in test class

Class----------->
public class InvestmentSettingAura {
    
    @auraEnabled
    public static Investment_Settings__c getInvSetting(){
        
        List<Investment_Settings__c> InvSettingList = [SELECT Id,AutoBid__c,Investor__c,Max_AutoBid_Percentage__c,Maximum_exposure_to_1_Seller__c,
                                                       Maximum_Auto_Bid_Amount__c,Maximum_exposure_to_1_Customer__c,
                                                       Maximum_Credit_Grade_for_AutoBids__c
                                                       FROM Investment_Settings__c
                                                       WHERE Investor__c =:UserInfo.getUserId()];
        
        if(InvSettingList.size()>0){
            Investment_Settings__c invSet =new Investment_Settings__c();
            invSet = InvSettingList.get(0);
            return invSet;
        }
        else
            return null;
    }    
    
    @AuraEnabled 
    public static List<String>  autoBidGrade(){
        List<String> options = new List<String>();
        Schema.DescribeFieldResult fieldResult = Investment_Settings__c.Maximum_Credit_Grade_for_AutoBids__c.getDescribe();
        for (Schema.PicklistEntry f : fieldResult.getPicklistValues())
            options.add(f.getLabel());
        return options;
    }
    
    @AuraEnabled 
    public static string  saveInvestmentSettings(String invObj){
        Investment_Settings__c invSet =new Investment_Settings__c();
        invSet = (Investment_Settings__c)JSON.deserialize(invObj, Investment_Settings__c.class);
        //invSet = invObj;
        SavePoint sp = Database.setSavepoint();           //[is covering in test class]
        
        try{
            update invSet;
            return 'Success';
        }
        catch(Exception e){
            Database.rollback(sp);
            return 'Error : '+ e.getMessage();
        }
    }
}

Test Class ------------>

@isTest
public class InvestmentSettingAuraTest {  
     public static testMethod void testInvestmentSetting1(){
          Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
        user u = [select id from user where name='TMC Admin'];
         
        Investment_Settings__c invset = new Investment_Settings__c();
        invset.CurrencyIsoCode = 'SGD';
        invset.AutoBid__c = true;
        invset.Investor__c = u.id;
        insert invset;
        Investment_Settings__c invset1 = new Investment_Settings__c();
         invset1.Id= invset.Id;
        invset.CurrencyIsoCode = 'SGD';
        invset.AutoBid__c = true;
        invset.Investor__c = u.id;
        update invset;
        
        InvestmentSettingAura.getInvSetting();
        InvestmentSettingAura.autoBidGrade();
        InvestmentSettingAura.saveInvestmentSettings('invObj');
    }
}

Suraj TripathiSuraj Tripathi

Hi Rahul Sharma,

Please Use this line for you JSON data. hope it will help you.

Exchange this line:

InvestmentSettingAura.saveInvestmentSettings('invObj');


With this:

String myJSON = JSON.serialize(invset);
InvestmentSettingAura.saveInvestmentSettings(myJSON);

Mark as a best if it helps you.

Regards,

Suraj

Rahul Sharma 390Rahul Sharma 390

Hello Suraj ,

THnx For helping me out .

Could you please suggest me , how to cover the catch in this class ?

Suraj TripathiSuraj Tripathi

Hi Rahul,

You have to throw an exceptions data. For this you can skip that part it is almost covering enough. you can skip this.

Mark that best answer to help out solving questions.

Regards,

Suraj

Ginny MahantGinny Mahant
Hi Rahul,
   
Do you mean json of inserted or updated record in test class? Assuming you mean the latter, perhaps in your lightning controller you could check "isTestRunning()==True" after update invSet; and return a String containing the serialized object record that you have updated. In your test method you can de-serialize it and you should have your updated object record in testclass.

Hope this helps. 

the last line of code is a bit confusing ..InvestmentSettingAura.saveInvestmentSettings('invObj'); care to explain what this is ??