简体   繁体   English

C#Microsoft Dynamics GP将新员工添加到taCreateEmployee

[英]C# Microsoft Dynamics GP add new employee to taCreateEmployee

How do I get the next available employee id from the taCreateEmployee? 如何从taCreateEmployee获取下一个可用的员工ID?

I building a small windows form program that adds new employees to Microsoft Dynamics Great Plains database using the eConnect tool. 我构建了一个小的Windows窗体程序,该程序使用eConnect工具将新员工添加到Microsoft Dynamics Great Plains数据库。 I was able to successfully build the xml document and send it to the server however I am getting an error message: 我能够成功构建xml文档并将其发送到服务器,但是却收到错误消息:


'taCreateEmployee' expects parameter '@I_vEMPLOYID', which was not supplied. 'taCreateEmployee'期望未提供参数'@I_vEMPLOYID'。

So I guess I need to get the employee id before trying to insert the record but how do I get the employee id? 因此,我想我需要在插入记录之前获取员工ID,但是如何获取员工ID? Here is the xml. 这是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: 这是我构建xml对象的代码:

  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: 这是我使用econnect工具拨打电话的地方:

  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 使用选择查询检查员工是否存在,如果存在则返回ID
  2. Query UPR40201 table to get next available id 查询UPR40201表以获取下一个可用的id
  3. Update UPR40201 table after record was successfully added 成功添加记录后更新UPR40201表

The first one, accepts a ss# as an input parameter. 第一个接受ss#作为输入参数。 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. 如果是这样,那么我返回ID并在taCreateEmployee上调用UpdateIfExits。

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. 如果不存在,则调用第二个存储过程,该过程从表UPR40201的NEXTEMPID列中获取下一个可用的雇员ID。 This table only holds one value and that is the next available id. 该表仅包含一个值,也就是下一个可用的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. 有了有效的员工ID后,我将使用eConnect工具更新记录。 On success I update the value in table UPR40201 column NEXTEMPID by calling the final stored procedure and increment the id. 成功后,我通过调用最终存储过程来更新表UPR40201列NEXTEMPID中的值,并增加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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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