Create a method that convert a List<SObject> to a Map<SObjectField, SObject> with SObjectField as method parameter, any suggestions? - salesforce

global with sharing class test1 {
global Map<SObjectField, SObject> ConvertMap(List<SObject> listToConvert){
List<SObject> listToConvert = new List<SObject>;
Map<SObjectField, SObject> mapTest = new Map<SObjectField, SObject>;
mapTest.putAll(listToConvert);
return mapTest;
}
I've written this code, but it doesn't respect the request 'cause I can't put SObjectField as method parameter since the Map won't recognize the variable.
Does anyone have suggestions on how to do it?
Thank you in advance.

Do you really need SobjectField as map keys? the special class? If you're fine with strings as keys then there's a builtin, getPopulatedFieldsAsMap()
If you absolutely need SobjectField (I don't even know if it's serializable and can be passed to integrations, LWC etc)... You'd have to loop and marry the results to something like
Schema.describeSObjects(new List<String>{'Account'})[0].fields.getMap()

Related

Use content of a tuple as variable session

I extracted from a previous response an Object of tuple with the following regex :
.check(regex(""""idSc":(.{1,8}),"pasTemps":."codePasTemps":(.),"""").ofType[(String,String)].findAll.saveAs ("OBJECTS1"))
So I get my object :
OBJECTS1 -> List((1657751,2), (1658105,2), (4557378,2), (1657750,1), (916,1), (917,2), (1658068,1), (1658069,2), (4557379,2), (1658082,1), (4557367,1), (4557368,1), (1660865,2), (1660866,2), (1658122,1), (921,1), (922,2), (923,2), (1660875,1), (1660876,2), (1660877,2), (1658300,1), (1658301,1), (1658302,1), (1658309,1), (1658310,1), (2996562,1), (4638455,1))
After that I did a Foreach and need to extract every couple to add them in next requests So we tried :
.foreach("${OBJECTS1}", "couple") {
exec(http("request_foreach47"
.get("/ctr/web/api/seriegraph/bydates/${couple(0)}/${couple(1)}/1552863600000/1554191743799")
.headers(headers_27))
}
But I get the message : named 'couple' does not support index access
I also though that to use 2 regex on the couple to extract both part could work but I haven't found any way to use a regex on a session variable. (Even if its not needed for this case but possible im really interessed to learn how as it could be usefull)
If would be really thankfull if you could provided me help. (Im using Gatling 2 but can,'t use a more recent version as its for work and others scripts have been develloped with Gatling2)
each "couple" is a scala tuple which can't be indexed into like a collection. Fortunately the gatling EL has a function that handles tuples.
so instead of
.get("/ctr/web/api/seriegraph/bydates/${couple(0)}/${couple(1)}/1552863600000/1554191743799")
you can use
.get("/ctr/web/api/seriegraph/bydates/${couple._1}/${couple._2}/1552863600000/1554191743799")

How can i achieve dictionary type data access in Chromium embedded CEF1

I would like to achieve dictionary like data pattern that can be accessed from the
java script. Something like this:
pseudo Code:
for all records:
{
rec = //Get the Record
rec["Name"]
rec["Address"]
}
I am trying to achieve with CefV8Accessor, but i am not getting near to the solution.
Kindly provide few links for the reference, as i see the documentation is very less from chromium embedded.
If I understand correctly, you're trying to create a JS "dictionary" object for CEF using C++. If so, here's a code snippet that does that:
CefRefPtr<CefV8Value> GetDictionary(__in const wstring& sName, __in const wstring& sAddress)
{
CefRefPtr<CefV8Value> objectJS = CefV8Value::CreateObject(NULL);
objectJS->SetValue(L"Name", sName, V8_PROPERTY_ATTRIBUTE_NONE);
objectJS->SetValue(L"Address", sAddress, V8_PROPERTY_ATTRIBUTE_NONE);
return objectJS;
}
The CefV8Accessor can also be used for that matter, but that's only if you want specific control over the set & get methods, to create a new type of object.
In that case you should create a class that inherits CefV8Accessor, implement the Set and Get methods (in a similar way to what appears in the code above), and pass it to the CreateObject method. The return value would be an instance of that new type of object.
I strongly suggest to browse through this link, if you haven't already.

one to many relationship in Objectify4

I am working with GAE with java. i am just creating a sample application with Student and Course relationships. I am having many branches and many students. Each branch can have many students, i tried like
ObjectifyService.register(Course.class);
ObjectifyService.register(Student.class);
Course course = new Course();
course.setName("ss");
course.setBranch("cs");
ofy().save().entity(course).now();
Student stu = new Student();
stu.setName("student1");
Key<Course> courseKey = new Key<Course>(Course.class, course.getId()); // getting error here
stu.setCourse(courseKey);
System.out.println("saving");
ofy().save().entity(stu).now();
I am not sure how to define this relationship in objecitfy4. I followed the tutorial http://www.eteration.com/objectify-an-easy-way-to-use-google-datastore/
Thanks.
Look at the Javadocs for Key. Instead of a public constructor, there is a more convenient creator method which requires less typing of <> generics:
Key<Course> courseKey = Key.create(Course.class, course.getId());
There are two keys that you might use:
first the GAE low-level com.google.appengine.api.datastore.Key and
second, the objectify's com.googlecode.objectify.Key.
You can use both with Objectify (as under the hood they are ultimately converted to low-level API).
Neither has a public constructor so you can not use new with them.
With low-level keys you'd use KeyFactory.createKey("Course", course.getId()).
With objectify key you'd use com.googlecode.objectify.Key.create(Course.class, course.getId())
When we use Objectify , if we need any Key to be created for condition(KeyFactory.createKey("Course", course.getId()) , In course object, Id field should be specified with index. It will work fine.

Change Value of ParamInfo after adding to DynamicParameters?

I'm calling a stored proc in a foreach loop and would like to change the value of one of the parameters on each iteration. Currently, there doesn't seem to be any way to access the parameters once they've been added to DynamicParameters although from reading the source, I can see that DynamicParameters does keep an internal Dictionary. Any reason why this isn't public or if there's another way to get at the ParamInfos to change values?
Update
What I have currently:
foreach ( var fooID in fooIDs )
{
var dynamicParameters = new DynamicParameters();
dynamicParameters.Add( ParameterNames.BarID, barID );
dynamicParameters.Add( ParameterNames.FooID, fooID);
connection.Execute( ProcNames.MyProc, dynamicParameters, commandType:CommandType.StoredProcedure );
}
Re-Add the parameter.
// Call Add() with new values.
dynamicParameters.Add(ParameterNames.BarID, differentBarID);
There is no real reason DynamicParameters is so secret about what it does, the ParamInfo class could be exposed and I would be happy to provide proper iteration/modification properties and/or methods. If you feel like you would like to pitch in, please submit a patch.
In the mean time you can simply implement IDynamicParameters which is the trivial interface we use to dispatch this to the underlying command, in your app. You can use DynamicParameters as a starting point.

save Elements in Map/Array/Collection ...... Grails

I'm new to grails 1.3.7 and I have a problem.
I want to store different elements/paramters in one list/array/map/whatever..
the data to be stored looks like this:
id : answera, answerb, answerc, answerd, answere, answerf, answerg, answerh
id is a number
answers are booleans
so Ive got a lot of ids (well, maybe 20) and for each one 8 answers-booleans.
How do I store them the best, so that I can access them very easy again?
Thank you :-)
[EDIT] Thanks a lot for those answers, I will try it out now! :-)
I have now a map containing an id (int) and an object representing my answers (its a pojo which contains booleans answera, answerb, etc...)
Now I give this map to a gsp. How do I know get the data out of it? Thanks for help! :-)
A Map would be the best approach, however it really has nothing to do with grails. Do you need to persist these to a Domain Class/Database?
What a map would look like...
def map = [:]
map.put(id1, [new Answer(accepted:true), new Answer(accepted:false)];
map.put(id2, [new Answer(accepted:false), new Answer(accepted:false)];
I don't think this would give you an easy domain class to work with. Sounds like you would want a grails domain class to encapsulate the answers. Something like...
class Question{
static hasMany = [answers:Answer]
Integer id
Boolean answered
def hasBeenAnswered(){
answers.each(){ answer->
if (answer.accepted){
answered = true;
return true;
}
}
return false;
}
def acceptAnwser(Answer answer){
answer.accepted = true;
this.answered = true;
}
}
class Answer{
static belongsTo = [question:Question]
Integer id
Boolean accepted
String text
}
And then your code would be easier to use...
def allQuestion = Question.list();
def allUnansweredQuestions = Question.findAllByAnswered(false);
def allAnsweredQuestions = Question.findAllByAnswered(true);
A Map seems like the obvious structure. The keys of the map should be the ids and the values of the Map should be either a List<Boolean> or (probably preferably) a class that encapsulates these 8 booleans.

Resources