Dynamics CRM Online : Setting option set value with a plug-in - c#

I am attempting to create a plug-in that will populate a option set field with a value that is in a string field on an entity.
Here's how I think it would happen, just don't know how to approach it:
-Plug-in is fired, via create message;
-Plug-in gets the 'Target' entity, and checks if the string field is populated;
-If the string value is the same as an Option Set value's label, set the option set field value on the target entity;
-Update the target entity with the option set field populated.
I can accomplish all the steps, minus the comparing the string value to existing option set values. I'm not sure how to retrieve all the Option Set data to search through it.

Related

Sort the option set based on the back end option set value

I have tried to sort the option set value but it is sorting based on the Alphabet, it should be sorted based on option value
I tried the below code, but not sorting based on the option set value.
QueryExpression filBusinessInventory = new QueryExpression("new_filbusinessinventoryfa");
filBusinessInventory.Criteria.AddCondition("createdon", ConditionOperator.On, dateString2);
filBusinessInventory.Criteria.AddCondition("statecode", ConditionOperator.Equal, "Active");
filBusinessInventory.ColumnSet = new ColumnSet("createdon", "new_customer", "new_month", "new_year");
filBusinessInventory.AddOrder("new_month", OrderType.Ascending);
filBusinessInventory.Orders.Add(new OrderExpression("new_month", OrderType.Descending));
Need to sort the option set field "month" based on the option set value not on Alphabetic order.
Ordering on optionset fields are done on the value of the field and not on the Alphabet.
Just add
filBusinessInventory.AddOrder("new_month", OrderType.Ascending);
without adding also
filBusinessInventory.Orders.Add(new OrderExpression("new_month", OrderType.Descending));
You can retrieve data from crm as it is I.e ordering by label but when you have entity collection object in C# you can order it based on option set value.
You can also convert your entity collection object into list and apply lambda function which will give you option of ordering based on value.

Algorithm for setting the next AutoNumber for the table

I have a table called AutoNumber which have fields like
Public Class AutoNumber{
int Id,
Bool Autogenerate
Book IsLocked,
Int IncrementValue,
String Prefix,
String Postfix,
int IdentityValue
string ModuleName
}
I store the settings for a module in this table.In this table i store the Next AutoNumber for a module .So whenever I am creating Customer I would fire the query and get the Value for the AutoNumber.
Now this works fine with the Single Create and Single save where in i could check the Identity Value and set the NextAutoNumber after a customer is saved.
I have functionality where in users can create multiple customer at once specifying the count like 10,so it would create the tentative autoNos for customer making sure its not duplicate.
But there is setting which is autogenerate is true and is islocked is false which kinds of the breaks my code .
i am unable to set next autoNumber when this settings are enabled .
The user changes the autoNos as his wish and i am unable to determine whether that user entered
new value or he its system generated and thus unable to set the next autoNumber
It is not an answer. But I want give you a suggestion that you may use a static counter. This counter will keep on updating by all users and then on some event you may restart the counting after adding that counter in existing number of database. By this you may minimize the calls to database.

C# assigning a drop down list value to a session value

I have a drop down list which retrieves values from a session I declare as follows.
var autoData = autoRetriever.GetAutoDataByUserId(user.Id);
Session["AutoData"] = autoData;
I then use autoData to populate my drop down list with a “Name” and a int “Value” as indexed in the database. Name and value are both part of my table in the database.
What I am trying to do now is once a user selects from the drop down I am passing the value (e.g 0,1,2,3,n) the id on the db table to a method. OnSelected changed I want to use this value and retrieve another column “Path” from my session (autoData). Namely the path that is associated with the id. Once I have the correct “Path” associated with this value I can move forward with it.
With my OnSelectedIndexChanged method I then have the following. This string selectedAuto contains my value (0,1,2,3,n)
string selectedAuto = AutoDropDown.SelectedValue;
Does anyone have any advice on how I could move forward with this? If I have not explained enough please let me know and I will delve deeper into it.
As #David mentioned yes correct i am trying to use that value to identify that record. That is the part i am finding tricky. How can i retrieve that "Path" based on the value i select.

How to set a property value to auto increment in visual studio lightswitch 2012

i want to set a property value in a table to default auto increment,but their are no options to do so in lightswitch2012 to my knowledge which is given that i recently started learning lightswitch,very light.
ok heres the real problem,this is the table
[customer][id,customer_id,name]
i want to set customer_id by default to id unless it is manually changed to different value.
how to acheive this?
In the Entity designer make your Customer_ID not required.
Write Code for Customers_Inserted.
Then, check to see if the Customer_ID is null. If it is, copy the ID field to it.
private void Customers_Inserted(Customer entity)
{
if (entity.Customer_ID == null) {
entity.Customer_ID = entity.ID;
}
}
You're right, there is no "auto-increment" data type available in LightSwitch. The ID property auto-increments, but that's a special case, handled by LightSwitch.
If you were attaching to an external SQL database, if you added a column that was an Integer Identity column, although it'll just appear as an Integer property in LightSwitch, it would still auto-increment because that's actually done in the SQL database itself.
The problem with all auto-increment properties is that you won't get the actual value until the record is saved.
Can I ask why you need an auto-increment property?
I may be misunderstanding what you are trying to achieve, but if you are using either a table or a grid, and you want to set the values for various entities for each new row your user adds (like customer_id = id, etc.), you can use the _Changed method and Add event to programmatically set any of the new row entities.
If this is along the lines of what you're looking for, watch Beth Massi's video How Do I: Copy Data from One Row into a New Row? You should be able to adapt her code to accomplish what you have in mind I think.

a member that is computed or generated cannot be changed

i have a column in database that takes a bit value(1,0). the default is set to 1. I am using Linq. when I try to change the value it gives me this exception.
A member that is computed or generated cannot be changed.
in Linq
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_IsDefault", DbType="Bit NOT NULL",IsDbGenerated="true" )]
if I remove the IsDbGenerated Attribute. I am able to change the values but when I import some data directly using a CSV file then the default value is always coming 0 but it was set to 1 in the DB.
Can't you set the default value of the field or property in code to 1 (or true) as well? You could also set the default in the constructor of the object that the field belongs to.

Categories