Showing posts with label Address. Show all posts
Showing posts with label Address. Show all posts

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.





Tuesday, 18 October 2016

AIF Part 2- Create document service with logistics postal address and contact information

There is a common requirement of adding the address and contact information to a new entity or document in AX. Here I am explaining an example for the Student entity.

I created a custom table called as Student and need to associate address and contact information to this table aa per the global address book.

In this post I will demonstrate how to use AIF framework for handling the address part for a custom entity.

Step1: Create a student table. Add foreign key relation to DirPartytable.

Step2: Create a query AxStudent. We need to add DirPartyPostalAddressView and DirPartyContactInfoView to our query for handling address related scenarios. Add data sources as shown below.

Also set the join property as per the required scenarios, in my case I have joined all the data sources with outer join.

Set the update property to yes if the update operation is needed to be performed through AIF.

Step3: Go to Tools --> Application integration framework --> Create document service

Step4: Select the query which we have created. In my case it is AxStudent and follow the steps as shown in below screen shot.


Note: Service operations are selected as per the requirement. In my demo I will demonstrate CRUD operations. To perform update we need to include find, read and update operations. I will demonstrate the use of each operation and the difference in my upcoming posts.


This step will generate the service classes for performing the AIF operations. All the related classes will be found under below path.
Go to --> Projects -->Private Projects --> AxdAxStudent(In my case)

Step5: Move the above project to shared node and compile the project. Delete all the cache methods and to-do statements. again compile your project, your project should be error free before proceeding ahead.

Step6: Go-to the AxdAxStudent project under the classes find the Axd class generated by framework. In my case name of the Axd class is AxdAxStudent. Navigate to prepareforSaveExtended method.

Find the below section under this method.



Replace the above mentioned code with exactly as mentioned in below image.

Step7: Guess what your entity is ready to be linked with standard global address book using party type address. You now just need to perform the steps to deploy the service and test it.

In my next post I will demonstrate how to test this service using C# code from visual studio.