Pages

Thursday, August 4, 2011

How to change a customer's party type

I often see customers that are created wrong in client's systems.  They will have a party type of "organization" when they should really have "person" or vice versa.  The difference between the two, as far as I can tell, is that organizations are permitted to have multiple contacts, and other little things like that, while person types are supposed to be simpler.

Here is a little static method I wrote to change their party type.  One thing to worry about, when you go from Organization to Person, you may want to check if there are multiple contacts before you make the change.  Not exactly sure what it will do.




static server boolean changeCustPartyType(CustTable _custTable, DirPartyType _dirPartyType)
{
    CustTable       custTable;
    ;

    if (_custTable && _custTable.PartyType != _dirPartyType)
    {
        ttsbegin;

        custTable.selectForUpdate(true);
        custTable.data(_custTable);

        custTable.PartyType = _dirPartyType;

        custTable.setNameAlias();

        DirParty::updatePartyFromCommon(custTable.PartyId, custTable, DirSystemPrivacyGroupType::Public, true);

        custTable.doUpdate();

        smmBusRelTable::updateFromCustTableSFA2(custTable);

        custTable.setAccountOnVend(custTable.orig());

        smmTransLog::initTrans(custTable, smmLogAction::update);

        ttscommit;

        return true;
    }

    return false;
}

5 comments:

  1. What does the three last method calls before ttscommit do?

    ReplyDelete
  2. Hi Alex..

    I'm have a same problem with your posting but my problem is at dynamics ax 2012. while your method for ax 2009.

    Please your advice
    method change party for dynamics ax 2012.


    Thanks before and best regards,

    Toms

    ReplyDelete
  3. The is missing something, because after executed, there is not record in the tabla DirOrganizationDetail...

    I added the following lines to create the record there:

    dirParty = DirParty::constructFromCommon(custTable);
    dirParty.parmOrganizationInfo(true);
    dirOrganizationDetail = DirOrganizationDetail::find(custTable.PartyId,true);
    dirOrganizationDetail.initFromDirParty(dirParty);
    if (!dirOrganizationDetail)
    {
    dirOrganizationDetail.insert(false);
    }

    //Add before the following line
    DirParty::updatePartyFromCommon(custTable.PartyId, custTable, DirSystemPrivacyGroupType::Public, true);


    best regards,

    ReplyDelete
  4. Hi Alex,

    Thanks for your suggestion, it pointed me in the right way to go for my scenario. Taking a look at the MSDN documentation here

    https://technet.microsoft.com/en-us/library/Hh208963.aspx

    you realise that you need to create a new DirPartyTable record with the new type that you want. So you can use the following code:

    newDirPartyTable = DirPartyTable::createNew(DirPartyType::Organization, _customerName);

    Once you have created your new DirPartyTable record with the correct type, you need to assign and modify all of the original values you had from your old DirPartyTable record:
    Taking the example of primary address:

    newDirPartyTable.PrimaryAddressLocation = oldDirPartyTable.PrimaryAddressLocation
    custTable.Party = newDirPartyTable.RecID
    DirPartyLocation.Party = newDirPartyLocation.RecId

    update()

    Thus, with the above model, you can modify contact information and anything else you have configured on your customer.

    Thanks
    Sid

    ReplyDelete
    Replies
    1. Thanks for the feedback Sid. This blog post was a method for AX 2009 and it's been ~4 years since I've looked at it, but good info, and hopefully your comments will help someone else.

      Delete