function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AmanAman 

String to Sobject

 

I have a list of type String which contains objects of Salesforce like Contact, Account, Opportunity,quote,case etc.

I want to convert the list of string in sObject to use the following line.

 

List<String> a=new List<String>();a.add(sObject.valueOf('Account'));

a.add('Contact');

a.add('opportunity');

a.add('Quote');

a.add('Account');

 

for(String x:a)

{

Map<String,sObjectField> fmap=Schema.sObjectType.x.fields.getMap();

}

// but it  showing error. How i convert a string into Sobject.

 

the red color X is will be string from a list.....

 





Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

You need to do it like this

 

 

List<String> a=new List<String>();
a.add('Account');
a.add('Contact');
a.add('opportunity');
a.add('Quote');

 

<String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
//This will have obj name as key and field map as value
Map<String , Map<String,Schema.SObjectField> > mapObj_FielMap = new Map<String , Map<String,Schema.SObjectField> >();

for(String objName : a)
{
Schema.SObjectType sobjType = gd.get(objName);
Schema.DescribeSObjectResult r = sobjType.getDescribe();
Map<String,Schema.SObjectField> M = r.fields.getMap();
mapObj_FielMap.put(objName , M);
}

 I hope will help you , please ask if any confusion.

All Answers

kiranmutturukiranmutturu

why can't u take list of sobject type itself there?

Shashikant SharmaShashikant Sharma

You need to do it like this

 

 

List<String> a=new List<String>();
a.add('Account');
a.add('Contact');
a.add('opportunity');
a.add('Quote');

 

<String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
//This will have obj name as key and field map as value
Map<String , Map<String,Schema.SObjectField> > mapObj_FielMap = new Map<String , Map<String,Schema.SObjectField> >();

for(String objName : a)
{
Schema.SObjectType sobjType = gd.get(objName);
Schema.DescribeSObjectResult r = sobjType.getDescribe();
Map<String,Schema.SObjectField> M = r.fields.getMap();
mapObj_FielMap.put(objName , M);
}

 I hope will help you , please ask if any confusion.

This was selected as the best answer
AmanAman

Thanks

:smileyhappy:

Shashikant SharmaShashikant Sharma

Your welcome Aman

Nilesh MendheNilesh Mendhe
Hello Shashikan Sir, Here I am also getting the Same problem, please visit

http://salesforce.stackexchange.com/questions/41744/need-a-string-to-map-conversion-or-any-solution-for-below
rajesh k 10rajesh k 10
Hi Sreekanth sharma,

 USing Your Without using fieldset code i displayed all objects related fields dynamically in an visualforce page.But i con,t able gave Save functionality.Using below code i saved but record only created related object but record related information not saved.How to save object related record information also.

 using below code i save object record in database dynamically. Schema.SObjectType targetType = Schema.getGlobalDescribe().get(strFieldName); SObject myObj = targetType.newSObject(); insert myObj;

Note:using above code related object record only created.

but I gave some fields information here but those fields information is not saved in this record How to save fields information also related object record Dynamically?

help meeeeeeeeeeeeeeeee..........
srilakshmi1.387861669756762E12srilakshmi1.387861669756762E12
Hi,
Here i am getting list of picklist fields of an account into  list<string> (listObjectFields1).


for(string pickval:listObjectFields1)
        {
             system.debug('************'+pickval);// displaying all picklist fields
            Schema.DescribeFieldResult fieldResult = Account.Industry.getDescribe();// here instaded of Industry i want to give pickval(it is not posible because it is a string ,so i want to convert this pickval to account type.)how can i     please help me 
            system.debug('33333333'+fieldResult);
            list<schema.picklistentry> values = fieldResult.getPickListValues();             
            system.debug('************'+values);
            for (Schema.PicklistEntry a : values)
            {                
                options.add(new SelectOption(a.getLabel(), a.getValue()));
            }         
            
        }
rajesh k 10rajesh k 10
Hi Srilakshmi,
                               See below link
http://gautamsfdc.blogspot.in/2013/01/display-multi-select-picklist-as-multi.html