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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Add customeraddress to contact with CrmServiceClient

(0) ShareShare
ReportReport
Posted on by

Hello,

who has a working example of adding a customeraddress to a contact ?

I have a contact and want to add a customaddress to this contact using a plugin. ( CRM 2016 on premise )

What is the best way to do this ?

Thanks

Ulrich

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at
    RE: Add customeraddress to contact with CrmServiceClient

    Hi Ulrich,

    Why are you using a wrapper class?

    The code which James provided should give you the functionality that you are looking for, but I think you were looking to do this for customer address.

    Entity address = new Entity("customeraddress");

    Guid addressId = Guid.NewGuid();

    address["customeraddressid"] = addressId;

    address["parentid"] = contactGuid;

    address["addresstypecode"] = new OptionSetValue(2);

    address["addressnumber"] = ; // Use 3 or above

    address["line1"] = "Address Line 1";

    address["line2"] = "Address Line 2";

    address["line3"] = "Address Line 3";

    address["city"] = "City";

    address["stateorprovidence"] = "State/Province";

    address["postalcode"] = "Postal Code";

    context.AddObject(project);

    context.SaveChanges();

    return addressId;

    Hope this helps.

  • Community Member Profile Picture
    on at
    RE: Add customeraddress to contact with CrmServiceClient

    Hello James,

    thanks for your help. I am using the CrmClientService form the assembly Microsoft.Xrm.Tooling.

    I am using the following code

    Dictionary<string, CrmDataTypeWrapper> AddressData = new Dictionary<string, CrmDataTypeWrapper>();

    AddressData.Add("addresstypecode", new CrmDataTypeWrapper(AddressTypeCode, CrmFieldType.Picklist));

    AddressData.Add("parentid", new CrmDataTypeWrapper(new EntityReference("contact", contact.contactid), CrmFieldType.Lookup));

    service.CreateNewRecord("customeraddress", AddressData);

    I am getting an invalid cast error.

    Can you please take a look what i am doing wrong ?

    Thanks

    Ulrich

  • Suggested answer
    jestuder Profile Picture
    158 on at
    RE: Add customeraddress to contact with CrmServiceClient

    Late Bound:

    If using the 2 address fields on the contact form.  Replace address1 with address2 for second address.

    Entity contact = new Entity("contact");
    contact["contactid"] = contactGuid;
    contact["address1_line1"] = ;
    contact["address1_line2"] = ;
    contact["address1_line3"] = ;
    contact["address1_city"] = ;
    contact["address1_stateorprovidence"] = ;
    contact["address1_postalcode"] = ;
    crmservice.update(contact);

    If you are creating a new address in the customeraddress table and linking to the contact via a relationship.  Use the same style code I provided but create and new entity as customeraddress and change it to the attributes you need.  Then you will need to do a new EntityReference to set the contact its related too.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans