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
miku1051miku1051 

Map of API name and value pair..?

Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();
Opportunity opp=[Select id,name,accountId from Opportunity];
 Map<String,string> valueMap=new Map<String,String>();

    for(opportunity o:opp)
           {
                for(String fieldName: fieldMap.keySet())
                {
                    **valueMap.put(fieldName,o);**
                }

           }

 I want to create a map of apiname and value pair..please help... 

i want record value...like if apiname of opportunity field is name..then i want record value..ie opportunity name...

Best Answer chosen by Admin (Salesforce Developers) 
sushant sussushant sus
hi,
try this
Map<String,opportunity> valueMap=new Map<String,opportunity>();

for(opportunity o:opp)
{
for(String fieldName: fieldMap.keySet())
{
**valueMap.put(fieldName,o);**
**valueMap.put(fieldName,o.name);**//in this dont change u map it will remain(string,string)

}

}

All Answers

souvik9086souvik9086
valueMap.put(fieldName,'Opportunity '+fieldname);

Try this.

 

If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.

Thanks

sushant sussushant sus
hi,
try this
Map<String,opportunity> valueMap=new Map<String,opportunity>();

for(opportunity o:opp)
{
for(String fieldName: fieldMap.keySet())
{
**valueMap.put(fieldName,o);**
**valueMap.put(fieldName,o.name);**//in this dont change u map it will remain(string,string)

}

}
This was selected as the best answer
vbsvbs

@Miku - This should be what you are after I guess, assuming you are try to setup a map of field names v/s opportunity object field values. Replace your astericked line:

**valueMap.put(fieldName,o);**

 with this

valueMap.put(fieldName,o.get(fieldName));

 

Do mark this as a solution if this helps and KUDOS would be the way to go....

 

Regards

vbs

 

 

MyGodItsColdMyGodItsCold

I'm having trouble generating the fieldMap - what is leadSchema?

miku1051miku1051
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Schema.SObjectType leadSchema = schemaMap.get(objname);

 objname here is name of any object...may selected from picklist on vf page..

 

Let me know if you have any issue..

miku1051miku1051

here is sample code...

 

Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();
Opportunity opp = [Select id,name,accountId from Opportunity];
Map<String,String> valueMap = new Map<String,String>();

for ( Opportunity o : opp ) {
    for ( String fieldName : fieldMap.keySet() ) {
        valueMap.put(fieldName, o.get(fieldName));
    }
}
MyGodItsColdMyGodItsCold

Thanks! 

 

Instead of

 

valueMap.put(fieldName,o.get(fieldName));

 

I used:

 

valueMap.put(fieldName,string.valueof(o.get(fieldName)) );

 

To avoid type mismatch.

miku1051miku1051

yeah..cheers it worked for you...

 

 

MyGodItsColdMyGodItsCold

Well, I'm still having problems. Message:

 

SObject row was retrieved via SOQL without querying the requested field: Opportunity.PROSP_SRC_DESC__c 

 

An unexpected error has occurred. Your development organization has been notified.

 

Here are the exact items I'm using:

 

1. Apex Controller - ApiValueMapMaker.cls:

 

public with sharing class ApiValueMapMaker {
	public Map<String,String> valueMap {set;get;}
    public ApiValueMapMaker(ApexPages.StandardController stdController)     {
    	valueMap = new Map<String,String> ();
    	Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Schema.SObjectType leadSchema = schemaMap.get('Opportunity');
        //Opportunity op = (Opportunity)stdController.getRecord();
        Opportunity op = [Select id,name,accountId from Opportunity LIMIT 1];
        Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();
	    for(String fieldName: fieldMap.keySet()) {
            valueMap.put(fieldName,string.valueof(op.get(fieldName))); 
        }
    }
}

 2. Visualforce page - ApiValueMapMakerDisplay.page:

 

<apex:page standardcontroller="Opportunity" extensions="ApiValueMapMaker">
Map is:
<br/>
{!ValueMap}
</apex:page>

 

Then, I called it via:

 

/apex/ApiValueMapMakerDisplay?id=*** opportunity SFDC ID goes here ***

 

When I replaced the getRecord() (from the ?id=... on the page), to a SELECT .... LIMIT 1, I still get the error message.

 

Thanks,

 

miku1051miku1051

You need to make query dynamic..as you are not adding all the fields in your query

 

Opportunity op = [Select id,name,accountId from Opportunity LIMIT 1];

 

String query1='Select ';
            for(String str:fieldMap.keySet())  
            {
                query1+=str+',';
            }
            query1=query1.removeEnd(',');
           query1+=' from '+objname+' where id=\''+oppid+'\'';
for(Sobject o:finalQueryRes)
           {
                for(String fieldName: fieldMap.keySet())
                {
                    System.debug('--------value---------'+o.get(fieldName));
                    if((o.get(fieldName))!=null)
                    {
                        valueMap.put(String.valueof(fieldName),String.valueof(o.get(fieldName)));
                    }
                }
                
                
           }

 try this...

 

or try adding all the fields in your query...it will work....

miku1051miku1051

let me know if it works...