You need to sign in to do that
Don't have an account?
JohnDurai
Test class for Custom component controller class
Hi All - Below is the controller class for my custom component, can someone help me with test class for this controller. Thanks!
public inherited sharing class CFRNotesControllerPOC {
public class CFRNotesControllerException extends Exception {}
@AuraEnabled(cacheable=true)
public static NotesFieldWrapper getFieldInfo(String recordId, String fieldName){
return new NotesFieldWrapper(recordId, fieldName);
}
@AuraEnabled
public static void saveNotesInfo(String recordId, String fieldName, String fieldValue){
SObject sobjectRecord = ((Id)recordId).getSObjectType().newSObject(recordId);
sobjectRecord.put('Id', recordId);
sobjectRecord.put(fieldName, fieldValue);
Database.update(sobjectRecord);
}
public class NotesFieldWrapper {
@auraEnabled
public String fieldValue;
@AuraEnabled
public String fieldDescribe;
//to check FLS
@AuraEnabled
public Boolean fieldAccess;
public NotesFieldWrapper(String recordId, String fieldName) {
Schema.DescribeSObjectResult objDescribe = ((Id)recordId).getSObjectType().getDescribe();
String objName = objDescribe.getName();
Map<String, Schema.SObjectField> fields = objDescribe.fields.getMap();
if(String.isNotEmpty(fieldName)){
Schema.SObjectField fieldToken = fields.get(fieldName);
this.fieldDescribe = JSON.serialize(fieldToken.getDescribe());
this.fieldAccess = fieldToken.getDescribe().isAccessible();
String query = 'SELECT Id, ' + fieldName + ' FROM ' + objName + ' WHERE Id = \'' + recordId + '\'';
SObject sobj = (SObject) Database.query(query);
if(sobj != null){
this.fieldValue = (String) sobj.get(fieldName);
}
}
}
}
public inherited sharing class CFRNotesControllerPOC {
public class CFRNotesControllerException extends Exception {}
@AuraEnabled(cacheable=true)
public static NotesFieldWrapper getFieldInfo(String recordId, String fieldName){
return new NotesFieldWrapper(recordId, fieldName);
}
@AuraEnabled
public static void saveNotesInfo(String recordId, String fieldName, String fieldValue){
SObject sobjectRecord = ((Id)recordId).getSObjectType().newSObject(recordId);
sobjectRecord.put('Id', recordId);
sobjectRecord.put(fieldName, fieldValue);
Database.update(sobjectRecord);
}
public class NotesFieldWrapper {
@auraEnabled
public String fieldValue;
@AuraEnabled
public String fieldDescribe;
//to check FLS
@AuraEnabled
public Boolean fieldAccess;
public NotesFieldWrapper(String recordId, String fieldName) {
Schema.DescribeSObjectResult objDescribe = ((Id)recordId).getSObjectType().getDescribe();
String objName = objDescribe.getName();
Map<String, Schema.SObjectField> fields = objDescribe.fields.getMap();
if(String.isNotEmpty(fieldName)){
Schema.SObjectField fieldToken = fields.get(fieldName);
this.fieldDescribe = JSON.serialize(fieldToken.getDescribe());
this.fieldAccess = fieldToken.getDescribe().isAccessible();
String query = 'SELECT Id, ' + fieldName + ' FROM ' + objName + ' WHERE Id = \'' + recordId + '\'';
SObject sobj = (SObject) Database.query(query);
if(sobj != null){
this.fieldValue = (String) sobj.get(fieldName);
}
}
}
}
Let me know if this helps, if it does, please mark this answer as best so that others facing the same issue will find this information useful. Thank you