Frage

I'd like to update a field in an instance of an entity using a numeric value. All the examples I've seen (such as this one) are based on the paradigm of retrieving the instance, altering it and then storing in the DB. That's seems inefficient and the code looks nasty and unnecessarily long.

Is there a shorter way to update an entity?

I have the following data:

  • the name as String (one being lead, the other being *new_holder*)
  • the guid as Guid
  • the value as int
  • the field name as "String"

So, I've tried to start like so.

DynamicEntity entity = new DynamicEntity("new_holder");
TargetUpdateDynamic update = new TargetUpdateDynamic();
update.Entity=entity;

The problem is that I'm not certain how to act around the data to be stored.

  1. Should I Add() as properties? Or just write into the bag?

    entity.Properties.Add(...);  
    entity["field name"] = ...;
    
  2. How do I treat the identity? Do I really need to use that long syntax?

    Guid guid = ...;  
    Lookup lookup = new Lookup("new_holder", guid);
    LookupProperty lookupProperty = new LookupProperty("id", lookup);
    entity.Properties.Add(lookupProperty);
    
  3. Am I assuming the correct type when declaring the lookup?

    Lookup lookup1 = new Lookup("lead", guid1);  
    Lookup lookup2 = new Lookup("new_holder", guid2);
    
  4. Am I assuming the correct field name when declaring the lookup property? Or should I use new_holderid, holder_guid or anything else other than just id?

    LookupProperty lookupProperty = new LookupProperty("id", lookup);
    
War es hilfreich?

Lösung

You don't need to retrieve the entity first if you already have the Guid of the record.

I don't have a CRM 4.0 environment to test but:

1) If you want to assign a value to a property it is not necessary to check (and add) the property first, just write:

entity["field name"] = ...;

but I'm not sure if this works also for the primary id field

2) The identity isn't a Lookup type but a Key type, so if your entity is new_holder the id field is new_holderid:

entity["new_holderid"] = new Key(guid);

because I can't check if this syntax works, you can always write:

entity.Properties.Add(new KeyProperty("new_holderid", new Key(guid)));

3) If you want to assign an int you need to use CrmNumber:

entity["fieldname"] = new CrmNumber(5);

Full example:

DynamicEntity entity = new DynamicEntity("new_holder");
entity.Properties.Add(new KeyProperty("new_holderid", new Key(guid)));
entity["field name"] = new CrmNumber(5);
TargetUpdateDynamic update = new TargetUpdateDynamic();
update.Entity = entity;
UpdateRequest request = new UpdateRequest();
request.Target = update;
UpdateResponse response = (UpdateResponse)service.Execute(request);

As I wrote before I don't have a CRM 4.0 instance, but you can try the code and check which syntax works

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top