USP_DATAFORMTEMPLATE_EDIT_MKTOURCECODE

The save procedure used by the edit dataform template "Source Code Edit Form".

Parameters

Parameter Parameter Type Mode Description
@ID uniqueidentifier IN The input ID parameter indicating the ID of the record being edited.
@NAME nvarchar(100) IN Name
@ISDEFAULT bit IN Use this layout as the default
@CHANGEAGENTID uniqueidentifier IN Input parameter indicating the ID of the change agent invoking the procedure.
@ITEMLIST xml IN Items

Definition

Copy

CREATE procedure dbo.USP_DATAFORMTEMPLATE_EDIT_MKTOURCECODE
(
  @ID uniqueidentifier,
  @NAME nvarchar(100),
  @ISDEFAULT bit,
  @CHANGEAGENTID uniqueidentifier = null,
  @ITEMLIST xml
)
as
  set nocount on;

  declare @CURRENTDATE datetime
  set @CURRENTDATE = GetDate();

  begin try
    if @CHANGEAGENTID is null  
      exec dbo.USP_CHANGEAGENT_GETORCREATECHANGEAGENT @CHANGEAGENTID output;

    -- get the current default template
    if @ISDEFAULT<>0
      begin
        declare @DEFAULTID uniqueidentifier
        select top 1 @DEFAULTID=[ID] from dbo.[MKTSOURCECODE] where    [ISDEFAULT]<>0;

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

    update dbo.[MKTSOURCECODE]
    set 
      [NAME] = @NAME,
      [ISDEFAULT] = @ISDEFAULT,
      [CHANGEDBYID] = @CHANGEAGENTID,
      [DATECHANGED] = @CURRENTDATE
    where [ID] = @ID;

    -- don't update if the code is in use --
    declare @INUSE bit;
    set @INUSE = 0;

    if exists (select top 1 [ID] from dbo.[MKTMARKETINGPLANITEM] where [SOURCECODEID]=@ID)
      set @INUSE =1;
    if exists (select top 1 [ID] from dbo.[MKTSEGMENTATION] where [SOURCECODEID]=@ID)
      set @INUSE =1;

    if @INUSE=0 and @ITEMLIST is not null
      exec dbo.[USP_MKTSOURCECODE_GETITEMLIST_UPDATEFROMXML] @ID, @ITEMLIST, @CHANGEAGENTID, @CURRENTDATE;
  end try

  begin catch
    exec dbo.USP_RAISE_ERROR
    return 1;
  end catch

  return 0;