web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

ObjectContext.SaveChanges Method saves all field values and kicks off workflow

(0) ShareShare
ReportReport
Posted on by 1,577

Hello,

I am running an SSIS package to import CRM contacts. As part of the import, if I find contacts which already exist, I update one field in the contact entity. It is an option set field. I then use the UpdateObject and SaveChanges method (System.Data.Objects).

This works fine, but we have a workflow which is meant to run when a specific contact field changes. The field to kick start the workflow is not changed or even referenced in the import, but the workflow runs on every record affected by the import.

I could be wrong but I am assuming that the SaveChanges method I am using actually re-writes all contact field values and that is why the workflow is starting when it should not be.

Is there any way to only save the single field which I update as part of the import - thereby preventing the workflow to trigger erroneously? 

I hope this makes sense. Thanks.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Royal King Profile Picture
    27,686 on at

    you are right. SAve changes will save all the fields populated in the object instance. In order to avoid updating all the fields you have to set only fields that you want to update not everything.

  • Seamus Profile Picture
    1,577 on at

    Thanks for that, I want to do exactly what you suggest but I don't know how. Could you advise how I just save one field and not all, please.

  • Verified answer
    Community Member Profile Picture
    on at

    Hi

    I think you need to use an intermediate object for update your found contact.

    create new object and fill the id and the field to update, like this:

    var objectToUpdate = new Entity("contact");

    objectToUpdate.id = foundContact.id;

    objectToUpdate["FieldName"] = value;

    UpdateObject(objectToUpdate);

    Hope this help!

  • Royal King Profile Picture
    27,686 on at

    You have 2 options either remove the attributes from the object before saving or create new object with attributes that you want to update and call save changes method on the new object.

  • Seamus Profile Picture
    1,577 on at

    thanks...I think I understand...can 2 contact entities share the same id?

    or maybe that is not what you are suggesting. how else do I link the intermediate object to the target contact?

  • Suggested answer
    Community Member Profile Picture
    on at

    What you want to do is to new up a new Entity after you get the query results, set the ID, and only set the fields you want to change,Then call service.Update() with that new entity.

  • Daniel Wikell Profile Picture
    2,360 on at

    Two contacts can't share the same id no. The context would complain that it is already tracking an object with the same id if you tried adding the second entity (with only the select fields in it) to the context.

    Insterad of the context, using the orgservice update method that yessoufy suggested will work around that problem and let you do exactly what you want.

  • Seamus Profile Picture
    1,577 on at

    Thanks everyone - so just to clarify - the orgservice.update method will save just the updated field and not change my (different) workflow field -  therefore will not trigger my workflow?

  • Verified answer
    Daniel Wikell Profile Picture
    2,360 on at

    Not quite, the orgservice.update method would also save all fields if you pass it the entire object. The thing with this method is that it allows you to create a separate object and just set the fields that you want to be updated.

    So if you find an existing contact, you want to create a new contact object and set the id to the same as the one you found, add the fields coming from the import and then perform the orgservice.update call.

  • Seamus Profile Picture
    1,577 on at

    Thanks Daniel - I have implemented this now and it is working perfectly. Sorry I am being slow on the uptake here but I have one last confusion/question: I am setting the id of the new contact object to be the same as the existing contact id, but 2 contacts can't share the same id? Is it actually just that I am in effect "borrowing" the id to make an attribute update but not actually saving a new contact (at which stage id conflicts would) cause problems?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans