	declare @configurationGroupIdAffiliatedGuid uniqueidentifier = 'b696e645-a597-4990-a996-99289ad86387'

	--account delete
	declare @ConfigurationEntityTreeIdAffiliatedAccountDeleteGuid uniqueidentifier = '227A7AFE-54C6-4603-BB54-3E887743DDC2'

	--account deactivate
	declare @ConfigurationEntityTreeIdAffiliatedAccountDeactivateGuid uniqueidentifier = '92D64393-DFE7-4360-9F00-431E83219E99'

	declare @configurationGroupIdAffiliated int
	select @configurationGroupIdAffiliated = id from ConfigurationGroups where guidkey=@configurationGroupIdAffiliatedGuid


	---------------------------------------------------------------
	--04 - create groups for 
	--	Affiliated account delete
	--	Affiliated account deactivate
	DECLARE @ConfigurationGroups TABLE (EntityId int NOT NULL,GuidKey uniqueidentifier not null );
	
	--activate account:
	INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (14, @ConfigurationEntityTreeIdAffiliatedAccountDeleteGuid) 
	INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (14, @ConfigurationEntityTreeIdAffiliatedAccountDeactivateGuid) 


	MERGE INTO dbo.ConfigurationGroups AS TARGET  
		USING @ConfigurationGroups AS SOURCE  
		ON upper(TARGET.GuidKey) = upper(SOURCE.GuidKey)
		WHEN MATCHED THEN    
				UPDATE SET entityid = SOURCE.entityid
		WHEN NOT MATCHED BY TARGET THEN   
				INSERT (EntityId,GuidKey) VALUES (Source.EntityId,upper(source.GuidKey)) 
	OUTPUT $action, SOURCE.*;

	---------------------------------------------------------------
	--account delete:
	declare @configurationGroupIdAffiliatedAccountDelete int
	select @configurationGroupIdAffiliatedAccountDelete = id from ConfigurationGroups where guidkey=@ConfigurationEntityTreeIdAffiliatedAccountDeleteGuid

	--account delete:
	declare @configurationGroupIdAffiliatedAccountDeactivate int
	select @configurationGroupIdAffiliatedAccountDeactivate = id from ConfigurationGroups where guidkey=@ConfigurationEntityTreeIdAffiliatedAccountDeactivateGuid
	---------------------------------------------------------------
	--05 - create groups for Affiliated Entities
	DECLARE @ConfigurationGroupParameters TABLE (GroupId int NOT NULL,ParameterId int not null, Value nvarchar(max));
	
	--Affiliated account delete
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountDelete,75,  N'https://sb.alertmessage.com/acct/{0}'		-- N'Url')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountDelete,76,  N'affiliated-api'					-- N'ClientId')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountDelete,77,  N'application/json'	-- N'ContentType')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountDelete,78,  N'DELETE'								-- N'Method')

	--Affiliated account deactivate
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountDeactivate,75,  N'https://sb.alertmessage.com/acct/{0}/deactivate'		-- N'Url')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountDeactivate,76,  N'affiliated-api'					-- N'ClientId')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountDeactivate,77,  N'application/json'	-- N'ContentType')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountDeactivate,78,  N'POST'								-- N'Method')

	MERGE INTO dbo.ConfigurationGroupParameters AS TARGET  
			USING @ConfigurationGroupParameters AS SOURCE  
			ON TARGET.GroupId  = SOURCE.GroupId
			AND TARGET.ParameterId =  SOURCE.ParameterId
			WHEN MATCHED THEN    
					UPDATE SET Value=source.Value
			WHEN NOT MATCHED BY TARGET THEN   
					INSERT ( GroupId,ParameterId,Value) VALUES (Source.GroupId, SOURCE.ParameterId,source.Value)  
	OUTPUT $action, SOURCE.*;

	---------------------------------------------------------------
	--06: ConfigurationEntityTree
	--Set the identifier in the tree for the instance
	declare @ConfigurationEntityTreeIdAffiliatedAlarmGuid uniqueidentifier = 'A4E36C1F-6B1F-4029-BE7C-7AE97F95DDF1'

	DECLARE @ConfigurationEntityTree TABLE (EntityTypeId int not null,GroupId int NOT NULL, MachineName nvarchar(max), ServiceName nvarchar(max), ServiceId nvarchar(max), 
											ModuleIdentifier nvarchar(max),ParentEntityId int null,GuidKey uniqueidentifier not null );


	declare @ConfigurationEntityTreeIdAffiliatedParentId int
	select @ConfigurationEntityTreeIdAffiliatedParentId = id from ConfigurationEntityTree where guidkey=@ConfigurationEntityTreeIdAffiliatedAlarmGuid

	--Affiliated Account Delete
	INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
	select 14, @configurationGroupIdAffiliatedAccountDelete, N'MachineId', N'Affiliated', N'AffiliatedDefault',N'AffiliatedAccountDelete',@ConfigurationEntityTreeIdAffiliatedParentId, @ConfigurationEntityTreeIdAffiliatedAccountDeleteGuid

	--Affiliated Account Deactivate
	INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
	select 14, @configurationGroupIdAffiliatedAccountDeactivate, N'MachineId', N'Affiliated', N'AffiliatedDefault',N'AffiliatedAccountDeactivate',@ConfigurationEntityTreeIdAffiliatedParentId, @ConfigurationEntityTreeIdAffiliatedAccountDeactivateGuid

	MERGE INTO dbo.ConfigurationEntityTree AS TARGET  
			USING @ConfigurationEntityTree AS SOURCE  
			ON upper(TARGET.GuidKey) = upper(SOURCE.GuidKey)
             
			WHEN MATCHED THEN    
					UPDATE SET  MachineName=source.MachineName, ServiceName=source.ServiceName, ServiceId=source.ServiceId, 
					ModuleIdentifier=source.ModuleIdentifier, ParentEntityId=source.ParentEntityId
			WHEN NOT MATCHED BY TARGET THEN   
					INSERT ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId,GuidKey) 
					VALUES ( source.EntityTypeId, source.GroupId, source.MachineName, source.ServiceName, source.ServiceId, source.ModuleIdentifier, source.ParentEntityId, upper(source.GuidKey))
	OUTPUT $action, SOURCE.*;

