Friday, 21 October 2016

AX 2012  AIF document service C# code to create multiple address and contact information lines

In my previous posts I demonstrated below points.

1. Basics of AIF
2. How to create a document service
3. How to use and attach the address framework on any custom entity.
4, How to test the create operation using C# code through visual studio console application.

This post demonstrate a more specific "multiple lines" testing scenario which we need to test often.

In my service I have a student and its related address. Microsoft dynamics AX supports multiple type of addresses to be stored for single entity.

I am sharing the below code which will help in testing the similar multiline or header -line scenarios.

Note: Refer Bold italic part in below code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestStudentService.ServiceReference;//Added namespace o access service in below
{
class Program
{static void Main(string[] args)
{StudentTableServiceClient proxy = new StudentTableServiceClient();
// Create an instance of the document class.
AxdStudentTable axdStudent = new AxdStudentTable();
//Create instances of the entities that are used in the service and
// set the needed fields on those entities.
Int32 rollnumber = 10;
AxdEntity_StudentTable studentTable = new AxdEntity_StudentTable();
studentTable.RollNumber = rollnumber;studentTable.Name = "Suresh";
studentTable.Standard = "Tenth";

//Create a postal address
AxdEntity_DirPartyPostalAddressView address = new AxdEntity_DirPartyPostalAddressView();
address.Street = "2122 My street ";
address.City = "Beverly Hills";
address.State = "";
address.CountryRegionId = "USA";
address.ZipCode = "90210";
address.Roles = "Home";

//Create Electronic address
List<AxdEntity_DirPartyContactInfoView> lines = new List<AxdEntity_DirPartyContactInfoView>();

for (int index = 0; index < 2; index++)
{// Add phone number

if (index == 0)
{AxdEntity_DirPartyContactInfoView electronic = new AxdEntity_DirPartyContactInfoView();

electronic.LocationName = "Harry potter Phone";

electronic.Type = AxdEnum_LogisticsElectronicAddressMethodType.Phone;

electronic.Locator = "+61 (0) 448 146 250";

electronic.Roles = "Business";

electronic.TypeSpecified = true;
lines.Add(electronic);

}
// Add email
if (index == 1)
{AxdEntity_DirPartyContactInfoView electronic = new AxdEntity_DirPartyContactInfoView();

electronic.LocationName = "Harry potter Email";

electronic.Type = AxdEnum_LogisticsElectronicAddressMethodType.Email;

electronic.Locator = "aj@upwork.com";

electronic.Roles = "Business";

electronic.TypeSpecified = true;
lines.Add(electronic);
}
}
///Add the addresses to the party record and the party to the extension table record
AxdEntity_DirPartyTable_DirPartyTable dirPartyTable = new AxdEntity_DirPartyTable_DirPartyTable();

dirPartyTable.Name = "Suresh";

dirPartyTable.DirPartyPostalAddressView = new AxdEntity_DirPartyPostalAddressView[1] { address };
dirPartyTable.DirPartyContactInfoView = lines.ToArray();studentTable.DirPartyTable = new AxdEntity_DirPartyTable_DirPartyTable[1] { dirPartyTable };
axdStudent.StudentTable = new AxdEntity_StudentTable[1] { studentTable };

try

{// Call the create method on the service passing in the document.

CallContext context = new CallContext();

context.Company = "EAM";

context.Language = "en-au";

EntityKey[] returnFloc = proxy.create(context, axdStudent);

// The create method returns an EntityKey which contains the ID of the sales order.

EntityKey returnedFloc = (EntityKey)returnFloc.GetValue(0);

Console.WriteLine("the student has been created of " + returnedFloc.KeyData[0].Value);

Console.ReadLine();
}catch (Exception e)
{Console.WriteLine(e.ToString());

Console.ReadLine();
}               
}  
}
}


In my next post I will share the code to test the update operation.


No comments:

Post a Comment