Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new record with a specific owner without calling AssignRequest in CRM 2011

In our application, we create a few thousand phonecall records. Each phonecall should have a different owner, determined by a method named GetAnyAppropriateSystemUser(), which finds some random SystemUser based on some criteria.

In the code example below, we create a phonecall, and later use AssignRequest on it to specify its owner.

PhoneCall phoneCall = new PhoneCall();

// 
// stuff to set up the new PhoneCall instance here; populate fields, etc...
//

// determine this phonecall's owner through some algorithm
Guid appropriateOwner = GetAnyAppropriateSystemUser();

Guid createdPhoneCallId = _serviceProxy.Create(phoneCall);
if (createdPhoneCallId != Guid.Empty)
{
    AssignRequest phoneCallAssign = new AssignRequest();
    phoneCallAssign.Assignee = new EntityReference(SystemUser.EntityLogicalName, appropriateOwner);
    phoneCallAssign.Target = new EntityReference(PhoneCall.EntityLogicalName, createdPhoneCallId);
    _serviceProxy.Execute(phoneCallAssign);
}

This works allright, but there are two calls, one to create, and one to assign. Is it ok to just set "ownerid" of the PhoneCall record before calling Create() method, thus eliminating the need to call an AssignRequest later? It seems to work, and I even found an example doing a similar thing in the SDK, as shown below.

SDK Sample: Roll Up Goal Data for a Custom Period Against the Target Revenue

// Create three goals: one parent goal and two child goals.
Goal parentGoal = new Goal()
{
    Title = "Parent Goal Example",
    RollupOnlyFromChildGoals = true,
    ConsiderOnlyGoalOwnersRecords = true,
    TargetMoney = new Money(300.0M),
    IsFiscalPeriodGoal = false,
    MetricId = new EntityReference
    {
        Id = _metricId,
        LogicalName = Metric.EntityLogicalName
    },
    GoalOwnerId = new EntityReference
    {
        Id = _salesManagerId,
        LogicalName = SystemUser.EntityLogicalName
    },
    OwnerId = new EntityReference
    {
        Id = _salesManagerId,
        LogicalName = SystemUser.EntityLogicalName
    },
    GoalStartDate = DateTime.Today.AddDays(-1),
    GoalEndDate = DateTime.Today.AddDays(30)
};
_parentGoalId = _serviceProxy.Create(parentGoal);

Although it seems to work, are there anything that we must be aware of if we set ownerid before creating the new record? Are there any differences?

Thank you very much in advance.

like image 514
tdgtyugdyugdrugdr Avatar asked Apr 09 '13 08:04

tdgtyugdyugdrugdr


1 Answers

As you already found is allowed to set the ownerid when you create the record.

But is not possible to edit the owner of an existing record in the same way, in that case you must use the AssignRequest.

Check also this question: ETL Software, can't retrieve owner of a contact

like image 152
Guido Preite Avatar answered Sep 30 '22 11:09

Guido Preite