0

I have a Map of IDs of a custom object (scopeAccountMap).

I am iterating through each object in scopeAccountMap and I am creating a map of the fields for each of these objects.

I am receiving the following error:

Comparison arguments must be compatible types: SObject, String

I am receiving this error because scope[s].getSObject(fieldNameString) is returning an object. I understand this, but do not know the correct solution.

What do I need to do so that I am able to compare and set the field of scope[s].fieldNameString where fieldNameString could be Name, Id, etc. depending on the fields returned in the scope list.

Scope is a list of Custom_Object__c records.

The use case for this functionality is that if blank fields are returned from an endpoint which we are getting the data from, I do not want the retrieved field data to overwrite the existing fields if there is data in the existing fields.

  for(integer s=0; s < scope.size(); s++) {

  existingCustObj = [SELECT Id, Name from Custom_Object__c];

  Map<ID, Custom_Object__c> scopeAccountMap = new Map<ID, Custom_Object_c>(scope);

  system.debug(scopeAccountMap.keyset());

    for (ID idKey : scopeAccountMap.keyset()) {
        Custom_Object__c a = scopeAccountMap.get(idKey);
        Map<String, Object> fieldsToValue = a.getPopulatedFieldsAsMap();

    for (String fieldName : fieldsToValue.keySet()) {
        String fieldNameString = String.valueOf(fieldsToValue.get(fieldName));

        //BELOW IS WHERE I NEED TO DYNAMICALLY REFERENCE THE OBJECTS FIELDS********
        if (scope[s].getSObject(fieldNameString) == '' || scope[s].getSObject(fieldNameString) == null) {
        scope[s].getSObject(fieldNameString) = existingCustObj[0].getSObject(fieldNameString);    
        }

        else {
            system.debug('scope value is not blank');
        }

        System.debug('field name is ' + fieldName + ', value is ' + fieldsToValue.get(fieldName));
        }
    }

EDIT:

Upon modifying the code from .getSObject to .get I now receive the following error:

Expression cannot be assigned

This error is referencing the line where I assign scope[s] to existingCustObj[0].

  • 1
    As a side note that both answers didn't say, assignment is done with "put", not that record.get(field) = notation you used. The correct form is record.put(field, value). Also, to be pedantic, getSObject is returning an sObject, not a generic Object. They are two different things. – sfdcfox Jun 6 '18 at 17:25
  • Yep, this is it! I was switching between .get and .getSobject not really understanding the difference between the two and reading some other threads though .getSobject was applicable, which it wasn't, but the assignment part closed the case. – S.B. Jun 6 '18 at 17:34
2

getSobject() is the generic sObject method for obtaining an sObject instance value for a given relationship field name. Since you're simply mapping field values, what you need is get(), which returns the primitive value.

Here's an illustrative example to make the distinction a little clearer.

List<Contact> children = [SELECT Account.Name, Id, FirstName, LastName FROM Contact];

Id accountId = (Id)children[0].get('AccountId');
Account a = (Account)children[0].getSobject('Account');
String accountName = a.get('Name');
String fn = (String)children[0].get('FirstName');

Note that for the primitive values (String, and Id for a relationship field), we use get(). getSobject() is used when we want to work with an sObject instance for the relationship field in Apex, which you don't need here.

| improve this answer | |
  • Please see the edit, upon changing the method to. get I receive a new error. – S.B. Jun 6 '18 at 17:27
  • Please review sfdcfox's comment. – David Reed Jun 6 '18 at 17:30
2

To get the value from a field, use get. The getSObject method is to get an entire parent record, and so should only be used with the relationship name for a Lookup or Master-Detail type field.

Object value = scope[s].get(fieldName);
if (value != null && value != '')
{
    // do stuff
}
| improve this answer | |
  • value != '' isn't necessary. – sfdcfox Jun 6 '18 at 17:20
  • I'm just copying the OP's logic. While you can't query an empty string from the database, it is unclear if the records have been manipulated since they were queried. – Adrian Larson Jun 6 '18 at 17:21
  • Please see the edit, upon changing the method to. get I receive a new error. – S.B. Jun 6 '18 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.