CREATE TRIGGER [dbo].[@TRIGGERNAME@] ON @SYNCQUEUETABLE@
FOR INSERT NOT
FOR REPLICATION AS

SET ANSI_NULLS ON
SET ANSI_WARNINGS ON
SET NOCOUNT ON
BEGIN
	DECLARE @RPMSyncTable VARCHAR(30), @RPMSyncKey VARCHAR(30), @RPMSyncValue NVARCHAR(255)
	DECLARE @CompanyID VARCHAR(100)
	DECLARE @Result INT 
	
	/* Get Company ID GUID from the Company Settings table */
	SELECT @CompanyID = LTRIM(RTRIM(RPM_Company_Setting_Valu))
	FROM @COMPANYSETUPTABLE@
	WHERE RPM_Company_Setting_Key = '@COMPANYIDSETTING@' AND INTERID = DB_NAME() 
	
	/* Get the table and key values that were inserted */
	SELECT	 @RPMSyncTable = LTRIM(RTRIM(RPM_Sync_Table_Name))
		,@RPMSyncKey = LTRIM(RTRIM(RPM_Sync_Key_Field_Name))
		,@RPMSyncValue = LTRIM(RTRIM(RPM_Sync_Key_Field_Value))
	FROM inserted 
	
	/* Call the REST service to add this to the Data Sync Queue in RPM */
	EXEC @SYSTEMDATABASE@.dbo.usp_RPMSyncQueue @CompanyID
		,@RPMSyncTable
		,@RPMSyncKey
		,@RPMSyncValue
		,@Result OUTPUT 
	
	/* Delete queued record if the stored procedure returned 0;   */ 
	/* Otherwise, leave it in the queue for the next time around. */
	IF @Result = 0
	BEGIN
		DELETE @SYNCQUEUETABLE@
		WHERE RPM_Sync_Table_Name = @RPMSyncTable AND RPM_Sync_Key_Field_Value = @RPMSyncValue;
	END
END