0

I am not familiar with the data collections and want the solution. requirement is like:

I want to have object name in the where statement for query like:

select id , Name from Documents__c where ObjectId = '000002233377'

I am getting the object name from

ObjectName = objectInstance.getDescribe().getName();

now if the ObjectName is 'Account' the ObjectId in query should be 'Account__c', if the ObjectName is 'Contact' the ObjectId in query should be 'Contact__c'

So how can I Get the ObjectId directly the key name if I use map like:

    Map<String, String> MyStrings = new Map<String, String>{
                       'Account' => 'Account_id__c', 
                       'Contact' => 'Contact__c',
                       'Insurance Quote' => 'Insurance_Quote__c',
                       'Payment Transaction' => 'Transaction__c'};

1 Answer 1

1

This code will create a dynamic SOQL query where the field queried will vary based on using the type of the SObject to lookup the field name:

     ...
     Account a = ...;
     myMethod(a);
     ...

private void myMethod(SObject sob) {
    String objectName = String.valueOf(sob.getSObjectType());
    String field = myStrings.get(objectName);
    String value = sob.Id;                   // '000002233377';
    for (Documents__c d : Database.query(''
            + ' select Id, Name'
            + ' from Documents__c'
            + ' where ' + field + ' = :value'
            ) {
        // Use the Id and Name in here
    }
}
0

You must log in to answer this question.

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