> ## Documentation Index
> Fetch the complete documentation index at: https://docs.superoffice.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Create a Contact entity through an entity

> How to create a Contact entity through an entity two ways.

Creating a contact through an entity can be done in two different ways:

* If you create an entity called A and assign it to an entity called B, when saving B - entity A will be saved through `NestedPersistent`

* you can create an entity that is a property of another entity. Then when saving the main entity, the entity created as the property of it will be saved as well.

Both these would give the same results.

## Example 1

```csharp CS theme={null}
using SuperOffice.CRM.Entities;
using SuperOffice.CRM.Rows;
using SuperOffice;
using(SoSession mySession = SoSession.Authenticate("sam", "sam"))
{
  //Create a Contact Entity
  Contact newContact = Contact.CreateNew();

  //Setting the Defaults for the Contact
  newContact.SetDefaults();

  //Assigning values for the individual properties of the Contact Entity
  //Assigning basic properties to a Contact
  newContact.Name = "EuroCenter";
  newContact.OrgNr = "1234523";
  newContact.Number1 = "7412885";

  //Adding a Row type property to a Contact Entity
  newContact.Country = new CountryRow.IdxCountryId(40);

  //Creating Email Rows
  EmailRow eMail1 = EmailRow.CreateNew();
  eMail1.EmailAddress = "Matt1@Fox.com";
  eMail1.Description = "Mathews first email";

  EmailRow eMail2 = EmailRow.CreateNew();
  eMail2.EmailAddress = "Matt2@Fox.com";
  eMail2.Description = "Mathews second email";

  //Adding the created Row types to the Properties of Rows type to the Contact Entity
  newContact.Emails.Add(eMail1);
  newContact.Emails.Add(eMail2);

  //Assigning values to Properties of Entity Collection Types.
  Sale newSale1 = Sale.GetFromIdxSaleId(10);
  Sale newSale2 = Sale.GetFromIdxSaleId(20);
  newContact.Sales.Add(newSale1);
  newContact.Sales.Add(newSale2);

  //Retrieving an Instance of another Entity
  Person newPerson = new Person.IdxPersonId(20);

  //Assign the Created Contact to the other Entity
  newPerson.Contact = newContact;

  //Saving the Updated Entity
  newPerson.Save();
}
```

In the example above, we create a new contact as explained in [this example][1]. The difference is that we do not save the created contact. Instead, we assign it to another entity such as the `Person` entity and then save the `Person`:

```csharp CS theme={null}
newPerson.Contact = newContact;
newPerson.Save();
```

## Example 2

Below is the example of creating a `Contact` entity, which is a property of another entity, such as `Person`.

```csharp CS theme={null}
using SuperOffice.CRM.Entities;
using SuperOffice.CRM.Rows;
using SuperOffice;
using(SoSession mySession = SoSession.Authenticate("sam", "sam"))
{
  //Create an Instance of another Entity
  Person newPerson = Person.CreateNew();
  newPerson.SetDefaults();

  //Assigning values for the individual properties of the ContactEntity of the Person Property
  //Assigning basic properties to a Contact Entity
  newPerson.Contact.Name = "EuroCenter";
  newPerson.Contact.OrgNr = "1234523";
  newPerson.Contact.Number1 = "7412885";

  //Creating Email Rows
  EmailRow eMail1 = EmailRow.CreateNew();
  eMail1.EmailAddress = "Anna1@Nicole.com";
  eMail1.Description = "Anna first email";

  EmailRow eMail2 = EmailRow.CreateNew();
  eMail2.EmailAddress = "Anna2@Nicole.com";
  eMail2.Description = "Anna second email";

  //Adding the created Row types to the Properties of Rows type to the Contact Entity Property of the Person Entity
  newPerson.Contact.Emails.Add(eMail1);
  newPerson.Contact.Emails.Add(eMail2);

  //Assigning values to Properties of Entity Collection Types.
  Sale newSale1 = Sale.GetFromIdxSaleId(10);
  Sale newSale2 = Sale.GetFromIdxSaleId(20);
  newPerson.Contact.Sales.Add(newSale1);
  newPerson.Contact.Sales.Add(newSale2);

  //Saving the Updated Entity
  newPerson.Save();
}
```

The difference between example 1 and example 2 is that here the properties of the `Contact` entity are accessed through the instance of the `Person` entity as shown below. However, the statements may vary a little depending on the data type.

```csharp CS theme={null}
using SuperOffice.CRM.Entities;
using SuperOffice.CRM.Rows;
using SuperOffice;
using(SoSession mySession = SoSession.Authenticate("sam", "sam"))
{
  //Create an Instance of another Entity
  Person newPerson = Person.CreateNew();
  newPerson.SetDefaults();

  //Assigning values for the individual properties of the ContactEntity of the Person Property
  //Assigning basic properties to a Contact Entity
  newPerson.Contact.Name = "EuroCenter";
  newPerson.Contact.OrgNr = "1234523";
  newPerson.Contact.Number1 = "7412885";

  //Creating Email Rows
  EmailRow eMail1 = EmailRow.CreateNew();
  eMail1.EmailAddress = "Anna1@Nicole.com";
  eMail1.Description = "Anna first email";

  EmailRow eMail2 = EmailRow.CreateNew();
  eMail2.EmailAddress = "Anna2@Nicole.com";
  eMail2.Description = "Anna second email";

  //Adding the created Row types to the Properties of Rows type to the Contact Entity Property of the Person Entity
  newPerson.Contact.Emails.Add(eMail1);
  newPerson.Contact.Emails.Add(eMail2);

  //Assigning values to Properties of Entity Collection Types.
  Sale newSale1 = Sale.GetFromIdxSaleId(10);
  Sale newSale2 = Sale.GetFromIdxSaleId(20);
  newPerson.Contact.Sales.Add(newSale1);
  newPerson.Contact.Sales.Add(newSale2);

  //Saving the Updated Entity
  newPerson.Save();
}
```

When saving the `Person` entity with the use of the `Save` method the new `Contact` is also saved NestedPersistent).

<Note>
  When using the above type of code, the main entity (such as the `Person` entity above) should also be created. When retrieving a person, it may already have a contact assigned to it. So therefore when adding values to the properties of the `Contact` in such an entity, what we would actually be doing is updating the existing `Contact` information.
</Note>

[1]: ./create-contact-entity


## Related topics

- [Create Default Contact Entity](/en/api/reference/restful/agent/contact_agent/create-default-contact-entity.md)
- [Create Default Contact Relation Entity](/en/api/reference/restful/agent/relation_agent/create-default-contact-relation-entity.md)
- [Post Contact Entity](/en/api/reference/restful/rest/contact/post-contact-entity.md)
- [Save Contact Entity](/en/api/reference/restful/agent/contact_agent/save-contact-entity.md)
- [Default Contact Entity](/en/api/reference/restful/rest/contact/default-contact-entity.md)
- [NSContactEntity](/en/automation/crmscript/reference/CRMScript.NetServer.NSContactEntity.md)
- [Create Default Appointment Entity](/en/api/reference/restful/agent/appointment_agent/create-default-appointment-entity.md)
