简体   繁体   中英

C# Microsoft Dynamics GP add new employee to taCreateEmployee

How do I get the next available employee id from the taCreateEmployee?

I building a small windows form program that adds new employees to Microsoft Dynamics Great Plains database using the eConnect tool. I was able to successfully build the xml document and send it to the server however I am getting an error message:




So I guess I need to get the employee id before trying to insert the record but how do I get the employee id? Here is the xml.

 <?xml version="1.0" encoding="utf-8"?> <eConnect xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <UPRCreateEmployeeType><eConnectProcessInfo xsi:nil="true" /> <taRequesterTrxDisabler_Items xsi:nil="true" /> <taCreateEmployee> <EMPLOYID /> //HAVE ALSO TRIED <EMPLOYID></EMPLOYID> AND <EMPLOYID> </EMPLOYID> with a space <EMPLCLAS>SEASONAL</EMPLCLAS> <LASTNAME>Lastname</LASTNAME> <FRSTNAME>Firstname</FRSTNAME> <ADRSCODE>PRIMARY</ADRSCODE> <ADDRESS1>111 Main St</ADDRESS1> <CITY>City</CITY> <STATE>State</STATE> <ZIPCODE>66000</ZIPCODE> <PHONE1>5730000000</PHONE1> <SOCSCNUM>00000000</SOCSCNUM> <BRTHDATE>1930-10-30 0:00:00.000</BRTHDATE> <LOCATNID>PITS</LOCATNID> <SUTASTAT>HI</SUTASTAT> <BIRTHDAY>129</BIRTHDAY> <BIRTHMONTH>10</BIRTHMONTH> </taCreateEmployee> <taCreateInternetAddresses_Items xsi:nil="true"/> </UPRCreateEmployeeType> </eConnect> 

Here is my code to build the xml object:

  private void SerializeObject(AllASEmlpoyees employeeList) { try { eConnectType econnect = new eConnectType(); UPRCreateEmployeeType[] value = new UPRCreateEmployeeType[employeeList.Candidates.Count()]; var count = 0; foreach (var item in employeeList.Candidates) { UPRCreateEmployeeType employee = new UPRCreateEmployeeType(); //employee record taCreateEmployee employeerecord = new taCreateEmployee(); //Console.WriteLine("First Name: " + item.FirstName + " Last Name " + item.LastName); var _with1 = employeerecord; _with1.EMPLOYID = ""; _with1.EMPLCLAS = item.GPEmloyeeClass; _with1.INACTIVE = 0; _with1.FRSTNAME = item.FirstName; _with1.LASTNAME = item.LastName; //_with1.MIDLNAME = ""; _with1.ADRSCODE = item.GPAddressCode; _with1.ADDRESS1 = item.Address1; _with1.ADDRESS2 = item.Address2; _with1.CITY = item.City; _with1.STATE = item.GPStateFullName; _with1.ZIPCODE = item.Zip; _with1.PHONE1 = item.Phone; _with1.SOCSCNUM = item.SSNum; _with1.BIRTHDAY = (short)item.GPBirthday.Day; _with1.BIRTHMONTH = (short)item.GPBirthday.Month; _with1.BRTHDATE = item.GPBirthdayAsString; _with1.LOCATNID = item.GPLocationId; _with1.SUTASTAT = item.GPStateAbbreviation; _with1.EMPLOYMENTTYPE = item.GPEmployeementType; _with1.UpdateIfExists = 1; employee.taCreateEmployee = employeerecord; value[count] = employee; //add array to xml table count++; } econnect.UPRCreateEmployeeType = value; FileStream fs = new FileStream(directory + @"\\" + file, FileMode.Create); XmlTextWriter writer = new XmlTextWriter(fs, new UTF8Encoding()); XmlSerializer serializer = new XmlSerializer(typeof(eConnectType)); serializer.Serialize(writer, econnect); writer.Close(); } catch (ApplicationException ex) { Console.WriteLine("Exception: " + ex.ToString()); } } 

Here is where I make the call using the econnect tool:

  public void eConnectSend(AllASEmlpoyees employeeList) { //Serialized XML File string xmldocument = null; //Connection String string connectString = null; //Result string xmlobject = null; using (eConnectMethods eConCall = new eConnectMethods()) { try { SerializeObject(employeeList); System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument(); xmldoc.Load(directory + @"\\" + file); xmldocument = xmldoc.OuterXml; connectString = "thisismyconnectionstringwhichisworking"; //send data to Great Plains xmlobject = eConCall.CreateTransactionEntity(connectString, xmldocument); Console.WriteLine("Object returned: " + xmlobject.ToString()); } catch (eConnectException exp) { Console.WriteLine("Exception: " + exp.ToString()); //Interaction.MsgBox(exp.ToString); } catch (System.Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); //Interaction.MsgBox(ex.ToString()); } finally { //eConCall.Dispose(); Console.WriteLine("Done"); Console.ReadLine(); this.cleanUpObjectsOnComplete(); } } } 

Any help would me great!

I do not know if this is a work around but this is how I fixed the problem. I added three stored procedures to the database.

Stored Procedures Add:

  1. Check if employee exists using select query and return id if exists
  2. Query UPR40201 table to get next available id
  3. Update UPR40201 table after record was successfully added

The first one, accepts a ss# as an input parameter. Using the parameter passed I run a query on the table looking for an employee that already exists. If it does, then I return the id and call UpdateIfExits on taCreateEmployee.

ALTER PROCEDURE [dbo].[MY_Emloyee_GetEmployeeIdBySocialSecurityNumber]
-- Add the parameters for the stored procedure here
@ssnum AS varchar(255),
@EmployeeID As varchar(10) OUTPUT

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;


    BEGIN TRY
        -- Insert statements for procedure here
        SELECT @EmployeeID=taCreateEmployee.EMPLOYID FROM taCreateEmployee WHERE     taCreateEmployee.SOCSNUM = @ssnum
        return 1
    END TRY
    BEGIN CATCH
        DECLARE
        @ErrorSeverity INT,
        @ErrorNumber   INT,
        @ErrorMessage  NVARCHAR(4000),
        @ErrorState    INT
        SET @ErrorSeverity = ERROR_SEVERITY()
        SET @ErrorNumber = ERROR_NUMBER()
        SET @ErrorMessage = ERROR_MESSAGE()
        SET @ErrorState = ERROR_STATE()
        IF @ErrorState = 0
        SET @ErrorState = 1
        RAISERROR ('ERROR OCCURED:%d',
                    @ErrorSeverity,
                    @ErrorState,
                    @ErrorNumber)
        IF XACT_STATE() < 0
        RETURN @ErrorState
    END CATCH
END
END


If it does not exists then I call my second stored procedure that gets the next available employee id from the table UPR40201 column NEXTEMPID. This table only holds one value and that is the next available id.

ALTER PROCEDURE [dbo].[MY_Employee_GetNewEmployeID]
-- Add the parameters for the stored procedure here
@EmployeeID AS varchar(10) OUTPUT

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;


BEGIN TRY
    -- Insert statements for procedure here
    SELECT @EmployeeID=NEXTEMPID FROM UPR40201
    RETURN 1
END TRY
BEGIN CATCH
    DECLARE
    @ErrorSeverity INT,
    @ErrorNumber   INT,
    @ErrorMessage  NVARCHAR(4000),
    @ErrorState    INT
    SET @ErrorSeverity = ERROR_SEVERITY()
    SET @ErrorNumber = ERROR_NUMBER()
    SET @ErrorMessage = ERROR_MESSAGE()
    SET @ErrorState = ERROR_STATE()
    IF @ErrorState = 0
    SET @ErrorState = 1
    RAISERROR ('ERROR OCCURED:%d',
                @ErrorSeverity,
                @ErrorState,
                @ErrorNumber)
    IF XACT_STATE() < 0
    RETURN @ErrorState
END CATCH
END


Once I have a valid employee Id, I update the record by using the eConnect tool. On success I update the value in table UPR40201 column NEXTEMPID by calling the final stored procedure and increment the id.

ALTER PROCEDURE [dbo].[MY_Employee_UpdateNextEmployeeId]
-- Add the parameters for the stored procedure here
@NEXTEMPID AS varchar(40)

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
--BEGIN COMMANDS
BEGIN TRY
BEGIN TRANSACTION
    -- Insert statements for procedure here
    DELETE FROM UPR40201 WHERE NEXTEMPID != '0';
    INSERT INTO UPR40201 (NEXTEMPID) VALUES (@NEXTEMPID);
    COMMIT TRANSACTION
RETURN 1
END TRY
BEGIN CATCH
  DECLARE
    @ErrorSeverity INT,
    @ErrorNumber   INT,
    @ErrorMessage  NVARCHAR(4000),
    @ErrorState    INT
  SET @ErrorSeverity = ERROR_SEVERITY()
  SET @ErrorNumber = ERROR_NUMBER()
  SET @ErrorMessage = ERROR_MESSAGE()
  SET @ErrorState = ERROR_STATE()
  IF @ErrorState = 0
    SET @ErrorState = 1
  RAISERROR ('ERROR OCCURED:%d',
             @ErrorSeverity,
             @ErrorState,
             @ErrorNumber)
  IF XACT_STATE() < 0
    ROLLBACK TRANSACTION
    RETURN @ErrorState
END CATCH
END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM