Map lookup fields - dynamics-crm

I am new to CRM and I need your help. I have a button "Clone" that is shown when a record is selected. When I click that button it opens the quick create form.
I am retrieving the data for the selected record via Web api. Now, what I need to do is automatically fill all the fields in quick create form. enter image description here
My question is how to map the lookup field (name)?

Lookup Field has 3 properties, ID,Name and Entity Schema name. In below code
ID of user, Full name (primamry column), and Entity is systemuser (i.e user entity).
// Set lookup column
formParameters["preferredsystemuserid"] = "3493e403-fc0c-eb11-a813-002248e258e0"; // ID of the user.
formParameters["preferredsystemuseridname"] = "Admin user"; // Name of the user.
formParameters["preferredsystemuseridtype"] = "systemuser"; // Table name.
https://learn.microsoft.com/en-us/power-apps/developer/model-driven-apps/clientapi/reference/xrm-navigation/openform#example-2-open-a-form-for-new-record

Related

Remove user while also removing unused tags in many-to-many relationship in GraphQL?

I'm using Hasura for this.
Say for example I have a users, tags and users_tags table in a many-to-many relationship.
User's can define multiple tags (like a persons hobbies) and each tag can belong to multiple people.
Table users {
id int [pk increment]
}
Table tags {
id int [pk increment]
name text
}
Table users_tags {
id int [pk increment]
user [ref: - user.id]
tag [ref: - tags.id]
}
The users_tags table is a many-to-many table.
When a new user is created, I can create new tags and associate them with users just fine.
But when a user is deleted, what GraphQL mutation can I use to remove tags from both tags and users_tags if they don't point to other users.
This depends on your desired data model. If a tag is always associated with one user only, you can get rid of the users_tags table and add the user to the tags table.
Then, if you've set up a foreign key on the tag table that references the id field on the user table then this can be done automatically.
You will be given 4 options on deletion:
restrict: Does not allow you to delete a user that is referenced on a tag
cascade: Deletes the tag if the user is deleted
set null: Sets this row as null if the user is deleted
set default: Sets this row as the default value if the user is deleted
You'll want to select the cascade option here.
If you want to keep your current data model you will be able to do this cascading on the users_tags table if the tag or user is deleted but will need some process in place to delete tags that have no users associated to them.
The easiest way would probably be to add a custom SQL function but you could also have the following graphql query to find the tags that have 0 users_tags associated with them and then provide a list of primary keys to a delete tags mutation.
query CountOfUsersTagsForTag {
tags {
id
users_tags_aggregate {
aggregate {
count
}
}
}
}

How to pass an EntityReference to add attribute value on a lookup field in Microsoft Dynamics 365 CRM

Entity contact = new Entity("contact");
contact.Attributes.Add("fullname", "h api test");
contact.Attributes.Add("emailaddress1", "hh#devenv1.local");
contact.Attributes.Add("telephone1", "1");
contact.Attributes["parentcusotmerid"] = new EntityReference("Organization", );
Guid contactId = m_OrgServ.Create(contact);
Console.WriteLine(contactId);
The lookup field I want to set
The logicalname of the lookup field is parentcusotmerid, and
m_OrgSerc.create
is basically
Service.create
I am setting attribute values for the fields, it works fine for normal text boxes where I am entering values, however for lookup values it doesn't work. I know lookup fields are of type EntityReference, so I need to know the LogicalName of the entity the lookup is pointing and the Id of the record.
I have tried it but its asking for the GUID of the Organization field now, so I'm not sure if I am going about it the right way?
You cannot set "parentcustomerid" to organization. It's special reference field that takes either Account or Contact entity reference as parameter.
If you want to set it you go like this
contact.Attributes["parentcusotmerid"] = new EntityReference("account", Guid.NewGuid());
or
contact.Attributes["parentcusotmerid"] = new EntityReference("contact", Guid.NewGuid());
where Guid.NewGuid() is Guid of your Account or Contact that you want to reference

how to select a user id and merge the updated user inputs in respective fields

i have a controller with #SessionAttributes("user_email") now i wish to do a merge operation for this particular user_email in session
i have a dao implementation class which performs merge operation as follows:
#Override
public boolean mergeusers(Users users) {
session.getCurrentSession().merge(users); //saves or updates the lawyer details //
return true;
}
but even though i give session.getcurrentSession it merges the new user input as a new user in the database instead of merging/updating the fiel that matches the session's user_email
im new to this could someone please tell me what im doing wrong
two options
populate the Id of the user before merging or
find the user from the database (findById) and update the data and merge it

Retrieve Customer Entity in Dynamics CRM 2016

As we know Dynamics CRM has a specific attribute value: Customer. This value combines the Client and Account entity, but I'm blind or MSDN doesn't have specification about retrieving this field in query.
For example:
QueryByAttribute query = new QueryByAttribute(entName);
query.ColumnSet = new ColumnSet(new String[] { searchAttr });
query.Attributes.Add(searchAttr);
query.Values.Add(searchValue);
EntityCollection retrived = service.RetrieveMultiple(query);
This code accepts entity name and searches the attribute's name and value, but when I run it I don't know which type of entity I get from my DataSouce: Client or Account.
So the question is: is it possible to retrieve Customer entity in one query?
No, you must first know which entity you are trying to retrieve.
Get the value held within the Customer field as an EntityReference:
var customer = entity.GetAttributeValue<EntityReference>("customerid");
Get the LogicalName of the EntityReference:
var customerEntity = customer.LogicalName;

How to manage new Breeze entities which are aggregate roots?

I have a domain model which has a Customer, which in turn has 1 Address (1:1) and 1 or more Phone numers (1:M).
Customer has user supplied PK (a string), while Address and Phone use identity column (server generated).
I am struggling in trying to understand how to manage Breeze entity creation for a "Add new Customer" screen.
The form on the screen allows user to enter Customer, Address, and Phone data.
I am using Durandal and Knockout so my "customeradd.js" viewmodel looks something like this:
// -- snip ---
var customer = ko.observable(),
hasChanges = ko.computed(function () {
return datacontext.hasChanges();
});
var vm = {
hasChanges: hasChanges,
customer: customer,
activate: activate
};
return vm;
function activate() {
customer(datacontext.createCustomer());
}
// -- snip ---
and my "/services/datacontext.js" :
// -- snip ---
breeze.NamingConvention.camelCase.setAsDefault();
var manager = new breeze.EntityManager(config.remoteServiceName);
var hasChanges = ko.observable(false);
manager.hasChangesChanged.subscribe(function (eventArgs) {
hasChanges(eventArgs.hasChanges);
});
function createVehicle() {
return manager.createEntity("Customer");
}
// -- snip ---
My questions are following:
Once I create a Customer, do I need to create Address and list of Phones and add them to Customer entity before making it a KO observable? Or is this done automatically by createEntity() method?
How do I create a Customer but without having to specify the Id? If I set the key to null or '', Breeze complains ("Error: Cannot attach an object to an EntityManager without first setting its key or setting its entityType 'AutoGeneratedKeyType' property to something other than 'None'"). However, if I generate the temp key (using either breeze.core.getUuid() or something else), then it shows up in my Id form field, and I really want the end user to specify it....Do I have to resort to extending the entity with extra field and then do the swapping and validation before saving (I don't like this idea at all)? Is there a better way?
In order to enable/disable buttons on my form I am tracking if there are changes in EntityManager. But every time entity is created, it is automatically in 'added' state so hasChanges is true. What I want is for changes to be picked up only if user edits the form (and therefore makes changes to underlaying entity). What is the best way to approach this?
BTW, I have seen this recommendation to register custom constructor for entity (I have already implemented it but I am still not clear how to let user supply their own id and to flag entity as modified only when user edits it...)
I realize this has been up for a while, but here are my thoughts (in case anyone comes looking).
If you use the entityManager to create your customerl and everything is specified correctly in the metadata, you can just create the customer and add phone numbers/addresses as needed. Breeze automatically makes an entity's properties observable (if specified correctly and if breeze knows that KO is being used)
If you can only do it the way that you say, then you are stuck. Ideally, you would have a user-entered ID which is NOT the key (though you could still force it to be unique) and a database-generated key, which Breeze will manage behind the scenes (assigning a negative key until it is saved to the data store, then updating the key and all related keys without any input from you).
if you use the 2nd approach for answer 2, then your buttons can easily be enabled and disabled using ko data-binding. When you create the entity, save its value to the viewmodel (custSource). Then you can add to the save button the data-bind="disable: custSource == Customer(), enable: custSource != Customer()". (You might need to play around with the syntax -- I haven't tested that part yet)
I don't think you need a custom constructor unless you are doing something different from what I understand.
PS. you should be aware that I believe Breeze wants Knockout defined as 'ko', while Durandal definitely expects it to be 'knockout', so you will probably need a 'map' property in your require.config
I think you could solve some of your problems by taking a slightly different approach to your entity creation. Here's your current approach:
Create a customer entity
User modifies that entity
Save the changes
Instead, try this:
User enters customer information
Create and save the customer entity
I realize that this doesn't really answer your questions, but I've found the second approach to be much easier to implement. Just have the user enter all the information you need to create a customer, and then supply those values to createEntity.
customeradd.js
// -- snip ---
var vm = {
customerId: ko.observable(),
address: ko.observable(""),
phoneNumbers: ko.observableArray([]),
submit: submit
};
return vm;
function submit() {
datacontext.createCustomer(
vm.customerId(),
vm.address(),
vm.phoneNumbers());
}
// -- snip ---
/services/datacontext.js
// -- snip ---
/**
* Creates a new customer.
* #param {int} id - The customer's id number.
* #param {string} address - The customer's address.
* #param {string[]} phoneNumbers - An array of the customer's phone numbers.
*/
function createCustomer(id, address, phoneNumbers) {
return manager.createEntity("Customer",
{
id: id,
address: address,
phoneNumber: phoneNumbers
});
}
// -- snip ---

Resources