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
nbknbk 

System.TypeException: Data type not supported in test class

Hello Everyone,
I am getting System.TypeException: Data type not supported in API 30.0 versions while running the same class is working in previous versions. Below are the sample class.
Class:
global class SampleUtility
{

public static Map<String , Boolean> searchMap = new Map<String , Boolean>() ;
  
    public String createDynamicQuery(Set<String> selectClaseFields, SObject sObjectRecord)
        {
          
            Schema.SObjectType systemObjectType ;
            ////System.debug('sObjectRecord :::::: ' + sObjectRecord) ;
            if(sObjectRecord != null)
                {
                    systemObjectType = sObjectRecord.getSObjectType();
                    Schema.DescribeSObjectResult r = systemObjectType.getDescribe();

                    String objectName = r.getLocalName();
                    if(objectName != null && objectName  != '' && selectClaseFields != null && selectClaseFields.size() > 0)
                        {
                            //get operator for fields which have value in sObject
                            //Schema.DescribeSObjectResult r = systemObjectType.getDescribe();
                            Map<String,Schema.SObjectField> M = r.fields.getMap();
                            Map<String,DisplayType> fieldMap = new Map<String,DisplayType>();
                          
                            Set<String> existingFieldNamesList = M.keySet();
          
                            //Remove tag fields from fields name
                            for(String fieldName : existingFieldNamesList)
                                {
                                    //String str = String.valueOf(sObjectRecord.get(fieldname));
                                    //if(str != null && str != '' && str != '[]')
                                    if(sObjectRecord.get(fieldname)!='[]' && sObjectRecord.get(fieldname)!=null && sObjectRecord.get(fieldname)!='') //throwing error
                                        {
                                            searchMap.put(objectName, true) ;
                                            Schema.SObjectField field = M.get(fieldName);
                                            Schema.DescribeFieldResult fieldDesc = field.getDescribe();
                                            fieldMap.put(fieldName, fieldDesc.getType());
                                            //Base_AC_Utility.selectedFieldLabel += fieldDesc.getLabel() + '-' + sObjectRecord.get(fieldname) + ' ,' ;
                                        }
                                }
                           }
                   }return null;
                   }       
}

Test Class
@isTest
public class Test_Utility
{
  public static testMethod void unittest1()
  {
      Account acc = new Account(name='testaccount');
      insert acc;
      Contact contact =  new Contact(accountid=acc.id,LastName='Test');
                
                  insert contact;
                
     SampleUtility controller= new SampleUtility();
      Set<String> selectClaseFields = new Set<String>{'Id','Name'};
      String actualQuery1 =  controller.createDynamicQuery(selectClaseFields,contact);
    
  }
}

Please let me know if you have any workaround for this issue.
Best Answer chosen by nbk
nbknbk
Thanks for the update. I tried wtih (string) sObjectRecord.get(fieldname)!='[]' but even though the issue exists. Alternatively I solved the issue below.

If we down the API version from 30.0 the test class will not throw any error, but when I upload the class from package org it uploaded to APP Exhange and the components run on latest salesforce api version (30.0).

Root cause of the issue-The Type cast issue is related with native fields with Address (otheraddress,mailingaddress,billingaddress) data type. Currently I have not included address type fields to the method and able to solve the issue.

Class:Base_AC_Utility
public String createDynamicQuery2(Set<String> selectClaseFields, SObject sObjectRecord)
{
if(objectName != null && objectName != '' && selectClaseFields != null && selectClaseFields.size() > 0)
{
//get operator for fields which have value in sObject
//Schema.DescribeSObjectResult r = systemObjectType.getDescribe();
Map<String,Schema.SObjectField> M = r.fields.getMap();
Map<String,DisplayType> fieldMap = new Map<String,DisplayType>();
Map<String , Base_Enum_CVMOpearatorsType> fieldWithOperator = new Map<String , Base_Enum_CVMOpearatorsType>() ;
Set<String> existingFieldNamesList = M.keySet();
existingFieldNamesList.remove('otheraddress'); //added this statement to overcome the issue
existingFieldNamesList.remove('mailingaddress');//added this statement to overcome the issue


}

As per my observations the upload package components from package org run always on latest salesforce API version (30.0).

As per the analysis the issue seems to be platform level update in API version 30.0 on Data type conversion and previous versions the existing code working fine.



All Answers

Eli Flores, SFDC DevEli Flores, SFDC Dev
You can set the API version for the class by going into the UI for the class, click edit, click on version settings, and then changing the Salesforce.com API version. Make it match the api version on your class.

That being said i think the issue if you need to cast the result of .get(fieldname) so
sObjectRecord.get(fieldname)!='[]'
should be
(string) sObjectRecord.get(fieldname)!='[]'
nbknbk
Thanks for the update. I tried wtih (string) sObjectRecord.get(fieldname)!='[]' but even though the issue exists. Alternatively I solved the issue below.

If we down the API version from 30.0 the test class will not throw any error, but when I upload the class from package org it uploaded to APP Exhange and the components run on latest salesforce api version (30.0).

Root cause of the issue-The Type cast issue is related with native fields with Address (otheraddress,mailingaddress,billingaddress) data type. Currently I have not included address type fields to the method and able to solve the issue.

Class:Base_AC_Utility
public String createDynamicQuery2(Set<String> selectClaseFields, SObject sObjectRecord)
{
if(objectName != null && objectName != '' && selectClaseFields != null && selectClaseFields.size() > 0)
{
//get operator for fields which have value in sObject
//Schema.DescribeSObjectResult r = systemObjectType.getDescribe();
Map<String,Schema.SObjectField> M = r.fields.getMap();
Map<String,DisplayType> fieldMap = new Map<String,DisplayType>();
Map<String , Base_Enum_CVMOpearatorsType> fieldWithOperator = new Map<String , Base_Enum_CVMOpearatorsType>() ;
Set<String> existingFieldNamesList = M.keySet();
existingFieldNamesList.remove('otheraddress'); //added this statement to overcome the issue
existingFieldNamesList.remove('mailingaddress');//added this statement to overcome the issue


}

As per my observations the upload package components from package org run always on latest salesforce API version (30.0).

As per the analysis the issue seems to be platform level update in API version 30.0 on Data type conversion and previous versions the existing code working fine.



This was selected as the best answer