How to concatenate two maps in apex - concatenation

I have two maps, different sObject fields are being stored, one for each sObject. I want to add the fields from map1 to the beginning of map2 or vise-versa.
Is it possible to add the contents of one map to another without merging similar fields or keys? Can the values in a map that is an array be placed in such a way that they come after the values of the first map?

You can use the putAll(fromMap) method : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm
Map<String, String> map1 = new Map<String, String>();
map1.put('Red','FF0000');
Map<String, String> map2 = new Map<String, String>();
map2.put('Blue','0000FF');
// Add map1 entries to map2
map2.putAll(map1);
System.Debug(map2);
//map2 = {Blue=0000FF, Red=FF0000}
If you want to go the other way : map1.putAll(map2);

Related

Dynamic change of field value using sObject

I'm trying to use sObject to dynamically change Name field objects across while organization.
I've tried using SomeId.getSObjectType().newSObject(SomeId) to create the sObject, but when I try to change the Name field I have error
Variable does not exist: Name
Map<Id, string> idsToUpdate = new Map<Id, string>();
// Put the Id's and associated name values in the map
List<SObject> sObjectsToUpdate = new List<SObject>();
foreach(Id idToUpdate : idsToUpdate.keySet) {
SObject o1 = idToUpdate.getSObjectType().newSObject(idToUpdate);
o1.Name = idsToUpdate.get(idToUpdate);
sObjectsToUpdate.add(o1);
}
update sObjectsToUpdate;
As I can see other posts, this is the way of creation dynamic update of objects.
Any idea why this happens?
Not all objects have a name field, you should check for the existence of the name field before trying to set the field also you must use the put method
Map <String, Schema.SObjectField> fieldMap = o1.getSobjectType().getDescribe().fields.getMap();
if(fieldMap.containsKey('Name')){
o1.put('Name', 'Test');
}

How to update the existing field of solr index using java?

I have a Solr index in a core having 3000 documents.
I want to modify the value of a single field in the entire core based on unique key PaperID.
I'm using the following java code but instead of updating the existing value it adds new documents.
if (solrDocument.get("PaperID").equals(solrDocument2.get("PaperID"))) {
String Mscore = (String) solrDocument.get("ID");
String ModifyScore = (String) solrDocument.get("Author");
//solrDocument.setField("ID", ModifyScore);
//update the field
System.out.println(Mscore);
System.out.println(ModifyScore);
System.out.println(solrDocument2.get("Mscore") + "\n");
SolrInputDocument sid = new SolrInputDocument();
Map<String, Object> fieldModifier = new HashMap<String, Object>(1);
fieldModifier.put("set", ModifyScore);
sid.setField("ID", fieldModifier);
//solr.add(sid);
solr.commit();
}
can anyone guide me accordingly...Best Regards
Your code isn't changing anything, since you've commented out the .add command. And you have to use the actual ID in the ID field, since Solr won't know what document to update otherwise. The field you're changing should have the fieldModifier attached:
SolrInputDocument doc = new SolrInputDocument();
Map<String, String> fieldModifier = new HashMap<String, String>();
// Change ModifyScore to the new value, since you're just using the current value now..
fieldModifier.put("set", ModifyScore);
doc.addField("ID", Mscore);
doc.addField("Author", fieldModifier);
solr.add(doc);
solr.commit();

How can we convert List<SelectOption> to a List<string> in apex?

I have a list of "selectoption" and I want to use this in my SOQL.
The WHERE IN clause gives me incompatible error.
Can we convert list of selectoption to a list of strings?
What exactly you want to take from SelectOption items to list of strings - labels, values or something else? In any case, you can use getter-methods, like getLabel() or getValue() and collect them to list. For instance,
List<SelectOption> selectOptions = new List<SelectOption>(); //here it's empty, but you has filled one
List<String> stringValues = new List<String>();
for(SelectOption so: selectOptions){
stringValues.add(so.getValue());
}
//use list of strings as you wish
You can find documentation about SelectOption methods here.

Extract JSON inside JSON in CN1

My app receives JSON responses from an API. When it arrives, the app uses the JSONParser class to get a result as a Map:
JSONParser jp = new JSONParser();
Map <String, Object> result = jp.parseJSON(new InputStreamReader(input, "UTF-8"));
The "result" contains 2 values. First a URL that I can get using the key "url":
String url = result.get("url");
And second, another set of data which is in JSON format and I get using the key "eager". I tried to extract it to a Map:
java.util.List<Map<String, Object>> eager = (java.util.List<Map<String, Object>>) result.get("eager");
The resulting Map does not behave like a Map, so I passed the whole eager Map to a String, just to check the content and I noticed that the data is inside brackets, like this:
[{item1=x, item2=x,.....itemN=x}]
Question:
How can I get the data as a JSON object?
You parse your result String as a List. What happend if you try to get "data" like this ?
java.util.List<Map<String, Object>> newResult = (java.util.List<Map<String, Object>>) result.get(0);
You need to use the key "root" not "data" to get your list.
"result" is a Map.
"eager" is a List of Maps.
You need to get the first position of the eager Map in order to get the Map with the data:
Map <String, Object> result = jp.parseJSON(new InputStreamReader(input, "UTF-8"));
java.util.List<Map<String, Object>> eager = (java.util.List<Map<String, Object>>) result.get("eager");
Map<String, Object> eagerMap = (Map<String, Object>) eager.get(0);

Add image in arraylist

I have to create a WPF app. which collects strings and images in rows. I am not sure if I could use multidimedional array or ArrayList but I cannot figure out how to insert the image into the array. Anyone can help me?
So if you want 1 Image against a variable number of string's
your Image becomes the Key of the Dictionary and the List<string> it's corresponding Value.
public Dictionary<Image, List<string>> MyCollection { get; private set; }
...
// Initialisation
MyCollection = new Dictionary<Image, List<string>>();
// Adding new Row
var tempImage = new Image();
MyCollection.Add(tempImage, new List<string>(){"A", "B", "C"});
// Modifying existing row -- for `Key` tempImage we'll add a string "D" and remove string "A"
List<string> existingValues = MyCollection[tempImage];
existingValues.Add("D");
existingValues.Remove("A");
// Removing rows
MyCollection.Remove(tempImage);
You can download this sample from Here. Hope that clarifies some of the usage ideas. I'd suggest looking at some Simple Examples to get a better understanding of how you can use Dictionary<...> to achieve your requirements.

Resources