1

I have a logic issue with the next code which I cannot manage to solve. I will receive a Map<String, List<Sobject>> recordsMap and I need to match the String which will be the object name and get the List and match it with the mapping fields

public List<SubsExtendedInfo> generateWrapperInfo (Map<String, List<Sobject>> recordsMap){
        //object Name >> records

        Map<String, SubscriberMapping__c> mappingFieldsMap = SubscriberMapping__c.getAll();

        // This mappingFieldsMap has the name of the object with the mapping fields
        // The obj name should match with the map string and then by its name get the equivalent mapping fields List<Sobject> - mappingFieldsMap

       // mappingFieldsMap.get(Key).SF_Object_API_Name__c
       // This is the object name field from the mappingFieldsMap
        return null;
}

I cannot figure out how to do it, could anyone help me?

SubscriberMapping__c Structure

Product Field Name || FieldName__c

Object API Name || SF_Object_API_Name__c

SF_Field_API_Name || SF_Field_API_Name__c

SubscriberMapping__c Values

Name || Product Field Name || Object API Name || SF_Field_API_Name

Contact-Email || Email || Contact || Email

Contact-First Name || First Name || Contact || FirstName

And the Map will the String for the obj name and List is the object with its fields and values, the fields are the same as SF_Field_API_Name in the custom setting.

So I should match the obj name and then the field names to get its values.

  • can you post a picture of custom settings and the values of recordsMap – Santanu Boral May 15 '17 at 19:24
  • I have added all the data I have, the custom setting structure and 2 rows as example of what it has. – BoDiE2003 May 15 '17 at 19:43
1

If I can understand your requirement correctly, here will be the code.

public List<SubsExtendedInfo> generateWrapperInfo (Map<String, List<Sobject>> recordsMap){

Map<String, SubscriberMapping__c> mappingFieldsMap = SubscriberMapping__c.getAll();

    for(String str:mappingFieldsMap.keyset())
    {
        SubscriberMapping__c mc = SubscriberMapping__c.getValues(str);

        //retrieve values based on key
        String objectAPIName = mc.get('Object API Name');
        String strSF_Field_API_Name = mc.get('SF_Field_API_Name');

        //compare with the values passed as argument in this method.
        if(recordsMap.containsKey(objectAPIName))
        {
            //get the list of SObject
            List<Sobject> lst = recordsMap.get(objectAPIName);

            //loop through the list
            for(Sobject sobj:lst)
            {
                //print the values from SObject where API name will from custom settings.
                System.debug('print the values=' + sobj.get(strSF_Field_API_Name))

            }
        }
    }
}
| improve this answer | |
  • 1
    Yes the approach works for me to understand how to iterate over the method args. Thank you – BoDiE2003 May 15 '17 at 20:31

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.