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
maytawnmaytawn 

Map Iterate

I have a map of two strings.  I want to put the map in a simple loop and print out the key and value pairs.  Is this possible?

Best Answer chosen by Admin (Salesforce Developers) 
ahab1372ahab1372

just loop through the map.keySet() and use map.get(key) to get the second string

for (string theKey:theMap.keySet() )
{
   do something with theKey;
   do something with theMap.get(theKey);
}

 not sure though what you mean by "print out". Display them on a VF page?

 

 

All Answers

ahab1372ahab1372

just loop through the map.keySet() and use map.get(key) to get the second string

for (string theKey:theMap.keySet() )
{
   do something with theKey;
   do something with theMap.get(theKey);
}

 not sure though what you mean by "print out". Display them on a VF page?

 

 

This was selected as the best answer
maytawnmaytawn

Perfect.  That was exactly what I was looking for.  Except I'm getting an error.  This is what I am trying to do.

 

Map<String, String> c = new Map<String, String>();
c.put('name', opp.Name);
c.put('date', String.valueOf(opp.Arrival_Date__c));

String body = '_method=POST&';
for (String Key:c.keySet()) {
    body += EncodingUtil.urlEncode(Key)+'='+EncodingUtil.urlEncode(c.get(Key))+'&';
}

 

 

The error I get is: Save error: Method does not exist or incorrect signature: EncodingUtil.urlEncode(String)

 

 

maytawnmaytawn

Wonderful.  That was what I needed. Thank you.  For those that are interested, this is the final code that works.

 

Map<String, String> c = new Map<String, String>();
c.put('name', opp.Name);
c.put('date', String.valueOf(opp.Arrival_Date__c));

String body = '_method=POST&';
for (String Key:c.keySet()) {
    body += EncodingUtil.urlEncode(Key, 'UTF-8')+'='+EncodingUtil.urlEn​code(c.get(Key), 'UTF-8')+'&';
}

 

ahab1372ahab1372

great. can you accept the post as solution so that others see the topic as solved on the board?

 

btw why do you put the values in the map at all? Or is it just sample code?

maytawnmaytawn

Let me preface this by saying that I am very new to Apex, so there may be a better way to do this.  If anyone knows of a better way or a "best practice" please let me know.

 

The reason that I am putting the values in a map is because I am trying to make a generic web callout class.  My idea is to keep the name/value pairs close to the data and then let that call out to a central callout class.  In the example code above it is all together to give some context... but in actuallity there are two parts and they are separated.

 

This is what is really looks like:

trigger OpportunityCallout on Opportunity (after update) {
	for (Opportunity opp : Trigger.new) {
		String url = 'http://external.server.com';
		Map<String, String> dataPayload = new Map<String, String>();
		dataPayload.put('data[Event][name]', opp.Name);
		dataPayload.put('data[Event][start_date]', String.valueOf(opp.Arrival_Date__c));
		dataPayload.put('data[Event][end_date]', String.valueOf(opp.Departure_Date__c));
		dataPayload.put('data[Event][nightly_rate]', String.valueOf(opp.Nightly_Rate__c));
		dataPayload.put('data[Event][minimum_attendance]', String.valueOf(opp.Minimum_Attendance__c)); 
		dataPayload.put('data[Event][minimum_fee]', String.valueOf(opp.Minimum_Fee__c));
		dataPayload('data[Event][minimum_camper_days]', String.valueOf(opp.Minimum_Camper_Days__c));
		dataPayload.put('data[Event][deposit_amount]', String.valueOf(opp.Deposit_Amount__c));		
		
		// make the asynchronous web service callout
		WebServiceCallout.webPOST(url, dataPayload);

	}
}

 

So now the callout is pretty dumb and can be reused.  The one thing I am still working on is to figure out how to inteligently handle the response that is releavant to the calling trigger.

 

public class WebServiceCallout {
 
	@future (callout=true)
	public static void webPOST(String url, Map<String, String> dataPayload) {

 		String body = '_method=POST';
 		for (String Key:dataPayload.keySet()) {
 			String value = '';
 			if (dataPayload.get(Key) != null) { value = dataPayload.get(Key); }
 			body += '&'+EncodingUtil.urlEncode(Key, 'UTF-8')+'='+EncodingUtil.urlEncode(value, 'UTF-8');
    	}
 		
 		HttpRequest req = new HttpRequest();
 		req.setEndpoint(url);
 		req.setMethod('POST');
		req.setBody(body);

		HttpResponse res = new HttpResponse();
		Http http = new Http();
		res = http.send(req);
		//TODO: Do something with the response.
 	}
}

 

 

ahab1372ahab1372

got it. I figured there was more code involved :-)

 

To process the response, it might help to add the opportunity ID to the map, e.g. dataPayload.put('Id', opp.Id)

then you can (conditionally?) add the result to the opportunity, maybe as a closed or open task. 

 

I haven't done any callouts myself yet, so it's interesting to see what others do with them.