dynamics365 CRM populate form field with user login - dynamics-crm

I am new to dynamics 365 CRM. I created a form that the user will fill, it contains a lookup field that mapped to the User entity. I want to populate this field automatically depending on the user login. I have tried the business role but it didn't work. I have followed these links:
CRM Auto Populate Field From Option Set.
Auto Populate lookupfield
Dynamics CRM auto populate lookup values from custom field
the Code I am using is :
var userSetting = Xrm.Utility.getGlobalContext().userSettings;
var currentUser = new Array();
currentUser[0] = new Object();
currentUser[0].id = userSetting.userId;
currentUser[0].Name = userSetting.userName;
currentUser[0].entityType = "systemuser";
executionContext.getFormContext().getAttribute("student").setValue(currentUser);

First, retrieve logged in user settings with
var userSettings = Xrm.Utility.getGlobalContext().userSettings
Now, you should have access to the ID, Name, and Type(systemuser) of the record you want to set in your lookup field.
var currentUser = new Array();
var currentUserObject = new Object();
currentUserObject.id = userSettings.userId;
currentUserObject.name = userSettings.userName;
currentUserObject.entityType = "systemuser"
currentUser[0] = currentUserObject;
formContext.getAttribute("fieldname").setValue(currentUser);
From getGlobalContext.userSettings

Related

Automatically map a Contact to an Account

I want to add a field to Accounts which shows the email domain for that account e.g. #BT.com. I then have a spreadsheet which lists all the Accounts and their email domains. What I want to do is when a new Contact is added to Dynamics that it checks the spreadsheet for the same email domain (obviously without the contacts name in the email) and then assigned the Contact to the Account linked to that domain. Any idea how I would do this. Thanks
Probably best chance would be to develop CRM plugin. Register your plugin to be invoked when on after contact is created or updated (so called post-event phase). And in your plugin update the parentaccountid property of the contact entity to point to account of your choice.
Code-wise it goes something like (disclaimer: not tested):
// IPluginExecutionContext context = null;
// IOrganizationService organizationService = null;
var contact = (Entity)context.InputParameters["Target"];
var email = organizationService.Retrieve("contact", contact.Id, new ColumnSet("emailaddress1")).GetAttributeValue<string>("emailaddress1");
string host;
try
{
var address = new MailAddress(email);
host = address.Host;
}
catch
{
return;
}
var query = new QueryExpression("account");
query.TopCount = 1;
// or whatever the name of email domain field on account is
query.Criteria.AddCondition("emailaddress1", ConditionOperator.Contains, "#" + host);
var entities = organizationService.RetrieveMultiple(query).Entities;
if (entities.Count != 0)
{
contact["parentaccountid"] = entities[0].ToEntityReference();
}
organizationService.Update(contact);
I took Ondrej's code and cleaned it up a bit, re-factored for pre-operation. I also updated the logic to only match active account records and moved the query inside the try/catch. I am unfamiliar with the MailAddress object, I personally would just use string mapping logic.
var target = (Entity)context.InputParameters["Target"];
try
{
string host = new MailAddress(target.emailaddress1).Host;
var query = new QueryExpression("account");
query.TopCount = 1;
// or whatever the name of email domain field on account is
query.Criteria.AddCondition("emailaddress1", ConditionOperator.Contains, "#" + host);
query.Criteria.AddCondition("statecode", ConditionOperator.Equals, 0); //Active records only
var entities = organizationService.RetrieveMultiple(query).Entities;
if (entities.Count != 0)
{
target["parentaccountid"] = entities[0].ToEntityReference();
}
}
catch
{
//Log error
}

Can Dynamics Crm PartyList store an emailaddress

I have fields in activity form for email. It contains "to, cc and bcc" fields that are all fields of the type PartyList
The question is: Can I only store entity values like contact or account or can I also just store a email address which is not associated to any contact or account in the system?
Here is a picture explaining what I'm trying to achieve
As per this article it appears that the answer is yes via the addressUsed field.
Entity email = _sdk.Retrieve("email", emailId, new ColumnSet("to"));
EntityCollection to = email.GetAttributeValue<EntityCollection>("to");
if (to != null)
{
to.Entities.ToList().ForEach(party =>
{
EntityReference partyId = party.GetAttributeValue<EntityReference>("partyid");
bool isDeleted = party.GetAttributeValue<bool>("ispartydeleted");
string addressUsed = party.GetAttributeValue<string>("addressused");
// Do something...
});
}

Update Message (CrmService) Dynamics 4.0

I want to update the fields like "cs_hv_additionnalparticularities", "cs_hv_smscope" and so on, but the function srv.Update(de);updates all the form records, I mean it triggers a workflow that I don't want it to happen.
here is my code:
// Retrieve the DynamicEntity that goes with target
RetrieveRequest retrieve = new RetrieveRequest();
retrieve.Target = target;
retrieve.ColumnSet = new AllColumns();
retrieve.ReturnDynamicEntities = true;
// Create a response reference and execute the retrieve request.
RetrieveResponse response1 = (RetrieveResponse)srv.Execute(retrieve);
DynamicEntity de = (DynamicEntity)response1.BusinessEntity;
if (opp.Properties.Contains("cs_hv_additionnalparticularities"))
de["cs_hv_additionnalparticularities"] = opp["cs_hv_additionnalparticularities"];
if (opp.Properties.Contains("cs_hv_smscope"))
de["cs_hv_smscope"] = opp["cs_hv_smscope"];
if (opp.Properties.Contains("cs_hv_ugscope"))
de["cs_hv_ugscope"] = opp["cs_hv_ugscope"];
if (opp.Properties.Contains("cs_hv_acdc"))
de["cs_hv_acdc"] = opp["cs_hv_acdc"];
if (opp.Properties.Contains("cs_hv_smmv"))
de["cs_hv_smmv"] = opp["cs_hv_smmv"];
if (opp.Properties.Contains("cs_hv_smhv"))
de["cs_hv_smhv"] = opp["cs_hv_smhv"];
if (opp.Properties.Contains("cs_hv_ughv"))
de["cs_hv_ughv"] = opp["cs_hv_ughv"];
if (opp.Properties.Contains("cs_hvid"))
de["cs_hvid"] = opp["cs_hvid"];
de["cs_generercable"] = new CrmBoolean(true);
srv.Update(de);
I don't want to use this function srv.Update(de); to update the fields.
Can somebody please give me the update function code that can do this work ??
If you don't want to update additional columns, just retrieve the specific columns (property ColumnSet) instead of the new AllColumns().
You can use only the update the record with the Update method, make sure that your workflow triggers only for the requested fields, and not for additional fields.

Can I update the owner id of a Contact using LINQ?

I'm using CRM 2011, and attempting to update the OwnerId of contact using this code:
var crmContext = new CustomCrmContext(service);
var contact = crmContext.Contact.FirstOrDefault(c=>c.Id == id);
contact.OwnerId.Id= newOwnerId;
crmContext.UpdateObject(contact);
crmContext.SaveChanges();
I don't get any errors, however, the ownerId never updates in the database. I am able to update other attributes, but I'm just wondering if maybe the OwnerId is special and you have to use OrganizationRequest("Assign")? If so, where is this documented so I know what other attributes I cannot update?
The owner of a record cannot be modified with an update. You have to send a AssignRequest instead.
// Create the Request Object and Set the Request Object's Properties
var request = new AssignRequest
{
Assignee = new EntityReference(SystemUser.EntityLogicalName, _newOwnerId),
Target = new EntityReference(Account.EntityLogicalName, _accountId)
};
// Execute the Request
_service.Execute(request);

selected Picklist value not saving in dynamic CRM?

I have added javascript for reverse the items in the picklist (rating) to the opportunity entity. It is done. but when I am filling the data and saving it, it is not saving the selected item from the rating picklist to the database. What do I have to do?
var oField = crmForm.all.opportunityratingcode;
var items = oField.options.length;
var arrTexts = new Array(items);
var arrValues = new Array(items);
for(i=0;i<items;i++)
{
arrTexts[i]=oField.Options[i].Text;
arrValues [i]=oField.Options[i].DataValue;
}
for(i=0;i<=items;i++)
{
oField.DeleteOption(i);
}
for(j=items;j>0;j--)
{
var oOption1 =oField.Options;
oOption1.Text=arrTexts[j-1];
oOption1.DataValue= arrValues [j-1];
oField.AddOption(oOption1.Text,oOption1.DataValue);
alert(oOption1.DataValue);
}
Sounds like you need to add a .ForceSubmit in the onSave of the form. This forces CRM to save attribute data changes that you have made with JavaScript.
e.g.
crmForm.all.attribName.ForceSubmit = true;
Check the CRM SDK here: http://technet.microsoft.com/en-us/library/cc189831.aspx

Resources