Adding Annotation - dynamics-crm

I am trying to add a annotation via code and the annotation adds fine but it won't add the correct CreatedBy user. It just adds the annotation and sets the createdby user to the admin. This is what I am using:
Entity annotation = new Entity("annotation");
annotation["createdby"] = new EntityReference("systemuser", new Guid("2a213502-db00-e111-b263-001ec928e97f"));
annotation["objectid"] = new EntityReference("opportunity", opportunityId);
annotation["subject"] = "Lead Note";
annotation["notetext"] = p.Column_18;
Guid annotationId = lService.Create(annotation);
Do you have to do anything special to add the CreatedBy?
Thanks!

You cannot set createdby in Create or Update. See the description for the attribute createdby. It is not valid for Create or Update. The system will set this attribute to the user which is executing the current request.
You have to use impersonation to create a record in the name of another user. Therefore you have to set the CallerId property to the id of the user which identity you would like to impersonate. See Impersonate Using the ActOnBehalfOf Privilege

Related

Map lookup fields

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

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

Parse Android: update ParseObject containing an array of ParseUsers throws UserCannotBeAlteredWithoutSessionError

im working on an Android App.
I have a custom class which has relations with TWO ParseUsers and other fields. As suggested by the docs, I used an array (with key "usersArray") to store the pointers for the two ParseUsers, because I want to be able to use "include" to include the users when i query my custom class. I can create a new object and save it successfully.
//My custom parse class:
CustomObject customObject = new CustomObject();
ArrayList<ParseUser> users = new ArrayList<ParseUser>();
users.add(ParseUser.getCurrentUser());
users.add(anotherUser);
customObject.put("usersArray", users);
//I also store other variable which i would like to update later
customObject.put("otherVariable",false);
customObject.saveInBackground();
Also, i can query successfully with:
ParseQuery<CustomObject> query = CustomObject.getQuery();
query.whereEqualTo("usersArray", ParseUser.getCurrentUser());
query.whereEqualTo("usersArray", anotherUser);
query.include("usersArray");
query.findInBackground( .... );
My problem is when trying to UPDATE one of those CustomObject.
So after retrieving the CustomObject with the previous query, if I try to change the value of the "otherVariable" to true and save the object, I am getting a UserCannotBeAlteredWithoutSessionError or java.lang.IllegalArgumentException: Cannot save a ParseUser that is not authenticated exceptions.
CustomObject customObject = customObject.get(0); //From the query
customObject.put("otherVariable", true);
customObject.saveInBackground(); // EXCEPTION
I can see this is somehow related to the fact im trying to update an object which contains a pointer to a ParseUser. But im NOT modifying the user, i just want to update one of the fields of the CustomObject.
¿There is any way to solve this problem?
Maybe late but Parse users have ACL of public read and private write so you should do users.isAuthenticated() to check if its true or false.
If false then login with the user and retry. Note: you cannot edit info on two users at the same time without logging out and relogging in.
Another thing you can do is use Roles and define an admin role by using ACL which can write over all users.

Handle GUIDs in ASP.Net MVC 3

I have a bunch of tables in my database that have a column which is of type GUID. I do not want this to be a part of scaffolding when i generate my views, so i am using [ScaffoldColumn(false)]. I don't want the user to enter an actual GUID when "creating" a new entity. Is there any way to automate the process of automatically adding a new GUID (thru data annotations or some other way??) when a user creates a new Entity or do i have to modify the controller for every table that has a GUID so that every time a user creates a new entry i make sure i generate a new GUID before saving it?
Thanks.
This is where (IMHO) the new scaffolding features encourages bad code.
You should define separate ViewModels for your views. These contain only the fields you wish the user to change along with any view specific validation/rules.
Within your controller action, you can check whether the user input is valid (ModelState.IsValid) and if so, do some lefty-righty mapping between your ViewModel properties and those of your entity.
See ViewModel Best Practices for more details.
However, to answer your question, you can use the HiddenInput attribute on an Id field so that it is rendered as a <input type="hidden"...
With regard with setting the Id, create a base class for your entities e.g:
public abstract class BaseEntity {
public Guid Id {get;set;}
public BaseEntity() {
Id = Guid.NewGuid();
}
}
You can then inherit your entities from this class and when you call new MyEntity() the base class constructor will be invoked, thus setting the Id.

Auto-populate fields in email form in Dynamics CRM 2011

Imagine, you want to add an email to a case. The email form opens and the "To" field is auto-populated with the case's customer account.
I want to change the behavior in order to auto-populate the content of "To" with a custom property of the related case.
My first approach was to register a JavaScript for the OnLoad event of the form and let the script change the field. That would work, but I am wondering if there is a smarter way to achieve this. There is already some logic, which fills the "To" field. Is it possible to configure this existing feature?
Any hints are appreciated.
I do not believe that this specific scenario can be done more effectively than how you've already worked it out. I would've suggested looking at the data mappings (left-nav item when you pop open the relationship in the entity's customizations, same concept as discussed in this Dynamics CRM 4.0 article http://www.dynamicscare.com/blog/index.php/modifying-mapping-behavior-between-parent-child-records-in-microsoft-dynamics-crm/), but it does not appear to be applicable to this relationship.
This might help you:
DataService.EntityReference toEntityRef = new DataService.EntityReference();
toEntityRef.LogicalName = "contact";
toEntityRef.Id = Guid.Parse(to_id);
Entity toParty = new Entity();
toParty["partyid"] = toEntityRef;
toParty.LogicalName = "activityparty";
Entity emailEntity = new Entity();
emailEntity.LogicalName = "email";
EntityCollection toCollection = new EntityCollection();
toCollection.Entities = new ObservableCollection<Entity>();
toCollection.Entities.Add(toParty);
emailEntity["to"] = toCollection;
IOrganizationService soapService = ServerUtility.GetSoapService();
IAsyncResult res = soapService.BeginCreate(emailEntity, OnCreateComplete, soapService);
Call Back method:
private void OnCreateComplete(IAsyncResult result)
{
Guid emailId = (IOrganizationService)result.AsyncState).EndCreate(result);
}
Another approach would be to replace the Add Email buttons in the ribbon in order to call a custom JavaScript function. This function could open the mail window with window.open and initialize the To: field by setting the extraqs parameter to configure an ActivityParty for the email about to create. This can be done by setting:
partyid to the id of an allowed entity's instance
partyname to the name of the entity instance
partytype to 2 (for the recipent field, see here for details)
But the extraqs parameter is limited: You can set only one receiver and no other field (from, cc, bcc, ...). Moreover, replacing the buttons would bypass built-in functionality, which may change in future versions,
So I prefer the handling of the OnLoad event of the form.

Resources