Create an Opportunity linked to an Account
You can create an Opportunity object and link it to a new or existing Account object. If linked with an Account, the History section of the Account form displays details of the Opportunity and the Link To field on the Opportunity form displays the Account name. When you create an Opportunity programmatically, you must reload the Opportunity object before the Account link will appear on the Opportunity form.
The following C# and Visual Basic for Applications (VBA) examples show how to create a new Opportunity and link it to a new Account object.
C#
private void CreateOpportunityWithAccount()
{
Outlook.ApplicationClass _app = new Outlook.ApplicationClass();
Outlook.Application olApp = (Outlook.Application)_app;
Outlook.NameSpace olNameSpace = _app.GetNamespace("MAPI");
Outlook.Folders folders = olNameSpace.Session.Folders;
Outlook.Folder bcmRootFolder = (Outlook.Folder)folders["Business Contact Manager"];
Outlook.Folder accounts = (Outlook.Folder)bcmRootFolder.Folders["Accounts"];
Outlook.ContactItem newAccount =
(Outlook.ContactItem)accounts.Items.Add("IPM.Contact.BCM.Account");
newAccount.FullName = "Wide World Importers";
newAccount.FileAs = "WWImporters";
newAccount.Save();
string accountEntryID = newAccount.EntryID;
Outlook.Folder opportunities = (Outlook.Folder)bcmRootFolder.Folders["Opportunities"];
Outlook.TaskItem newOpportunity =
(Outlook.TaskItem)opportunities.Items.Add("IPM.Task.BCM.Opportunity");
newOpportunity.Subject = "Sales Opp with Wide World Importers";
if (newOpportunity.UserProperties["Parent Entity EntryID"] == null)
{
Outlook.UserProperty userProp = newOpportunity.UserProperties.Add("Parent Entity EntryID", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false, false);
userProp.Value = accountEntryID;
}
newOpportunity.Save();
}
VBA
Sub CreateOpportunityWithAccount()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim bcmRootFolder As Outlook.Folder
Dim olFolders As Outlook.Folders
Dim bcmAccountsFldr As Outlook.Folder
Dim bcmOppFolder As Outlook.Folder
Dim newAcct As Outlook.ContactItem
Dim newOpportunity As Outlook.TaskItem
Set olApp = CreateObject("Outlook.Application")
Set objNS = olApp.GetNamespace("MAPI")
Set olFolders = objNS.Session.Folders
Set bcmRootFolder = olFolders("Business Contact Manager")
Set bcmAccountsFldr = bcmRootFolder.Folders("Accounts")
Set bcmOppFolder = bcmRootFolder.Folders("Opportunities")
Set newAcct = bcmAccountsFldr.Items.Add("IPM.Contact.BCM.Account")
newAcct.FullName = "Wide World Importers"
newAcct.FileAs = "Wide World Importers"
newAcct.Email1Address = "someone@example.com"
newAcct.Save
Set newOpportunity = bcmOppFolder.Items.Add("IPM.Task.BCM.Opportunity")
newOpportunity.Subject = "Opportunity For Wide World Importers to enter into Retail Field"
If (newOpportunity.UserProperties("Parent Entity EntryID") Is Nothing) Then
Set userProp = newOpportunity.UserProperties.Add("Parent Entity EntryID", olText, False, False)
userProp.Value = newAcct.EntryID
End If
newOpportunity.Save
Set newOpportunity = Nothing
Set newAcct = Nothing
Set bcmOppFolder = Nothing
Set bcmAccountsFldr = Nothing
Set bcmRootFolder = Nothing
Set olFolders = Nothing
Set objNS = Nothing
Set olApp = Nothing
End Sub
See Also
Select an Opportunity | Edit an Opportunity | Delete an Opportunity | Create an Opportunity linked to a Business Contact | 2007 Office System: Updated Developer Content