3

I am trying to create a test class for below class. I have to create an Sobject object and call the function by passing sibject in parameter. How can I write a complete test class for below class:

I tried SObject so=new Sobject(); // But this seem to be wrong way of initializing it. Any suuggestion.

global with sharing class populateFieldService
{
    global static void populateByParameter(Map<String,String> fMap, SObject so)
    {

         Map<String,Schema.sObjectField> fieldMap=so.getSObjectType().getDescribe().fields.getMap();
         for(String key: fMap.KeySet())
         {
             if(key == 'RecordTypeName')
             {
                 try
                 {
                     so.put('RecordTypeId',so.getSObjectType().getDescribe().getRecordTypeInfosByName().get(fMap.get(key)).getRecordTypeId());
                 }
                 catch(Exception e)
                {
                    continue;
                }
             }
             else if(key != 'Id' && key !='RecordType' && fieldMap.get(key.toLowerCase()) != NULL && fieldMap.get(key.toLowerCase()).getDescribe().isUpdateable())
             {
                try
                {
                    String fieldType=String.ValueOf(fieldMap.get(key.toLowerCase()).getDescribe().getType()).toLowerCase();
                    if(fieldType == 'currency' || fieldType == 'double' || fieldType == 'percent' || fieldType == 'decimal')
                    {
                        so.put(key, double.valueOf(fMap.get(key))); 
                    }
                    else if(fieldType == 'boolean')
                    {
                        so.put(key, Boolean.valueOf(fMap.get(key))); 
                    }                   
                    else if(fieldType == 'date')
                    {
                        Date d=date.parse(fMap.get(key));
                        so.put(key,d ); 
                    }  
                    else If(fieldType == 'datetime')   
                    {
                        DateTime dt=DateTime.parse(fMap.get(key));
                        so.put(key,dt);
                    }           
                    else
                    {
                        so.put(key,fMap.get(key));
                    }
                }
                catch(Exception e)
                {
                    continue;
                }
             }
         }
    }
}

I tried the below piece of code to cover it but some of the lines still shows not covered.. Any idea what fields I should fill in for this?

@isTest
private class populateFieldServiceTest{

    @isTest
    static void rpopulateByParameterTest(){

    Map<String,String> fMap = new Map<String,String>();
    Account record = new Account();
    Map<String, String> fieldToValue = new Map<String, String> { 'Name' => 'ACME' };
    PopulateFieldService.populateByParameter(fieldToValue, record);

    }


}

enter image description here

4
  • Did you write this class?
    – Adrian Larson
    Commented Feb 9, 2017 at 21:25
  • No Actually :) Trying to test it though. Why do you ask?
    – SfdcBat
    Commented Feb 9, 2017 at 21:26
  • 4
    All those "toLowerCase" calls are unnecessary, because the field describe keys are case insensitive, and == is also case insensitive.
    – sfdcfox
    Commented Feb 9, 2017 at 21:29
  • Can you try this? Schema.SObjectType targetType = Schema.getGlobalDescribe().get(ObjectApi_name);// Sobject Object_name = targetType.newSObject(); Commented Feb 9, 2017 at 21:29

2 Answers 2

3

You can instantiate any specific type of object, but not a generic one. For instance you could do:

Account record = new Account();
Map<String, String> fieldToValue = new Map<String, String> { 'Name' => 'ACME' };
PopulateFieldService.populateByParameter(fieldToValue, record);

If you want full coverage you need to select a field of each type. You also need to provide a 'RecordTypeName' key.

fieldToValue.put('RecordTypeName', 'Some existing RecordType.Name...');
fieldToValue.put('SomeDateField__c', Date.today());
fieldToValue.put('SomeCurrencyField__c, 101);
// etc...

I will refrain from offering too much feedback about the functionality itself but...I would avoid using it personally.

3
  • Thanks. It covered couple of lines. Can you tell me how can I cover other lines. I believe I should provide some value for fieldtype some where. Pls see my edit.
    – SfdcBat
    Commented Feb 9, 2017 at 21:40
  • You need to provide more fields of the various types...
    – Adrian Larson
    Commented Feb 9, 2017 at 21:57
  • Where do I provide that ? Is it at 'Name'=> 'ACME'? How do I select field of each type and provide RecordTypeName. Sorry not familiar with the syntax for this?
    – SfdcBat
    Commented Feb 9, 2017 at 22:29
9

While you cannot instantiate an non-typed sObject, you can create it generically.

First, get the sObject type. For example, for "Account", I would do the following:

Schema.SObjectType accountType = Schema.getGlobalDescribe().get('Account');

Next, use the newSObject() method, as documented here.

SObject newAccount = accountType.newSObject();

I think Adrian's solution makes more sense here, but if you need to create an SObject generically, this will work.

2
  • Schema.getGlobalDescribe().get('Account');
    – SFDC
    Commented Oct 5, 2018 at 10:18
  • 1
    Corrected in the answer. Though looking at it now, I'd probably rather use SObject newAccount = Account.SObjectType.newSObject();
    – SFDC Neuf
    Commented Oct 5, 2018 at 20:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .