Wednesday, February 26, 2014

HTTP request through X++

X++ code to make HTTP request

xml                         response = "";
    System.Net.HttpWebRequest   httpRequest  = null;
    System.Net.HttpWebResponse  httpResponse = null;
    CLRObject                   clro         = null;
    System.IO.Stream            stream       = null;
    System.IO.StreamReader      streamReader = null;
    System.Byte[]               bytes        = null;
    System.Text.Encoding        encoding     = null;

    try
    {
        new InteropPermission(InteropKind::ClrInterop).assert();
        clro         = System.Net.WebRequest::Create(url);
        httpRequest  = clro;
        if(method)
        {
            httpRequest.set_Method(method);
        }
        if(header)
        {
            httpRequest.set_ContentType(header);
        }
        if(body)
        {
            encoding    = System.Text.Encoding::get_UTF8();
            bytes       = encoding.GetBytes(body);
            httpRequest.set_ContentLength(bytes.get_Length());
            stream      = httpRequest.GetRequestStream();
            stream.Write(bytes,0,bytes.get_Length());
            stream.Close ();
        }

        httpResponse = httpRequest.GetResponse();
        stream = httpResponse.GetResponseStream();
        streamReader = new System.IO.StreamReader(stream);
        response = streamReader.ReadToEnd();
        CodeAccessPermission::revertAssert();
    }
    catch(Exception::CLRError)
    {
        throw error(AifUtil::getClrErrorMessage());
    }
    return response;

Tuesday, February 25, 2014

Update or Create Party Addresses

Following code helps you to create or update party (Customer/Vendor/Employees) addresses using standard function

select firstOnly postalAddressView
                    join Location,LocationRole from locationRolesView
                   where locationRolesView.Location     == postalAddressView.Location
                      && locationRolesView.LocationRole == LogisticsLocationRole.RecId
                      && postalAddressView.Party        == partyId;


        postalAddressView.Street           = street1;
        if(street2)
        {
            postalAddressView.Street           += " " + street2;
        }
        postalAddressView.City             = city;
        postalAddressView.State            = stateId;
        postalAddressView.CountryRegionId  = countryCode;
        postalAddressView.ZipCode          = zipCode;
        postalAddressView.Address          = LogisticsPostalAddress::formatAddress(postalAddressView.Street,  postalAddressView.ZipCode, postalAddressView.City, postalAddressView.CountryRegionId, postalAddressView.State,'');
        postalAddressView.IsPrivate        = NoYes::No;


        postalAddressViewResult = dirParty.createOrUpdatePostalAddress(postalAddressView,[LogisticsLocationRole.recid]);