Sunday, 23 October 2016

C# code to update the Logistics postal address through AIF.

In my second post of this series, I demonstrated the creation of document service for a new entity. The post also include the solution on how to add the related postal address using AIF document service.

In order to achieve this we have added  calls to some standard API's in prepareForSaveDetailsExtended() method of our service class.

In this post I am sharing the code which can be used to test the update operation on student entity with address.

In order to update the surrogate key indexed entity, we need to use the find operation for finding the entity key, and then based on the found entity key we can do the read operation which will return the Axd class instance which can be updated as per the requirement. Below is the code which will demonstrate the same requirement.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestStudentService.ServiceReference1;//Added namespace o access service in below application

namespace TestStudentService
{
class Program
{
static void Main(string[] args)
{
int rollNumber = 4;

AxdStudentTable studentAxd = null;
CallContext context = new
CallContext();
context.Company = "EAM";

StudentTableServiceClient proxy = new StudentTableServiceClient();
try
{
studentAxd = proxy.read(context, Program.readCritera(rollNumber));
//Console.WriteLine("Read worked");
Program.updateCustomer(studentAxd, rollNumber);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
private static EntityKey[] readCritera(int rollNumber)
{
StudentTableServiceClient client = new StudentTableServiceClient();
AxdStudentTable localAxd = new AxdStudentTable();

CriteriaElement[] criteriaElements = new CriteriaElement[1];
criteriaElements[0] = new CriteriaElement();
criteriaElements[0].DataSourceName = "StudentTable";
criteriaElements[0].FieldName = "RollNumber";
criteriaElements[0].Value1 = rollNumber.ToString();
QueryCriteria queryCriteria = new QueryCriteria();
queryCriteria.CriteriaElement = criteriaElements;
CallContext readContext = new
CallContext();
readContext.Company = "EAM";
//Using find operation
localAxd = client.find(readContext, queryCriteria);
//Using read operation
EntityKey[] entityKeyList = new EntityKey[1];
EntityKey key = new EntityKey();
KeyField[] keyFields = new KeyField[1];
KeyField keyField = new KeyField();
keyField.Field = "RecID";


keyField.Value = localAxd.StudentTable[0].RecId.ToString();
keyFields[0] = keyField;
key.KeyData = keyFields;
entityKeyList[0] = key;
return entityKeyList;
}
private static void updateCustomer(AxdStudentTable studentTableAxd, int rollNumber)
{
try
{
StudentTableServiceClient updateProxy = new StudentTableServiceClient();
CallContext updateContext = new CallContext();
updateContext.Company = "EAM";

AxdEntity_StudentTable OldStudentEntity = studentTableAxd.StudentTable[0];
AxdEntity_StudentTable newStudentEntity = new AxdEntity_StudentTable();
AxdStudentTable newStudentAxd = new AxdStudentTable();

newStudentAxd.ValidTimeStateType = studentTableAxd.ValidTimeStateType;
newStudentAxd.ValidTimeStateTypeSpecified = true;

newStudentAxd.ValidAsOfDateTime = studentTableAxd.ValidAsOfDateTime;
newStudentAxd.ValidFromDateTime = studentTableAxd.ValidFromDateTime;
newStudentAxd.ValidToDateTime = studentTableAxd.ValidToDateTime;
newStudentEntity._DocumentHash = OldStudentEntity._DocumentHash;
newStudentEntity.RecId = OldStudentEntity.RecId;
newStudentEntity.RecVersion = OldStudentEntity.RecVersion;
newStudentEntity.action = AxdEnum_AxdEntityAction.update;
newStudentEntity.actionSpecified = true;
newStudentEntity.Name = "Suresk kumar";
#region Update the date effective entities
newStudentEntity.DirPartyTable = OldStudentEntity.DirPartyTable;
newStudentEntity.DirPartyTable[0].action = AxdEnum_AxdEntityAction.update;
newStudentEntity.DirPartyTable[0].actionSpecified = true;
newStudentEntity.DirPartyTable[0].DirPartyPostalAddressView[0].StreetNumber = "10";
newStudentEntity.DirPartyTable[0].DirPartyPostalAddressView[0].Roles = "Home";
newStudentEntity.DirPartyTable[0].DirPartyPostalAddressView[0].Street = "street updated 1223";
newStudentEntity.DirPartyTable[0].DirPartyPostalAddressView[0].action = AxdEnum_AxdEntityAction.update;
newStudentEntity.DirPartyTable[0].DirPartyPostalAddressView[0].actionSpecified = true;
newStudentEntity.DirPartyTable[0].DirPartyPostalAddressView[0].updateMode = AxdEnum_ValidTimeStateUpdate.Correction;
newStudentEntity.DirPartyTable[0].DirPartyPostalAddressView[0].updateModeSpecified = true;
newStudentEntity.DirPartyTable[0].DirPartyContactInfoView[0].Locator = "+61 (0) 12 345 678";
newStudentEntity.DirPartyTable[0].DirPartyContactInfoView[0].Roles = "Home";
newStudentEntity.DirPartyTable[0].DirPartyContactInfoView[0].Type = AxdEnum_LogisticsElectronicAddressMethodType.Phone;
newStudentEntity.DirPartyTable[0].DirPartyContactInfoView[0].action = AxdEnum_AxdEntityAction.update;
newStudentEntity.DirPartyTable[0].DirPartyContactInfoView[0].actionSpecified = true;

newStudentAxd.StudentTable = new AxdEntity_StudentTable[1] { newStudentEntity };
#endregion
updateProxy.update(updateContext, Program.readCritera(rollNumber), newStudentAxd);
Console.Write("Worked");
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine("Failed");
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
}
}



In my next post I will demonstrate the use of methods of document service class which we can use for our customizations.





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.