USP_DATAFORMTEMPLATE_ADD_MKTMARKETINGPLANTEMPLATE

The save procedure used by the add dataform template "Marketing Plan Template Add Form".

Parameters

Parameter Parameter Type Mode Description
@ID uniqueidentifier INOUT The output parameter indicating the ID of the record added.
@CHANGEAGENTID uniqueidentifier IN Input parameter indicating the ID of the change agent invoking the procedure.
@NAME nvarchar(50) IN Name
@ISDEFAULT bit IN Use this template as the default for new marketing plans
@ITEMLIST xml IN Items
@SITEID uniqueidentifier IN Site

Definition

Copy


CREATE procedure dbo.[USP_DATAFORMTEMPLATE_ADD_MKTMARKETINGPLANTEMPLATE]
(
  @ID uniqueidentifier = null output,
  @CHANGEAGENTID uniqueidentifier = null,
  @NAME nvarchar(50),
  @ISDEFAULT bit,
  @ITEMLIST xml = null,
  @SITEID uniqueidentifier = null
)
as
  set nocount on;

  declare @CURRENTDATE datetime;

  begin try
    if @ID is null set @ID = newid();

    if @CHANGEAGENTID is null exec dbo.[USP_CHANGEAGENT_GETORCREATECHANGEAGENT] @CHANGEAGENTID output;

    set @CURRENTDATE = getdate();

    -- get the current default template

    if @ISDEFAULT <> 0
      begin
        declare @DEFAULTID uniqueidentifier;
        select top 1 @DEFAULTID = ID from dbo.[MKTMARKETINGPLANTEMPLATE] where ([SITEID] = @SITEID or (@SITEID is null and [SITEID] is null)) and [ISDEFAULT] <> 0;

        if not @DEFAULTID is null
          if @DEFAULTID <> @ID
            update dbo.[MKTMARKETINGPLANTEMPLATE] set [ISDEFAULT] = 0, [CHANGEDBYID] = @CHANGEAGENTID, [DATECHANGED] = @CURRENTDATE where [ID] = @DEFAULTID;
      end;

    insert into dbo.[MKTMARKETINGPLANTEMPLATE] (
      [ID],
      [NAME],
      [SITEID],
      [ISDEFAULT],
      [ADDEDBYID],
      [CHANGEDBYID],
      [DATEADDED],
      [DATECHANGED]
    ) values (
      @ID,
      @NAME,
      @SITEID,
      @ISDEFAULT,
      @CHANGEAGENTID,
      @CHANGEAGENTID,
      @CURRENTDATE,
      @CURRENTDATE
    );

    if @ITEMLIST is not null
      exec dbo.[USP_MKTMARKETINGPLANTEMPLATE_GETITEMLIST_ADDFROMXML] @ID, @ITEMLIST, @CHANGEAGENTID, @CURRENTDATE;
  end try

  begin catch
    exec dbo.[USP_RAISE_ERROR];
    return 1;
  end catch

  return 0;