1

I have below string value from which I need a specific section to put in another string. Can someone please help?

String

[{"attributes":{"type":"Call2_vod__c","url":"/services/data/v37.0/sobjects/Call2_vod__c/a0422000000teg5AAA"},"**Name**":"**C-017441978**","Id":"a0422000000teg5AAA"}]

from above string I need to identify Name and fetch immediate data after the next : immediate after Name

In above case, the value is C-017441978

Can someone please help me to do substring this?

Reason

for (Id key : m.keySet()) {

   string objectfullname = m.get(key);
    Schema.SObjectType targetType = Schema.getGlobalDescribe().get(objectfullname);
    if (targetType == null) {
    system.debug('Type not found: '+objectFullname);

    }

    string query = 'SELECT Name FROM '+ objectfullname + ' where Id =\'' + key +'\'' ;   
    sobject [] records = Database.query(query);
    string JSONString = JSON.serialize(records);


    recordname.put(key,JSONString);
}

In above code after the JSON.serialize I am getting this error. Any help on above block for different approach is most welcome!

  • 1
    How dynamic is that structure? Probably, serialize as untyped,and get Name, like: Map<String,Object> m = JSON.deserializeUntyped(input_string); String r = (String)m.get('Name'); – kurunve Aug 2 '16 at 9:06
  • Can you please help on my updated question? Yes it's for serialize. :) – Testing_SFDC Aug 2 '16 at 9:08
  • your update even make it more unclean? which error? what are you trying to achieve in general? – kurunve Aug 2 '16 at 9:08
  • The value of Name only I need, as you suggested. – Testing_SFDC Aug 2 '16 at 9:10
  • so, you want to create map recordname, where name is stored by record id? – kurunve Aug 2 '16 at 9:10
1

You do not need serialization/deserialization.

You can easily use sObject method get to get name of the record. For example, in your code you can use this way:

for (Id key : m.keySet()) {
    string objectfullname = m.get(key);
    Schema.SObjectType targetType = Schema.getGlobalDescribe().get(objectfullname);
    if (targetType == null) {
        system.debug('Type not found: '+objectFullname);
    }else{
        string query = 'SELECT Name FROM '+ objectfullname + ' where Id =\'' + key +'\'' ;   
        sobject [] records = Database.query(query);
        if(!records.isEmpty()){
            recordname.put(key,(String)records.get(0).get('Name'));
        }
    }
}

Reference for sObject methods is here.

P.S. Code above is not really bulkified. In case if there are bunch of records of the same time, it would always cause an SOQL, and may hit limit very fast.

To avoid than, next quick sample has been created:

//Original map m = new Map<Id, String>();
//Result map record_names = new Map<Id, String>();
Map<String, Set<Id> > reversed_m = new Map<String, Set<Id>>();
for(Id key : m.keySet()){
    String object_full_name = m.get(key).toLowerCase();//Lower case just for sure
    if (!reversed_m.contains(object_full_name)){
        reversed_m.put(object_full_name, new Set<Id>());
    }
    reversed_m.get(object_full_name).add(key);
}

//in this case, we would have all different sobjects,
// and corresponding grouped record ids in new map

for (String object_name : reversed_m.keySet()) {
    //Ids for all the records of the same type
    Set<Id> ids = reversed_m.get(object_name);
    Schema.SObjectType target_type = Schema.getGlobalDescribe().get(object_name);
    if (target_type == null) {
        system.debug('Type not found: ' + object_name);
    }else{
        //single query for all records of the same type
        string query = 'SELECT Name FROM '+ object_name + ' where Id in :ids ';
        sObject[] records = Database.query(query);
        if(!records.isEmpty()){
            //populating result recordname for all returned records
            for(sObject sobj : records){
                record_names.put(sobj.Id,(String)sobj.get('Name'));
            }
        }
    }
}

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.