web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

How to pass a list certain value to a while loop?

(0) ShareShare
ReportReport
Posted on by 465

Hi,

I have a create customer service that accepts some parameters related to the customer in addition to a list of contact persons to be created for the customer.

I want to create the parties for the contact person first -- where as you can see i call "createCustomerContactPersonParty" method and insert parties based on the list of contact persons coming to the service input -- where i save  all parties created in a list.

Now i want to use these parties created to create the actual contact person where i pass the party created to  contactPersonEntity.ContactPersonPartyNumber

The thing is let's say i have those contacts as an input

Name: contactPerson1
Name: contactPerson2

and then i created 2 party records using dirPartyBaseEntity by filling the name and saving the party created for each one in a list

now how can i pass the parties i created to the correct contact person, the two while loops with move next are not helping me.. because i want the first party created to be passed to the first contact(the one that matches the name,then when i loop to the next contact, i want to pass the 2nd party created etc...not sure how to do it

class createCustomerService
{
   
    public CustomerResContract createCustomerDetails(CustomerReqContract _custDetails)  //custDetails has firstName and Lastname of the contact person
    {
        PartyNumber     customerContactPersonPartyNumber;
        List            contactPersons = new List(Types::Class);
        ListEnumerator  listContactPersonEnum;
        List            customerContactPersonPartyNumbers;
        ListEnumerator  listCustomerContactPersonPartyNumbersEnum;
        
        contactPersons = _custDetails.ContactPerson();
        
        if(contactPersons)  
        {
            listContactPersonEnum = contactPersons.getEnumerator();
            customerContactPersonPartyNumbers = new List(Types::String);
            if(listContactPersonEnum)
            {
                while (listContactPersonEnum.moveNext())
                {
                    contactPerson = listContactPersonEnum.current();
                    ttsbegin;
                    customerContactPersonPartyNumbers.addEnd(this.createCustomerContactPersonParty(contactPerson));
                    ttscommit;
                }
            }
        }
        
        // logic
        ttsbegin;
        this.createcustomer();
        //logic
        
        listContactPersonEnum.reset();
        if(listContactPersonEnum)
        {
            while (listContactPersonEnum.moveNext())
            {
                contactPerson = listContactPersonEnum.current();
                if(customerContactPersonPartyNumbers)
                {
                    listCustomerContactPersonPartyNumbersEnum = customerContactPersonPartyNumbers.getEnumerator();
                    if(listCustomerContactPersonPartyNumbersEnum)
                    {
                        while (listCustomerContactPersonPartyNumbersEnum.moveNext())
                        {
                            customerContactPersonPartyNumber = listCustomerContactPersonPartyNumbersEnum.current();
                            ContactPersonCreator personCreator= new ContactPersonCreator();
                            personCreator.createContactPerson(contactPerson, custTable, customerContactPersonPartyNumber);
                            break;
                        }
                    }
                }

            }
        }
    }
    
    public PartyNumber createCustomerContactPersonParty(ContactPersonContract _contactPerson)
    {
        DirPartyBaseEntity dirPartyBaseEntity;
        
        dirPartyBaseEntity.PartyType    = enum2Str(DirPartyType::Person);
        dirPartyBaseEntity.Name         = _contactPerson.PersonDetails().FirstName();
        dirPartyBaseEntity.insert();
    
        return dirPartyBaseEntity.PartyNumber;
    
    }

}

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    Hi,

    I would suggest you to first create the customer and then use a single while loop, create party and then create contact person in the same iteration. In this way you will not have to use another list and also you will not have the matching issue.

    //Create customer
    
    while (listContactPersonEnum.moveNext())
    {
        //Create party
        //Create contact person from the party created above
    }

  • D365FO user Profile Picture
    465 on at

    Hi Muhammad,

    The thing is the service at first used to create customer and contacts in one transaction using customer entity and contact person entity...I didn't have to create the party alone.

    But now because dual write is enabled i'm getting errors because the party need to be created first and committed then I can create the customer and contact with one transaction using the parties created at first.

    The reason I want to go with this approach is because if any error happens I can delete the party..if I go with your approach..this means if any error happens I need to delete the contact and the party..but I want to take advantage of creating the contact and the customer in one transaction and their parties in another transaction...so can you find a solution using my way?

  • Community Member Profile Picture
    on at

    Hi,

    In this case, you can use the Map collection instead of a list to store your created parties.

    https://learn.microsoft.com/en-us/dotnet/api/microsoft.dynamics.ax.xpp.map

    Map stores a key value pair (in your case "Name" and "Party number") and you can use the lookup(key) method of the Map to get your party number but this will work fine if the same name doesn't repeat since the key has to be unique.

    Regards,

    Kamal

  • D365FO user Profile Picture
    465 on at

    Hi Muhammad,t

    I thought about Maps but as you know when you create a contact person from the UI, you are allowed to use the same name..so any other idea?

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi,

    You can create the Party number field in your ContactPersonContract and populate in your first while loop after creating the party. In this way your party number will be associated to your ContactPersonContract object and you don't have to use any other list or map to maintain it.

    After creating the customer you can loop again on your same list and this time your ContactPersonContract will have the party number as well and you can use it to to create contact person.

    Regards,

    Kamal

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 672 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 534 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 289 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans