Create Opportunity & Prospect with contacts through code in AX 2012

void clicked()
{
smmOpportunityTable _oppTable;
StaggingOpportunity _stagOpportunity;
ContactPerson _contactPerson;
LogisticsLocation _logisticLocation;
LogisticsElectronicAddress _logisticEAddress, _logisticEAddressI;
DirPartyLocation _dirPartyLocation;
RecId _dirPartyCompanyId, _dirPartyPersonId, _owner;
;

if(StaggingOpportunity.IsInjected == NoYes::No)
{
try
{
ttsBegin;

_dirPartyCompanyId = DirPartyTable::createNew(DirPartyType::Organization, StaggingOpportunity.Company).RecId;
_dirPartyPersonId = DirPartyTable::createNew(DirPartyType::Person, StaggingOpportunity.Name).RecId;
_owner = Hcmworker::findByPerson(DirPersonUser::find(curUserId()).PersonParty).RecId;

//Creation of the opportunity
//On the creation of the opportunity, system will automatically create the Prospect
_oppTable.clear();
_oppTable.Subject = StaggingOpportunity.Subject;
_oppTable.OpenedByWorker = _owner;
_oppTable.OpenedDate = today();
_oppTable.OwnerWorker = _owner;
_oppTable.Party = _dirPartyCompanyId;
_oppTable.Status = smmOpportunityStatus::Active;
_oppTable.insert();

//Creation of Contact detail
_contactPerson.clear();
_contactPerson.ContactForParty = _oppTable.Party;
_contactPerson.MainResponsibleWorker = _oppTable.OwnerWorker;
_contactPerson.Party = _dirPartyPersonId;
_contactPerson.Sensitivity = smmSensitivity::Normal;
_contactPerson.insert();
//Creation of Logistic information as a Parent for Email and Phone data
_logisticLocation.clear();
_logisticLocation.Description = StaggingOpportunity.Company;
_logisticLocation.insert();

//Creation of Email address detail
_logisticEAddress.clear();
_logisticEAddress.Description = “Official Email address”;
_logisticEAddress.IsPrimary = NoYes::Yes;
_logisticEAddress.Location = _logisticLocation.RecId;
_logisticEAddress.Locator = StaggingOpportunity.Email;
_logisticEAddress.PrivateForParty = _oppTable.Party;
_logisticEAddress.Type = LogisticsElectronicAddressMethodType::Email;
_logisticEAddress.insert();

//Associate the data of above data with DirPartyTable
DirPartyTable::updatePrimaryContact(_contactPerson.Party, _logisticEAddress);
//Creation of data in dirPartyLocation
_dirPartyLocation.clear();
_dirPartyLocation.IsPrimary = NoYes::Yes;
_dirPartyLocation.Location = _logisticLocation.RecId;
_dirPartyLocation.Party = _dirPartyPersonId;
_dirPartyLocation.insert();
//Creation of Phone Number detail
_logisticEAddress.clear();
_logisticEAddress.Description = “Official Phone Number”;
_logisticEAddress.IsPrimary = NoYes::No;
_logisticEAddress.Location = _logisticLocation.RecId;
_logisticEAddress.Locator = StaggingOpportunity.Contact;
_logisticEAddress.PrivateForParty = _oppTable.Party;
_logisticEAddress.Type = LogisticsElectronicAddressMethodType::Phone;
_logisticEAddress.insert();
//Update the stagging information
/* while select forUpdate _stagOpportunity
where
_stagOpportunity.RecId == StaggingOpportunity.RecId
{
_stagOpportunity.OpportunityId = _oppTable.OpportunityId ;
_stagOpportunity.IsInjected = NoYes::Yes ;
_stagOpportunity.update();
}*/

update_recordSet _stagOpportunity
setting
OpportunityId = _oppTable.OpportunityId,
IsInjected = NoYes::Yes
where
_stagOpportunity.RecId == StaggingOpportunity.RecId;

ttsCommit;
StaggingOpportunity_ds.refresh();
StaggingOpportunity_ds.refreshEx();
StaggingOpportunity_ds.reread();
}
catch
{
error(strFmt(“%1”, Exception::Error));
}
}
else
warning(“Selected record already exist in the Opportunity section.”);

//super();
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s