Wednesday, 8 July 2015

Add download button in Add form which download the file

Blackbaud CRM 
I had a scenario in which download functionality needs to be added on Add form. This download button takes File Name as input from the user. The same functionality like we have in normal windows application in which user open a save file dialogue box and then user put the name of file and then save the file. So we were facing issues to actually map the same sort of functionality in BlackBaud. So what I did to make a View form instead of Add form, reason being to actually no need for Save button and in Add form we don’t have any functionality to hide the OOB save button. So, I made a view form like below.



In above picture, File name is an input field, normally we have view only fields in view form so I changed this in HTML page in UI Model and replace the span with input tag. In VB class of UI Model, I needed to actually fetch the data from some data form through DataFormLoadRequest() and put that data into the text file. So, in UI Model, I coded the below lines behind the Export button event

Private Sub _export_InvokeAction(sender As Object, e As InvokeActionEventArgs) Handles _export.InvokeAction
        If String.IsNullOrEmpty(Me.MYEXPORTFILE.Value) Then 'Checking if MYEXPORTFILE Field is empty
            Dim prompt As New UIPrompt()
            prompt.ButtonStyle = UIPromptButtonStyle.Ok
            prompt.Text = "Empyt File Name is not allowed."
            prompt.ImageStyle = UIPromptImageStyle.Information
            Me.Prompts.Add(prompt)
            Exit Sub
        End If

        If Not Me.MYEXPORTFILE.Value.Contains(".") Then
            Dim prompt As New UIPrompt()
            prompt.ButtonStyle = UIPromptButtonStyle.Ok
            prompt.Text = "File Name without Extension not allowed."
            prompt.ImageStyle = UIPromptImageStyle.Information
            Me.Prompts.Add(prompt)
            Exit Sub
        End If


        Dim Filename_Array As String() = Me.MYEXPORTFILE.Value.Trim.Split(New Char() {"."c}) 'spliting MYEXPORTFILE Field value & creating Array of string
        Dim counter As Int32 = 1 'Counter to skip first part of Array in loop

        For Each fern As String In Filename_Array 'Loop on Array to get Extension part in Array
            If Not counter = 1 Then 'skiping first part of Array
                If Not (fern = "ext" Or fern = "txt") Then 'Checing extension of File
                    Dim prompt As New UIPrompt()
                    prompt.ButtonStyle = UIPromptButtonStyle.Ok
                    prompt.Text = "File Extension '" + fern + "' is not Allowed."
                    prompt.ImageStyle = UIPromptImageStyle.Information
                    Me.Prompts.Add(prompt)
                Else
                    Me.DownloadCustomPrompt("MYEXPORTKEY", Me.MYEXPORTFILE.Value)
                End If
            End If
            counter += 1
        Next
    End Sub

The above yellow highlighted line will call the below File download event of UI Model which will actually start the downloading and writes the data into the text file, see the below event source code

Private Sub ExportOMerViewDataFormUIModel_FileDownloadCustom(sender As Object, e As FileDownloadCustomEventArgs) Handles Me.FileDownloadCustom
        If e.Key = "MYEXPORTKEY" Then

            '''''' Lines are commented for OM project business

            Dim omerID As String

            'omerID = Me.ID.Value.ToString
            omerID = Me.RecordId


            If String.IsNullOrEmpty(omerID) Then
                Throw New Exception("OMer Not Found")
            End If
            Dim fileText = String.Empty

            Dim request As New DataFormLoadRequest()
            request.FormID = New Guid(OMEREXPORT_FORMID)
            request.RecordID = omerID
            request.SecurityContext = Me.GetRequestSecurityContext()

            Dim reply As DataFormLoadReply = Nothing
            Try
                reply = DataFormLoad(request, Me.GetRequestContext())
            Catch ex As Exception
                reply = Nothing
            End Try

            If (reply IsNot Nothing) Then
                Dim fieldValue As DataFormFieldValue = Nothing
                If reply.DataFormItem.TryGetValue("FILETEXT", fieldValue) Then
                    Try
                        If fieldValue.Value IsNot Nothing AndAlso fieldValue.Value.ToString().Length > 0 Then
                            fileText = fieldValue.Value.ToString()
                        End If
                    Catch
                    End Try
                End If
            End If
            fileText = fileText.Replace(vbCrLf, vbLf)

            Dim file = fileText.Split(vbCrLf)
            If (Not fileText.Contains(vbCrLf)) Then
                file = fileText.Split(vbLf)
            End If
            Using sr As New IO.StreamWriter(e.Stream)
                For Each x In file
                    sr.WriteLine(x)
                Next
            End Using
        End If
    End Sub

The view data form Spec contains the below mentioned code

                <SPDataForm SPName="USR_USP_DATAFORMTEMPLATE_VIEW_EXPORTOMER">
                                <common:CreateProcedureSQL>
                                                <![CDATA[
create procedure dbo.USR_USP_DATAFORMTEMPLATE_VIEW_EXPORTOMER
(
                @ID uniqueidentifier,
                @DATALOADED bit = 0 output,
  @MYEXPORTFILE varchar(max) = null output
)
as
                set nocount on;
               
                -- be sure to set this, in case the select returns no rows
                set @DATALOADED = 0;
               
                -- populate the output parameters, which correspond to fields on the form.  Note that
                -- we set @DATALOADED = 1 to indicate that the load was successful.  Otherwise, the system
                -- will display a "no data loaded" message.
                select @DATALOADED = 1
   
   
               
                return 0;
                                                ]]>
                                </common:CreateProcedureSQL>
                </SPDataForm>

                <!-- describe fields on the form, which correspond to parameters on the SP.  Note that system paramters
                like the context @ID, @DATALOADED, and @CURRENTAPPUSERID need not be listed. -->
                <common:FormMetaData>
                                <common:FormFields>
      <common:FormField FieldID="ID" Caption="ID" DataType="Guid" Hidden="true" />
      <common:FormField FieldID="MYEXPORTFILE" Caption="File Name" DataType="String" />
                                </common:FormFields>

        <common:WebUIComponent>
            <common:UIModel AssemblyName="Blackbaud.CustomFx.OMer.UIModel.dll" ClassName="Blackbaud.CustomFx.OMer.UIModel.ExportOMerViewDataFormUIModel" />
            <common:WebUI>
                <common:ExternalResource Url="browser/htmlforms/custom/OMER/ExportOMerViewDataForm.html" />
            </common:WebUI>
        </common:WebUIComponent>


    <common:UIActions>
      <common:UIAction ActionID="EXPORT" Caption="Export" />
    </common:UIActions>
  </common:FormMetaData>



Friday, 3 July 2015

Custom PTGL process

Blackbaud CRM 
I had a scenario from one my client that in custom Post to GL process, they want those rows not included in the csv file which contains Zero amount. This can be easily achievable if we have custom class running behind PTGL process which would be calling via Javascript file. But my client only had custom Query view and nothing else and they downloaded their Post to GL file using OOB download file. So that was the problem which i was facing. So, i ran the SQL pro-filer and found one OOB SQL function which solved my issue. That function is called "UFN_POSTTOGLPROCESS_CUSTOMIZE". The original body of this function is

CREATE function [dbo].[UFN_POSTTOGLPROCESS_CUSTOMIZE]   
    (@InRecords UDT_GENERICIDANDBIT readonly, @ID uniqueidentifier)   
    returns @WORKTABLE table   
     (   
     GLTRANSACTIONID uniqueidentifier   
     )      
    with execute as caller   
    as   
    begin   
    /* Products - DO NOT EDIT THIS FUNCTION!  It is only for users to customize their post process*/   
    /* This is a default that just returns the input records */   
       
    insert into @WORKTABLE (GLTRANSACTIONID)   
    select ID from @InRecords   
   
    return   
    end    

So if you analyze above SQL function which is OOB, it only selects all the transaction and return those via table. So I picked that SQL function and alter this SQL function and written my custom logic

ALTER  function [dbo].[UFN_POSTTOGLPROCESS_CUSTOMIZE] 
    (@InRecords UDT_GENERICIDANDBIT readonly, @ID uniqueidentifier) 
    returns @WORKTABLE table 
     ( 
     GLTRANSACTIONID uniqueidentifier 
     )    
    with execute as caller 
    as 
    begin 
    /* Products - DO NOT EDIT THIS FUNCTION!  It is only for users to customize their post process*/ 
    /* This is a default that just returns the input records */ 
   
                insert into @WORKTABLE (GLTRANSACTIONID) 
             select ID from @InRecords  R
                where R.ID in (select ID from GLTRANSACTION where ID = R.ID and AMOUNT <> 0)
                 
 
    return 
    end  


I had filtered only those rows in which amount is not equal to 0 that discarded those rows which contains Zero amount. Because we don’t have any control only if we make a query view, other solution would offcourse a custom class library and filtered all those rows which contains amount equal to zero