> ## 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 an Appointment entity through an entity

> How to create an Appointment entity through another entity.

Creating an appointment through an entity can be done in 2 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;
using(SoSession mySession = SoSession.Authenticate("sam", "sam"))
{
  //Create an Appointment Entity
  Appointment newAppointment = Appointment.CreateNew();

  //Setting the Default values for the Appointment
  newAppointment.SetDefaults();

  //Assigning values to the individual properties of theAppointment Entity
  //Assigning basic properties to the Appointment
  newAppointment.Location = "Seminar Room 665";
  newAppointment.EndDate = new DateTime(2007, 3, 4);
  newAppointment.Status = SuperOffice.Data.AppointmentStatusNotStarted;
  newAppointment.Private = SuperOffice.Data.AppointmentPrivatePublic;
  newAppointment.HasAlarm = 1;
  newAppointment.Alarm = 5;

  //Assigning a Row type property to the Appointment Entity
  newAppointment.Associate = SuperOffice.CRM.Rows.AssociateRowGetFromIdxAssociateId(100);

  //Assigning an Entity type property to the Appointment
  newAppointment.Contact = Contact.GetFromIdxContactId(20);
  newAppointment.Person = Person.GetFromIdxPersonId(10);

  //Retrieving an Instance of another Entity
  Contact newContact = Contact.GetFromIdxContactId(10);

  //Assigning the Created Contact to the other Entity
  newContact.Appointments.Add(newAppointment);

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

In the example above, we have created an appointment as explained in [this example][1]. The difference is that here we do not save the created appointment. Instead, we assign it to the `Appointments` property of the `Contact` instance created and then save the `Contact`:

```csharp CS theme={null}
Contact newContact = Contact.GetFromIdxContactId(10);
newContact.Appointments.Add(newAppointment);
newContact.Save();
```

## Example 2

Below is an example of how we may use the `AddNew` method available in the `Appointments` property of the `Contact` class to create a new appointment.

```csharp CS theme={null}
using SuperOffice.CRM.Entities;
using SuperOffice;
using(SoSession mySession = SoSession.Authenticate("sam", "sam"))
{
  //Retrieving an Instance of another Entity
  Contact newContact = Contact.GetFromIdxContactId(10);

  //Create a new instance of the Appointment ot be added to the Collection
  newContact.Appointments.AddNew();

  //Set Default Values to the created Appointment
  newContact.Appointments[0].SetDefaults();

  //Assigning values to the individual properties of the Appointment Entity
  //Assigning basic properties to the Appointment
  newContact.Appointments[0].Location = "Seminar Room 664";
  newContact.Appointments[0].EndDate = new DateTime(2007, 3, 4);
  newContact.Appointments[0].Status = SuperOffice.Data.AppointmentStatus.NotStarted;
  newContact.Appointments[0].Private = SuperOffice.Data.AppointmentPrivate.Public;
  newContact.Appointments[0].HasAlarm = 1;
  newContact.Appointments[0].Alarm = 5;

  //Assigning a Row type property to the Appointment Entity
  newContact.Appointments[0].Associate = SuperOffice.CRM.Rows.AssociateRow.GetFromIdxAssociateId(100);

  //Assigning an Entity type property to the Appointment
  newContact.Appointments[0].Contact = Contact.GetFromIdxContactId(20);
  newContact.Appointments[0].Person = Person.GetFromIdxPersonId(10);

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

The difference between example 1 and example 2 is that here we have created an instance of the `Appointment` to be added to the `Appointments` collection and by indexing through the collection we have assigned the desired values for the specific appointment as shown below.

```csharp CS theme={null}
//Create a new instance of the Appointment ot be added to the Collection
newContact.Appointments[0].Location = "Seminar Room 664";
newContact.Appointments[0].Contact = Contact.GetFromIdxContactId(20);
```

The `Appointment` created will be saved when saving the `Contact` entity with the use of its `Save` method.

<Note>
  When adding an appointment by indexing through the Appointment collection of any entity, you should take caution to identify and add to the newly created `Appointment`, without updating an earlier `Appointment`.

  For instance, in example 2 if there was an appointment already existing in `Appointment[0]` location when you assigned values to properties what actually would happen is an update instead of an insert since the values that already exist in the 0th location will be modified.
</Note>

[1]: ./create-apt-entity


## Related topics

- [Create Appointment Entity From Existing](/en/api/reference/restful/agent/appointment_agent/create-appointment-entity-from-existing.md)
- [Create Default Appointment Entity](/en/api/reference/restful/agent/appointment_agent/create-default-appointment-entity.md)
- [Create Default Appointment Entity By Type](/en/api/reference/restful/agent/appointment_agent/create-default-appointment-entity-by-type.md)
- [Create Default Suggested Appointment Entity](/en/api/reference/restful/agent/appointment_agent/create-default-suggested-appointment-entity.md)
- [Post Appointment Entity](/en/api/reference/restful/rest/appointment/post-appointment-entity.md)
- [Save Appointment Entity](/en/api/reference/restful/agent/appointment_agent/save-appointment-entity.md)
- [Default Appointment Entity](/en/api/reference/restful/rest/appointment/default-appointment-entity.md)
