Accounts


List of examples:

Accounts

Creating an account




// Creates a new account object for the current site
AccountInfo newAccount = new AccountInfo()
{
    AccountName = "New Account",
    AccountSiteID = SiteContext.CurrentSiteID
};

// Saves the new account to the database
AccountInfoProvider.SetAccountInfo(newAccount);


> Back to list of examples

Updating accounts




// Gets all accounts from the current site whose name starts with 'New'
var accounts = AccountInfoProvider.GetAccounts()
                                            .WhereEquals("AccountSiteID", SiteContext.CurrentSiteID)
                                            .WhereStartsWith("AccountName", "New");

// Loops through individual accounts
foreach (AccountInfo account in accounts)
{
    // Updates the account's properties
    account.AccountName = account.AccountName.ToLowerCSafe();

    // Saves the modified account into the database
    AccountInfoProvider.SetAccountInfo(account);
}


> Back to list of examples

Adding contacts to an account




// Gets the 'New Account' account on the current site
AccountInfo account = AccountInfoProvider.GetAccounts()
                                            .WhereEquals("AccountSiteID", SiteContext.CurrentSiteID)
                                            .WhereEquals("AccountName", "New Account")
                                            .FirstObject;

if (account != null)
{
    // Gets all contacts whose last name is 'Smith'
    var contacts = ContactInfoProvider.GetContacts().WhereEquals("ContactLastName", "Smith");

    // Loops through individual contacts
    foreach (ContactInfo contact in contacts)
    {
        // Creates an object representing the relationship between the account and the contact
        AccountContactInfo accountContact = new AccountContactInfo()
        {
            AccountID = account.AccountID,
            ContactID = contact.ContactID
        };

        // Saves the account-contact relationship to the database
        AccountContactInfoProvider.SetAccountContactInfo(accountContact);
    }
}


> Back to list of examples

Removing contacts from an account




// Gets the 'New Account' account on the current site
AccountInfo account = AccountInfoProvider.GetAccounts()
                                            .WhereEquals("AccountSiteID", SiteContext.CurrentSiteID)
                                            .WhereEquals("AccountName", "New Account")
                                            .FirstObject;

if (account != null)
{
    // Gets the first contact whose last name is 'Smith'
    ContactInfo contact = ContactInfoProvider.GetContacts()
                                        .WhereEquals("ContactLastName", "Smith")
                                        .FirstObject;

    // Gets the relationship between the contact and the account
    AccountContactInfo accountContact = AccountContactInfoProvider.GetAccountContactInfo(account.AccountID, contact.ContactID);

    if (accountContact != null)
    {
        // Removes the contact from the account
        AccountContactInfoProvider.DeleteAccountContactInfo(accountContact);
    }
}


> Back to list of examples

Deleting an account




// Gets the 'New Account' account on the current site
AccountInfo deleteAccount = AccountInfoProvider.GetAccounts()
                                            .WhereEquals("AccountSiteID", SiteContext.CurrentSiteID)
                                            .WhereEquals("AccountName", "New Account")
                                            .FirstObject;

if (deleteAccount != null)
{
    // Deletes the account
    AccountInfoProvider.DeleteAccountInfo(deleteAccount);                   
}


> Back to list of examples

Account statuses

Creating an account status




// Creates a new account status object for the current site
AccountStatusInfo newStatus = new AccountStatusInfo()
{
    AccountStatusDisplayName = "New account status",
    AccountStatusName = "NewAccountStatus",
    AccountStatusSiteID = SiteContext.CurrentSiteID
};

// Saves the account status to the database
AccountStatusInfoProvider.SetAccountStatusInfo(newStatus);


> Back to list of examples

Updating an account status




// Gets the account status
AccountStatusInfo updateStatus = AccountStatusInfoProvider.GetAccountStatusInfo("NewAccountStatus", SiteContext.CurrentSiteName);

if (updateStatus != null)
{
    // Updates the account status properties
    updateStatus.AccountStatusDisplayName = updateStatus.AccountStatusDisplayName.ToLowerCSafe();

    // Saves the modified account status to the database
    AccountStatusInfoProvider.SetAccountStatusInfo(updateStatus);
}


> Back to list of examples

Updating multiple account statuses




// Gets all account statuses from the current site whose code name starts with 'New'
var statuses = AccountStatusInfoProvider.GetAccountStatuses()
                                            .WhereEquals("AccountStatusSiteID", SiteContext.CurrentSiteID)
                                            .WhereStartsWith("AccountStatusName","New");

// Loops through individual account statuses
foreach (AccountStatusInfo accountStatus in statuses)
{
    // Updates the account status properties
    accountStatus.AccountStatusDisplayName = accountStatus.AccountStatusDisplayName.ToUpper();

    // Saves the modified account status to the database
    AccountStatusInfoProvider.SetAccountStatusInfo(accountStatus);
}


> Back to list of examples

Assigning a status to an account




// Gets the 'New Account' account on the current site
AccountInfo account = AccountInfoProvider.GetAccounts()
                                            .WhereEquals("AccountSiteID", SiteContext.CurrentSiteID)
                                            .WhereEquals("AccountName", "New Account")
                                            .FirstObject;

// Gets the account status
AccountStatusInfo accountStatus = AccountStatusInfoProvider.GetAccountStatusInfo("NewAccountStatus", SiteContext.CurrentSiteName);

if ((account != null) && (accountStatus != null))
{
    // Checks that the account doesn't already have the given status
    if (account.AccountStatusID != accountStatus.AccountStatusID)
    {
        // Assigns the status to the account
        account.AccountStatusID = accountStatus.AccountStatusID;

        // Saves the updated account to the database
        AccountInfoProvider.SetAccountInfo(account);
    }
}


> Back to list of examples

Removing statuses from accounts




// Gets the account status
AccountStatusInfo accountStatus = AccountStatusInfoProvider.GetAccountStatusInfo("NewAccountStatus", SiteContext.CurrentSiteName);

if (accountStatus != null)
{
    // Gets all accounts from the current site that have the specified status
    var accounts = AccountInfoProvider.GetAccounts()
                                                .WhereEquals("AccountSiteID", SiteContext.CurrentSiteID)
                                                .WhereEquals("AccountStatusID", accountStatus.AccountStatusID);

    // Loops through the accounts
    foreach (AccountInfo account in accounts)
    {
        // Sets the status to 'None' for each account
        account.AccountStatusID = 0;

        // Saves the updated account to the database
        AccountInfoProvider.SetAccountInfo(account);
    }
}


> Back to list of examples

Deleting an account status




// Gets the account status
AccountStatusInfo deleteStatus = AccountStatusInfoProvider.GetAccountStatusInfo("NewAccountStatus", SiteContext.CurrentSiteName);

if (deleteStatus != null)
{
    // Deletes the account status
    AccountStatusInfoProvider.DeleteAccountStatusInfo(deleteStatus);
}


> Back to list of examples

Contact roles for accounts

Creating a contact role




// Creates a new contact role object
ContactRoleInfo newRole = new ContactRoleInfo()
{
    ContactRoleDisplayName = "New role",
    ContactRoleName = "NewContactRole",
    ContactRoleSiteID = SiteContext.CurrentSiteID
};

// Saves the contact role to the database
ContactRoleInfoProvider.SetContactRoleInfo(newRole);


> Back to list of examples

Updating a contact role




// Gets the contact role
ContactRoleInfo updateRole = ContactRoleInfoProvider.GetContactRoleInfo("NewContactRole", SiteContext.CurrentSiteName);

if (updateRole != null)
{
    // Updates the contact role properties
    updateRole.ContactRoleDisplayName = updateRole.ContactRoleDisplayName.ToLowerCSafe();

    // Saves the modified contact role to the database
    ContactRoleInfoProvider.SetContactRoleInfo(updateRole);
}


> Back to list of examples

Updating multiple contact roles




// Gets all contact roles from the current site whose code name starts with 'New'
var roles = ContactRoleInfoProvider.GetContactRoles()
                                                .WhereEquals("ContactRoleSiteID", SiteContext.CurrentSiteID)
                                                .WhereStartsWith("ContactRoleName", "New");

// Loops through individual contact roles
foreach (ContactRoleInfo role in roles)
{
    // Updates the contact role properties
    role.ContactRoleDisplayName = role.ContactRoleDisplayName.ToUpper();

    // Saves the updated contact role to the database
    ContactRoleInfoProvider.SetContactRoleInfo(role);
}


> Back to list of examples

Assigning a role to contacts in an account




// Gets the 'New Account' account on the current site
AccountInfo account = AccountInfoProvider.GetAccounts()
                                            .WhereEquals("AccountSiteID", SiteContext.CurrentSiteID)
                                            .WhereEquals("AccountName", "New Account")
                                            .FirstObject;

// Gets the contact role
ContactRoleInfo contactRole = ContactRoleInfoProvider.GetContactRoleInfo("NewContactRole", SiteContext.CurrentSiteName);

if ((account != null) && (contactRole != null))
{
    // Gets the relationships between the account and all of its contacts
    var accountContactRelationships = AccountContactInfoProvider.GetRelationships().WhereEquals("AccountID", account.AccountID);

    // Loops through the contact-account relationships
    foreach (AccountContactInfo accountContact in accountContactRelationships)
    {
        // Assigns the role to the contact for the specified account
        accountContact.ContactRoleID = contactRole.ContactRoleID;

        // Saves the account-contact relationship to the database
        AccountContactInfoProvider.SetAccountContactInfo(accountContact);
    }
}


> Back to list of examples

Deleting a contact role




// Gets the contact role
ContactRoleInfo deleteRole = ContactRoleInfoProvider.GetContactRoleInfo("NewContactRole", SiteContext.CurrentSiteName);

if (deleteRole != null)
{
    // Deletes the contact role
    ContactRoleInfoProvider.DeleteContactRoleInfo(deleteRole);
}


> Back to list of examples