0

I have the next problem, when I get the output susc I get the next line:

12:59:48:144 USER_DEBUG [32]|DEBUG|Susc: DopplerListSubscriber__c:{Id=a0246000001nTMeAAM, Contact__c=00346000002O4YvAAK, DopplerList__c=a0046000001OvCGAA0}

The issue comes here, when I need to access this iterated List and read the fieldnames Contact__c without adding in an explicit way the fieldname to the susc.Contact__c call.

Here is the code:

Map<String, String> LookupFieldsMap = DopplerListCriteriasUtil.getAvailableLookupObjects();

// Build query with the availableLookups and Fields Mapping
List<DopplerListSubscriber__c> sobjList = Database.query(DopplerSQLBuilder.buildSOSQLDopplerListSubscriber(listId));

Map<String, String> dopplerWrapper = new Map<String, String>();

    for (DopplerListSubscriber__c susc : sobjList) {
        for (String loField : LookupFieldsMap.values()) {

            System.debug('Susc: ' + susc);
            System.debug('LookupFields: ' + loField);
        }
    }

How can I access the susc item List Array to get the fields without naming them, since if I do this I do see them:

String asd = susc.Contact__c;
  • susc.get(loField) perhaps? – sfdcfox Mar 23 '17 at 16:22
  • No, it gives me the value of the field: 00346000002O4YvAAK not the actual field name: Contact__c. I made the same test – BoDiE2003 Mar 23 '17 at 16:59
  • I need that susc fieldname to actually compare with loField and in case both fieldnames are the same then do the logic. – BoDiE2003 Mar 23 '17 at 17:00
  • I'm not sure I understand your question, then. – sfdcfox Mar 23 '17 at 17:08
  • 2
    susc.getPopulatedFieldsAsMap() then? – sfdcfox Mar 23 '17 at 17:19
1

Here is the way in case you don't trust Sobject class:-

Account acc = new Account(Name='Ashwani', AccountNumber='1234567');
String accString = JSON.serialize(acc);
Map<String,Object> attributes = (Map<String,Object>)JSON.deserializeUntyped(accString

for (DopplerListSubscriber__c susc : sobjList) 
{
     for (String loField : LookupFieldsMap.values()) 
     {

          Boolean shouldDoLogic = attributes.containsKey(loField);
          if(shouldDoLogic)
          {
                // Go Ahead
          }
     }
}

If you trust:

for (DopplerListSubscriber__c susc : sobjList) 
    {
         for (String loField : LookupFieldsMap.values()) 
         {

              Boolean shouldDoLogic = susc. getPopulatedFieldsAsMap.containsKey(loField);
              if(shouldDoLogic)
              {
                    // Go Ahead
              }
         }
    }

You can tweak the above logic to get the desired result.

| improve this answer | |
  • I dont need to put data, it has data, check the LOG. I need to grab the fieldname Contact__c from the Array inside the List. – BoDiE2003 Mar 23 '17 at 16:59
  • You can use susc.get(loField). If it is returning data so, it is an expected field else you will not get data. Possibly run into error. @BoDiE2003 – Ashwani Mar 23 '17 at 17:05

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.