Thursday 4 December 2008

InfoPath and Great Plains Web Services: Update Customer Address Sample

This week I was asked to explore some the new web services available out the box with GP 10.0. It seems there are services to do pretty much anything you want with GP (Programmers Guide: https://mbs.microsoft.com/downloads/customer/WSProgrammersGuide.pdf). I thought I'd use some to create an InfoPath form to update customer details in GP. I had a dig around on the web, but didn't find any examples of InfoPath being used with these services, which seemed strange…


…until I found that alas – they are not InfoPath 'friendly'. Because of the way these services work it becomes necessary to create a middle tier web service ourselves, between the form and the GP web services.


[This posting is not a run-down of the web services in GP (see the above programmers guide for that). Instead it should serve as a starting point to show how we can use InfoPath to interact with GP data.]



Creating Our Web Service



  • Create a new Web Service project in Visual Studio
  • Add a Web Reference to the Dynamics GP Web Service. (Usually under /DynamicsGPWebServices/DynamicsGPService.asmx)



  • Now create our required Get and Set Web Methods (see code sample below)

Get Customer List:

[WebMethod]
public CustomerSummary[] GetCustomerList()
{
CustomerSummary[] custList;
CompanyKey companyKey;
Context context = new Context();
LikeRestrictionOfString classIdRestriction;
CustomerCriteria customerCriteria;


// Set up the Web Service
net.blackandblue.server01.DynamicsGP GPServ = new DynamicsGP();
GPServ.UseDefaultCredentials = true;

companyKey = new CompanyKey();
companyKey.Id = (-1);

context.OrganizationKey = (OrganizationKey)companyKey;
context.CultureName = "en-US";

classIdRestriction = new LikeRestrictionOfString();
classIdRestriction.EqualValue = "USA-ILMO-T1";

customerCriteria = new CustomerCriteria();
customerCriteria.ClassId = classIdRestriction;

// Get a list of customers matching the above restrictions
custList = GPServ.GetCustomerList(customerCriteria, context);


return custList;


}







Despite this method returning a custom array of customers, this renders through the web service as XML, and InfoPath is quite happy to pick up the fields. Below is the data set seen after connecting to my new web method in InfoPath (left).

This data populates a drop down list box in InfoPath. After selecting a customer and address, I want the user to be able to update the address and submit back to my web service – which in turn formats the data and calls the GP Web Service.



The method shown below either adds a new customer address, or updates an existing one:

[WebMethod]

public
bool UpdateAddress(string custID, string addressID, string line1, string line2, string line3, string city, string county,
string postcode, string contactPerson, string phone1Val, string phone1CCode, string phone1Ext, string phone2Val, string phone2CCode, string phone2Ext, string phone3Val, string phone3CCode, string phone3Ext, string AddUpdate)
{
CompanyKey companyKey;
Context context = new Context();
CustomerKey customerKey;

// Set up Web Service
net.blackandblue.server01.DynamicsGP GPServ = new DynamicsGP();
GPServ.Credentials = myCred;
GPServ.UseDefaultCredentials = true;

companyKey = new CompanyKey();
companyKey.Id = (-1);

context.OrganizationKey = (OrganizationKey)companyKey;
context.CultureName = "en-US";

// Assign Unique Keys - to define customer and address to update
customerKey = new CustomerKey();
customerKey.Id = custID;

CustomerAddressKey custAdressKey = new CustomerAddressKey();
custAdressKey.Id = addressID;

// Set customer address fields
CustomerAddress custAddress = new CustomerAddress();
custAddress.Key = custAdressKey;
custAddress.Key.CustomerKey = customerKey;

custAddress.Line1 = line1;
custAddress.Line2 = line2;
custAddress.Line3 = line3;
custAddress.City = city;
custAddress.State = county;
custAddress.PostalCode = postcode;
custAddress.ContactPerson = contactPerson;

PhoneNumber phone1 = new PhoneNumber();
phone1.CountryCode = phone1CCode;
phone1.Value = phone1Val;
phone1.Extension = phone1Ext;
custAddress.Phone1 = phone1;

PhoneNumber phone2 = new PhoneNumber();
phone2.CountryCode = phone2CCode;
phone2.Value = phone2Val;
phone2.Extension = phone2Ext;
custAddress.Phone2 = phone2;

PhoneNumber phone3 = new PhoneNumber();
phone3.CountryCode = phone3CCode;
phone3.Value = phone3Val;
phone3.Extension = phone3Ext;
custAddress.Phone3 = phone3;

Policy customerPolicy = GPServ.GetPolicyByOperation("UpdateCustomerAddress",context);

// Choose whether to update an existing address, or create a new one

try{

if(AddUpdate == "Update")
GPServ.UpdateCustomerAddress(custAddress, context, customerPolicy);
if (AddUpdate == "Add")
GPServ.CreateCustomerAddress(custAddress, context, customerPolicy);
return true;
} catch(Exception e)
{
return false;
}
}




Now all that needs to be done is to hook up my Add / Update method to my InfoPath form. I chose to add it as a query web service – as it returns a Boolean result that I can check in the form to show success or failure.



And Presto – An InfoPath 2007 form getting and updating data in GP 10. Of course, there is no reason now why we can't publish the form to InfoPath Forms Services.

Conclusions

As for business use – this method can be highly useful, as we can allow other non-GP staff to interact with the system. With further customization, it could suit the following scenarios:
  • Expenses Form?
  • Stock Item Creation?
  • Customer Management?

And of course, as we are using InfoPath if we hook into SharePoint too we can create approval processes for these forms to be approved before they are submitted to GP.


Resources

Cheers all:-
davros

No comments: