CREATE PROC [dbo].[@PROCNAME@] 
	 @RPMCompanyID VARCHAR(100)
	,@RPMSyncTable VARCHAR(30)
	,@RPMSyncKey VARCHAR(30)
	,@RPMSyncValue NVARCHAR(255)
	,@Result INT OUTPUT
AS
SET ANSI_NULLS ON
SET ANSI_WARNINGS ON
SET NOCOUNT ON

BEGIN
	DECLARE @Object INT
	DECLARE @ResponseText VARCHAR(8000)
	DECLARE @EntityID VARCHAR(10) 
		
	/* Create an instance of MSXML2.ServerXMLHTTP */
	EXEC sp_OACreate 'MSXML2.ServerXMLHttp'
		,@Object OUT 
		
	/* Specify the URL of the REST API endpoint */
	DECLARE @Url NVARCHAR(400)

	SELECT @Url = RTRIM(LTRIM(RPM_Company_Setting_Valu)) + 'api/RPMSyncQueues'
	FROM @COMPANYSETUPTABLE@
	WHERE RPM_Company_Setting_Key = '@SERVICEURLSETTING@'
	  AND INTERID = '' /* System level setting */ 
		
	/* Open a connection to the specified URL */
	EXEC sp_OAMethod @Object
		,'Open',NULL,'POST',@Url,false 
		
	/* Set request headers */
	EXEC sp_OAMethod @Object
		,'setRequestHeader',NULL,'Content-Type','application/json'

	EXEC sp_OAMethod @Object
		,'setRequestHeader',NULL,'CompanyID',@RPMCompanyID
		
	/* Determine entityID value for the table that was passed in */
	SET @EntityID = (
		CASE @RPMSyncTable
			WHEN 'MC40200' THEN '97'  /*Currency*/
			WHEN 'RM00101' THEN '98'  /*Customer*/
			WHEN 'GL00100' THEN '100' /*Account*/
			WHEN 'IV00101' THEN '102' /*Item*/
			WHEN 'IV00105' THEN '104' /*Item Currency*/
			WHEN 'IV40700' THEN '105' /*Location*/
			WHEN 'IV40800' THEN '106' /*Price Schedule*/
			WHEN 'IV40202' THEN '107' /*UofM*/
			WHEN 'IV40201' THEN '108' /*UofM Schedule*/
			ELSE '999'
		END 
		) 
			
	/* Only call REST service if sync table is an expected value */
	IF @EntityID != '999'
	BEGIN 
		/* surround record ID with single quotes, unless it is the Account Index */
		DECLARE @RecordID VARCHAR(255)
		IF @EntityID = '100'	/* Account */
			SET @RecordID = @RPMSyncValue
		ELSE
			SET @RecordID = '''' + @RPMSyncValue + '''';
		/* Send the request with the JSON payload */
		DECLARE @RequestBody VARCHAR(8000) = 
		'{ 
			"id": 0,
			"entryTime": "' + CONVERT(CHAR, GETDATE(), 120) + '",
			"entityID": ' + @EntityID + ',
			"queueTypeID": 89,
			"recordID": "' + @RecordID + '", 		  
			"statusID": 93 		
		}'
		EXEC @Result = sp_OAMethod @Object
			,'send',NULL,@RequestBody
	END 
	
	/* Clean up */
	EXEC sp_OADestroy @Object
END
