Pre-allocation of attributes by creating new entity - dynamics-crm-2011

While creating new entity in CRM, some fields will be pre-allocated. Exmaple: for a new contact - transactioncurrency and ownerid will be filled with values. Haw can I programmatically find out, which rules will be used for such pre-allocation. I can't call "initializefromrequest" to find it out, because I want to build offline DB and there apply the rules
thanks

Get the EntityMetadata for your entity then examine AttributeMetadata for each Attribute to determine if the IsCustomAttribute Property is true
Retrieve Entity metadata using EntityFilters = EntityFilters.Attributes
Iterate the Attribute collection of the EntityMetadata in the response
Check the IsCustomAttribute property on each AttributeMetadata

Related

Update existing entity

I am retrieving an Category entity through the DAL and change some values.
$randomId = 'an-existing-category-id';
$result = $this->categoryRepository->search((new Criteria())->addFilter(new EqualsAnyFilter('id', $ids)), Context::createDefaultContext());
$entity = $result->get($randomId);
$entity->setParentId('new-parent-id');
Instead of converting the entity to array and passing it to the DAL to upsert it i wonder if it is possible to "persist" the complete entity like in doctrine?
It is not possible, you can update a record using only update or upsert method of the entity repository.
But you don't need to convert the whole entity to an array, you just need to provide id and fields that should be updated (not all fields).

Find all resources (users) that has a schema extension property set to null

We have a scenario were we need to extend the existing schema in AAD for users to support our use case. As soon as a new user has been created (we do not control this process) we want to attach some additional information to the object. We also want to attach information to all the already existing user objects. We though the schema extension in the Graph API would solve this issue for us.
I've added a schema definition based on the tutorial here. Our extension got the name ext7sumrsqd_policies and have the following properties:
IsHandled (boolean)
SuggestedOwner (string)
After the extension was created I wanted to find all users that does not have the new "property" set (imagine a sync that is running on an intervall and want to check for new users that has not yet been handled).
Tried the following query first: https://graph.microsoft.com/v1.0/users?$filter=ext7sumrsqd_policies eq null
However the Graph API does not support filter with null.
Therefore tried to filter on any of the properties on the new extension:
https://graph.microsoft.com/v1.0/users?$filter=ext7sumrsqd_policies/IsHandled eq false
https://graph.microsoft.com/v1.0/users?$filter=ext7sumrsqd_policies/IsHandled eq true
However this filter never returns any user that has ext7sumrsqd_policies=null.
Are there any way you can filter the Graph API for resources that is currently missing a schema extension property or where the property is null?
As stated filtering by null is not supported. You might try creating a second schema extension such as ext7sumrsqd_hasPolicies that specifies if ext7sumrsqd_policies is null or not.

How can I retrieve the id of a document I added to a Cosmosdb collection?

I have a single collection into which I am inserting documents of different types. I use the type parameter to distinguish between different datatypes in the collection. When I am inserting a document, I have created an Id field for every document, but Cosmosdb has a built-in id field.
How can I insert a new document and retrieve the id of the created Document all in one query?
The CreateDocumentAsync method returns the created document so you should be able to get the document id.
Document created = await client.CreateDocumentAsync(collectionLink, order);
I think you just need to .getResource() method to get the create document obj.
Please refer to the java code:
DocumentClient documentClient = new DocumentClient(END_POINT,
MASTER_KEY, ConnectionPolicy.GetDefault(),
ConsistencyLevel.Session);
Document document = new Document();
document.set("name","aaa");
document = documentClient.createDocument("dbs/db/colls/coll",document,null,false).getResource();
System.out.println(document.toString());
//then do your business logic with the document.....
C# code:
Parent p = new Parent
{
FamilyName = "Andersen.1",
FirstName = "Andersen",
};
Document doc = client.CreateDocumentAsync("dbs/db/colls/coll",p,null).Result.Resource;
Console.WriteLine(doc);
Hope it helps you.
Sure, you could always fetch the id from creation method response in your favorite API as already shown in other answers. You may have reasons why you want to delegate key-assigning to DocumentDB, but to be frank, I don't see any good ones.
If inserted document would have no id set DocumentDB would generate a GUID for you. There wouldn't be any notable difference compared to simply generating a new GUID yourself and assign it into id-field before save. Self-assigning the identity would let you simplify your code a bit and also let you use the identity not only after persisting but also BEFORE. Which could simplify a lot of scenarios you may have or run into in future.
Also, note that you don't have to use GUIDs as as id and could use any unique value you already have. Since you mentioned you have and Id field (which by name, I assume to be a primary key) then you should consider reusing this instead introducing another set of keys.
Self-assigned non-Guid key is usually a better choice since it can be designed to match your data and application needs better than a GUID. For example, in addition to being just unique, it may also be a natural key, narrower, human-readable, ordered, etc.

How to get hidden field value in crm plugin?

I have a hidden field in crm form and setting value for hidden field on save button click using JavaScript.
I am trying to select the hidden filed value in c# plugin code on postcaseCreate event, but getting Key is not found in dictionary error, Can anyone tell me what I am missing here.
if (localContext.PluginExecutionContext.InputParameters.Contains("Target")
&& localContext.PluginExecutionContext.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parmameters.
caseEntityObj = (Entity)localContext.PluginExecutionContext.InputParameters["Target"];
string productIds = caseEntityObj.FormattedValues["my_hiddenfiedld"].ToString();
if (caseEntityObj == null) return;
}
Try to replace line
string productIds = caseEntityObj.FormattedValues["my_hiddenfiedld"].ToString();
with line
string productIds = caseEntityObj["my_hiddenfiedld"].ToString();
Why do you want to use FormattedValues? Is hidden field optionset? If yes and you need to get correspond text you will have to retrieve attribute using RetrieveAttribute message and get text of optionset from response.
The "Target" input parameter has only the attributes that were submitted to the framework. The system forms only submit attributes that contain changed data (or do not equal default values) as an optimization. If you created your own client UpdateRequest or CreateRequest and only submitted a few attributes, then your plugin's Target collection would only contain those few attributes as well.
In your case, I'm guessing that your hidden field isn't changing on an update and so it isn't included in your Target attribute collection.
If your plugin logic will always need to know the current value of a field regardless of whether it is included in the submitted attribute collection, you need to register a PreImage. Registering a PreImage tells CRM that you always need to know the current value of a certain field during this plugin. This is the value of the field before the current action.
Docs on PreImages: https://msdn.microsoft.com/en-us/library/gg309673.aspx#bkmk_preandpost
Pseudo code:
Use .Contains() to check Target attribute collection for attribute name.
If true, get value of attribute from Target attributes as this is the actual change just submitted by client.
If false, use .Contains() to check PreImage attribute collection for attribute name.
If true, get value of attribute from PreImage as this is the most recent value from the database.
Hope that helps!
Thanks Josh,Andril,
I created two steps in plugin code like postCaseCreate,PostCaseupdate event and one postImage and then I am getting value like below on postCase create event
string productIds = caseEntityObj["my_hiddenfiedld"].ToString();
For update getting the value from image. It is working fine. thanks a lot guys.
Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;
productIds = postImageEntity.Attributes["my_hiddenproducts"].ToString();

Core Data automatic migration doesn't create the new entity in my new model

I made a new version of my core data model which includes one new entity "Test" with one attribute "type".
The lightweight migration worked with no errors but I noticed that the new entity was not created in the database. When fetching this new entity "Test" , the result was nil. I had the attribute "type" set as non-optional with a default value of 1 because I expected that the migration will create the new entity with this default value . But it didn't. So my question is : is this expected behavior? In the case of core data migration, the new entities are not created but set as nil? Or if I am missing some steps , what happens if the new model has relationships between the new entity "Test" and the existing entities? Thanks.
You need to create an object of this entity, using for example -[NSEntityDescription insertNewObjectForEntityName:...]. Simply having the entity in the model will not create any objects.
You can do this at the end of migration, or lazily at startup (if you do a fetch for an object of this entity and can't find it).

Resources