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

	--activate account:
	declare @ConfigurationEntityTreeIdAffiliatedActivateAccountGuid uniqueidentifier = 'F159050E-26CD-4CF4-A3A0-95302CA304E7'

	--Delete Unnecessary parameters from ConfigurationGroupParameters
	declare @configurationGroupIdAffiliated int
	select @configurationGroupIdAffiliated = id from ConfigurationGroups where guidkey=@configurationGroupIdAffiliatedGuid

	--Configuration Tables - add Affiliated parameters:
	--01
	--AffiliatedToken ENTITY:
	DECLARE @ConfigurationEntityTypes TABLE ([Id] [int] NOT NULL,[Title] [nvarchar](max) NOT NULL);

	
	---------------------------------------------------------------
	---------------------------------------------------------------
	--02 new Parameters for AffiliatedToken
	DECLARE @ConfigurationParameterTypes TABLE (Id int NOT NULL,Title nvarchar(max) NOT NULL);
	
	
	---------------------------------------------------------------
	--04 - create groups for 
	--	Affiliated account Entity
	--	Affiliated token entity
	DECLARE @ConfigurationGroups TABLE (EntityId int NOT NULL,GuidKey uniqueidentifier not null );
	
	--activate account:
	INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (14, @ConfigurationEntityTreeIdAffiliatedActivateAccountGuid) --Activate Account


	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.*;

	---------------------------------------------------------------
	---------------------------------------------------------------

	--activate account:
	declare @configurationGroupIdAffiliatedActivateAccount int

	--activate account:
	select @configurationGroupIdAffiliatedActivateAccount = id from ConfigurationGroups where guidkey=@ConfigurationEntityTreeIdAffiliatedActivateAccountGuid
	---------------------------------------------------------------
	---------------------------------------------------------------
	--05 - create groups for Affiliated Entities
		
	DECLARE @ConfigurationGroupParameters TABLE (GroupId int NOT NULL,ParameterId int not null, Value nvarchar(max));
	

	--Affiliated activate account
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedActivateAccount,75,  N'https://sb.alertmessage.com/acct/{0}/activate'		-- N'Url')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedActivateAccount,76,  N'affiliated-api'					-- N'ClientId')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedActivateAccount,77,  N'application/x-www-form-urlencoded'	-- N'ContentType')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedActivateAccount,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 Activate Account
	INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
	select 14, @configurationGroupIdAffiliatedActivateAccount, N'MachineId', N'Affiliated', N'AffiliatedDefault',N'AffiliatedActivateAccount',@ConfigurationEntityTreeIdAffiliatedParentId, @ConfigurationEntityTreeIdAffiliatedActivateAccountGuid


	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.*;