USP_DATAFORMTEMPLATE_EDIT_SEGMENTMAPPINGS

The save procedure used by the edit dataform template "Account Segment Mappings Edit Form".

Parameters

Parameter Parameter Type Mode Description
@ID uniqueidentifier IN The input ID parameter indicating the ID of the record being edited.
@CHANGEAGENTID uniqueidentifier IN Input parameter indicating the ID of the change agent invoking the procedure.
@PDACCOUNTSEGMENTMAP xml IN PD Segment Mappings

Definition

Copy


CREATE procedure dbo.USP_DATAFORMTEMPLATE_EDIT_SEGMENTMAPPINGS (
    @ID uniqueidentifier
    ,@CHANGEAGENTID uniqueidentifier = null
    ,@PDACCOUNTSEGMENTMAP xml
)
as

set nocount on;

if @CHANGEAGENTID is null  
    exec dbo.USP_CHANGEAGENT_GETORCREATECHANGEAGENT @CHANGEAGENTID output

declare @CURRENTDATE datetime
set @CURRENTDATE = getdate()

merge dbo.PDACCOUNTSEGMENTMAPPING as target
using (
    select                     
        LONGDESCRIPTIONID as LONGDESCRIPTIONID
        ,PDACCOUNTSEGMENTVALUEID as PDACCOUNTSEGMENTVALUEID
    from
        UFN_PDACCOUNTSEGMENTMAP_FROMITEMLISTXML(@PDACCOUNTSEGMENTMAP)
  where not PDACCOUNTSEGMENTVALUEID is null
    ) as source
on 
    (target.LONGDESCRIPTIONID = source.LONGDESCRIPTIONID 
     and target.PDACCOUNTSTRUCTUREID = @ID -- RecordType is Account Structure

     and not source.PDACCOUNTSEGMENTVALUEID is null
    )    
when matched then
    update set 
            target.PDACCOUNTSEGMENTVALUEID = source.PDACCOUNTSEGMENTVALUEID,
            target.CHANGEDBYID = @CHANGEAGENTID,
            target.DATECHANGED = @CURRENTDATE
when not matched by target then
    insert (
        PDACCOUNTSTRUCTUREID
        ,LONGDESCRIPTIONID
        ,PDACCOUNTSEGMENTVALUEID
        ,ISDEFAULT
        ,ADDEDBYID
        ,CHANGEDBYID
        ,DATEADDED
        ,DATECHANGED
    )
    values (
        @ID
        ,source.LONGDESCRIPTIONID
        ,source.PDACCOUNTSEGMENTVALUEID
        ,0
        ,@CHANGEAGENTID
        ,@CHANGEAGENTID
        ,@CURRENTDATE
        ,@CURRENTDATE
     )
   when not matched by source and target.PDACCOUNTSTRUCTUREID = @ID and target.ISDEFAULT=0 then
    delete
   when not matched by source and target.PDACCOUNTSTRUCTUREID = @ID and target.ISDEFAULT=1 then
     update set
            target.PDACCOUNTSEGMENTVALUEID = null,
            target.CHANGEDBYID = @CHANGEAGENTID,
            target.DATECHANGED = @CURRENTDATE    
   ;

return 0;