Can I update the owner id of a Contact using LINQ? - 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);

Related

dynamics365 CRM populate form field with user login

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

eror when retrieve createdby field in crm using plugin

I try to retrieve the createdby field when I create a cases with a plugin, but the first retrieval fails, and the second and subsequent retrieval are successful. And then when I logged out and login with other user the first retrieval fails (retrieve result is the user before i change the user), and the second and subsequent retrieval are successful.
here is the code i write :
public void Execute(IServiceProvider serviceProv)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProv.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory servicefac = (IOrganizationServiceFactory)serviceProv.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = servicefac.CreateOrganizationService(context.UserId);
ITracingService trace = (ITracingService)serviceProv.GetService(typeof(ITracingService));
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity ent = (Entity)context.InputParameters["Target"];
if (ent.LogicalName != "incident")
return;
QueryExpression qe = new QueryExpression("incident");
string[] cols1 = { "createdby" };
qe.ColumnSet = new ColumnSet(true);
EntityCollection ec = service.RetrieveMultiple(qe);
foreach (Entity act in ec.Entities)
{
created = act. GetAttributeValue<EntityReference>("createdby").Name;
}
if (created == "CRM SNA")
{
created = string.Empty;
}
else
{
//here is the autonumber code
created = string.Empty;
}
}
}
What I want to make is an autonumber plugin, when cases are created by "CRM SNA" then the autonumber must not run, when cases are created by other users the autonumber will run.
How to make the first retrieve successful? and did not retrieve the user before?
thanks.
I assume your plugin runs in the Pre-Create step. CreatedBy and CreatedOn are not available in this step (probably because the record is not saved yet).
If you are just trying to get the user that executed the action that fired the plugin, use context.InitiatingUserId. You could also look into the documentation for the WhoAmI request.
Hope that helps!

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...
});
}

Creating a Salesforce Apex Trigger to update a lookup field in the Contacts object

I have created a trigger that will auto-create a contact when a specific Account record type is created or updated. The problem is that I am not sure how to populate the Contact 'Account Name' lookup field. This field is a lookup to the Account object. My code is below. Any help on how to integrate this missing component would be greatly appreciated.
trigger autoCreateContact on Account (after update, after insert)
{
List newContact = new List();
for (Account oAccount : trigger.new)
{
if (oAccount.RecordTypeid == '012F0000001MCfgIAG')
{
List<Contact> cCheck = [SELECT ID From Contact WHERE LastName=:oAccount.Name];
if(cCheck.isEmpty()==True)
{
System.debug(oAccount);
Contact oContact = new Contact();
oContact.LastName = oAccount.Name;
oContact.phone = oAccount.Phone;
oContact.email = oAccount.Email__c;
oContact.Owner = oAccount.Owner;
newContact.add(oContact);
}
}
if(newContact.isEmpty() == false)
{
Database.insert(newContact);
}
}
}
nice trigger I'm pretty sure you just need to add one line which is a reference to the account.id.
So if I were you I would add the link:
oContact.AccountID = oAccount.id;
NOTE: its not a good practice to have a SOQL inside the for loop.

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.

Resources