Can you populate a set with soql? - apex-code

Can you populate a set from a soql query? I couldn't get my syntax to work
Set c = [select id From Contact limit 1000 ];
thanks!

You need to declare the type of your set and make use of the set constructor that takes a list as a parameter giving you something like this:
Set<Contact> c = new Set<Contact>( [SELECT Id, Name FROM Contact LIMIT 1000] );

Related

How to bind value sql query in laravel?

I have a query. Query is include sql function. So I want to directly. But I need parameter.
$parameters = DB::select("SELECT GetFlowPropertyValue(flow.id, 'PCommonLineDataNumber') subscriberNumber, (SELECT Id FROM ConfigurationItem WHERE FlowId = flow.id ORDER BY Id DESC Limit 1) ConfigurationItemId, GetObjectTypeRelationParentObjectValue(6, 1, flow.id) CustomerId, GetFlowPropertyValue(flow.id, 'MainOfferingName') MainOfferingName, GetFlowPropertyValue(flow.Id, 'CampaignName') CampaignName FROM akislar flow WHERE id ='$query->id'");
As you can see, Final parameter come foreach but doesnt run. I use dd and I write query.
I see $query->id is look like empty. It is not defined.
How can I bind $query->id ?

Dynamic Linq core

Hi I am using a Jqwidgets Grid to display my data. It has a build in possibility to use filters but if you filter your records on the server side you have to build your own query. As I am working with Linq I thought to use the Dynamic Linq Library for Asp net core. Problem is there are not many examples or explanations how to do this. But I am busy for days now and not getting very far.The way I am setup; I have a normal Linq query:
var Mut = from M in _DB.Mutations
join S in _DB.Shifts on M.ShiftId equals S.ShiftId
join U in _DB.RoosterUsers on M.UserId equals U.RoosterUserId
join D in deps on M.UserId equals D.UserId
join DD in _DB.Departements on D.DepartementID equals DD.DepartementId
select new MutationModel
{
MutId=M.MutationId,
Naam=U.FirstName + " " + U.LastName,
UserId=M.UserId,
Departement= DD.DepartementName,
MutationType = S.publicName,
MutationGroup = S.ShiftType.ToString(),
DateTot =M.DateTill,
TijdVan=M.DateStartOn,
TijdTot=M.DateTill,
Status=CreateStatus(M.Tentative, M.ApprovedOn, M.Processed, M.CancelRefId, M.Deleted)
};
This query is running OK and gives me all the data I need for the Grid.
Then for the filter I would like to add a dynamic Linq Query using the System.Linq.Dynamic.Core library
But this is as far as I get things working until now:
var outQuery = Mut.Where("Status = #0 and UserId = #1", "Nieuw", "KLM22940").Select("Status");
My questions now :
1. In the where clause If I make the fieldname variable I get an error. how to do this??
2. In the Select Clause, how to add multiple Columns? (actually I just like to output all columns.)
Best would be to see an example. has somebody used Dynamic Linq to build a dynamic linq query for the JQWidgets Grid?
Thank you very much.
In what way you are trying to use fieldname variable in where clause ?
If you want to output all columns you can use ToList()
like
var outQuery = Mut.Where("Status = #0 and UserId = #1", "Nieuw", "KLM22940").ToList();
If you want to get some specific columns you can use Select clause like this
var outQuery = Mut.Where("Status = #0 and UserId = #1", "Nieuw", "KLM22940").Select("new(Status,UserId )");
This Select clause creates data class which contains Status and UserId properties and returns a sequence of instances of that data class.

How to define a map of SqlParameter specifing the SqlType in grovvy?

I want to create a list of parameter using the constructor MapSqlParameterSource that take a map as input.
Only thing is I don't know how to write a map that will tell MapSqlParameterSource not only ID and Value, but also SqlType.
id = 1
desc = "Description"
insertSql = "INSERT INTO table(id,desc) VALUES (:idCode,:descCode)"
SqlParameterSource mapParam = new MapSqlParameterSource([idCode: id, descCode: desc])
In this way I can create the SqlParameterSource, but I can't specify directly the SqlTypes, and I got in one of the test an exception:
oracle.jdbc.driver.OracleSql.computeBasicInfo(OracleSql.java:950)
oracle.jdbc.driver.OracleSql.getSqlKind(OracleSql.java:623)
oracle.jdbc.driver.OraclePreparedStatement.<init>(OraclePreparedStatement.java:1212)
oracle.jdbc.driver.T4CPreparedStatement.<init>(T4CPreparedStatement.java:28)
oracle.jdbc.driver.T4CDriverExtension.allocatePreparedStatement(T4CDriverExtension.java:68)
oracle.jdbc.driver.PhysicalConnection.prepareStatement(PhysicalConnection.java:3140)
oracle.jdbc.driver.PhysicalConnection.prepareStatement(PhysicalConnection.java:3042)
oracle.jdbc.driver.PhysicalConnection.prepareStatement(PhysicalConnection.java:6022)
That I'm assuming means that couldn't find the SqlType. Do you agree with my assumption and do you have a solution that is not something like this?
mapParam = new MapSqlParameterSource()
mapParam.addValue("idCode", stock, Types.NUMBER)
mapParam.addValue("descCode", stockType, Types.VARCHAR)
Try wrapping the value in an SqlParameterValue object using the SqlParameterValue(int sqlType, Object value) constructor

How to make JDBCTemplate.query() generic

I am new to spring framework.I found this even by looking in documentation but didn't find my desired method. Let me explain thing which I want JDBCTemplate.query() method can take multiple parameters which can be 2, 3 ,4....n.Now I am trying to find a method [if JDBTemplate have] through which i just passed a object/array/list , mean a generic parameter so I get rid from the multiple methods.As If I have 200 stored procedures then I have to write 200 mehods! huhCurrently I am doing this by passing an array and splitting it at DB end but I want to find a good way.Any idea/suggestions
Are you looking for something like this?
//set any number of parameters
Object[] parameters = new Object[] { new Integer(1), "test" };
String sqlQuery = "select * from table1 t where t.id = ? and t.text = ?";
SqlRowSet srs = getJdbcTemplate().queryForRowSet(sqlQuery , parameters);
You may generalize the is by passing the query and parameters from outside of the method as an argument.

Update entity columns iterating through col list using LINQ

I can get column list from the table using LINQ like this:
OrderDataContext ctx = new OrderDataContext();
var cols = ctx.Mapping.MappingSource
.GetModel( typeof( OrderDataContext ) )
.GetMetaType( typeof( ProductInformation ) )
.DataMembers;
This gives me the list of columns, so I can do this:
foreach ( var col in cols )
{
// Get the value of this column from another table
GetPositionForThisField( col.Name );
}
So this all works, I can iterate through column list and pull the values for those columns from an another table (since the column names are the keys in that another table), so I don't have to do switch....or lot of if...then...
Now the question:
After I get these values, how do I populate the entity in order to save it back? I would normally go like this:
ProductInformation info = new ProductInformation();
info.SomeField1 = val1;
info.SomeField2 = val2;
ctx.ProductInformation.InsertOnSubmit( info );
ctx.SubmitChanges();
But how to use the same column collection from above to populate the columns while iterating over that, when there is no such thing as:
info["field1"].Value = val1;
Thanks.
Just fetch the object that you want to modofy, set the property and call SubmitChanges. There is no need to create a new object and insert it. The Context tracks your changed properties and generates the update statement accordingly. In your case you may want to set the properties via reflection rather than manually since you are reading them from another table.
You'll need to use reflection. Assuming you can get the PropertyInfo from the metadata:
PropertyInfo property = GetPropertyForThisField(col.Name);
property.SetValue(info, val1, null);

Resources