Está en la página 1de 5

SAP BO. SAP Business One.

SAP SBO_SP_TransactionNotification usando CLR


Posted On lunes, 7 de marzo de 2011 at en 11:44 by Edgar Avena Vázquez

En el último post vimos como hacer algunas excepciones en las reglas de negocio en nuestro
sistema mediante el store SBO_SP_TransactionNotification. Ahora bien las cosas que
podríamos hacer mediante SQL suenan un poco escasas. Ahora supongamos que quisiéramos
enviar un correo a nuestros clientes cuando capturemos una entrega de mercancía, para
informarle que su pedido a sido liberado y hacer esto solo usando el
SBO_SP_TransactionNotification.

Y el método que vamos a usar para hacer esto es usando CLR de Visual Studio,
ayudándonos con el DI API de SAP Business One. A continuación nos vamos a ir directo a
nuestro Visual Studio y crearemos un nuevo proyecto en C#. En mi caso el proyecto tiene el
nombre SimpleDeliveryNotification después de esto vamos agregar la referencia al DI API de
SAP Business One. En este ejemplo yo use el DI API del 8.8. Ahora bien crearemos nuestro CLR
para SQL. De la siguiente manera:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using SAPbobsCOM;

using System.Data;

using System.Data.SqlTypes;

using System.Data.SqlClient;

using Microsoft.SqlServer.Server;

namespace SimpleDeliveryNotification

public class DeliveryNotification

public static void SendNotification(SqlString OrderNumber, SqlString Email, out SqlString


Msg)

Msg = "";

SAPbobsCOM.Messages oMsg = null;

SAPbobsCOM.Company oComp = new SAPbobsCOM.Company();


try

oComp.CompanyDB = "";

oComp.Server = "SAPBO";

oComp.CompanyDB = "BDCN-2011 - PRUEBAS";

oComp.UseTrusted = true;

oComp.DbServerType = BoDataServerTypes.dst_MSSQL2008;

oComp.UserName = "manager";

oComp.Password = "asdqwe";

if (oComp.Connect() != 0)

Msg = string.Format("{0}-{1} conecta server", oComp.GetLastErrorCode(),


oComp.GetLastErrorDescription());

return;

oMsg =
(SAPbobsCOM.Messages)oComp.GetBusinessObject(BoObjectTypes.oMessages);

oMsg.Subject = @"Notificacion de surtido";

oMsg.MessageText = @"Su orden a sido surtida de nuestros almacenes.\n ";

oMsg.MessageText += string.Format("Numero de orden: {0}\n\n Atte. Mi empresa.",


OrderNumber);

oMsg.Recipients.Add();

oMsg.Recipients.SetCurrentLine(0);

oMsg.Recipients.SendEmail = BoYesNoEnum.tYES;

oMsg.Recipients.EmailAddress = (string)Email;

oMsg.Recipients.UserType = BoMsgRcpTypes.rt_RandomUser;

oMsg.Recipients.UserCode = @"1";

if (oMsg.Add() != 0)

Msg = string.Format("{0}-{1} envia correo", oComp.GetLastErrorCode(),


oComp.GetLastErrorDescription());
return;

catch (Exception ex)

Msg += string.Format("{0} error", ex.Message);

return;

finally

oMsg = null;

oComp.Disconnect();

oComp = null;

Ahora bien después de tener este código generamos nuestro DLL y lo vamos a colocar en
una carpeta que nos sea de fácil acceso, recuerden ahí que copiar la DLL y el DLL del DI API
("Interop.SAPbobsCOM.dll") Una vez que hicimos esto vamos a configurar nuestro servidor
SQL para que acepte nuestro CLR. Y lo haremos de la siguiente manera:

sp_configure 'clr enable', 1

go

reconfigure

go

alter database [BDCN-2011 - PRUEBAS] set trustworthy on

go
Después de habilitar la opción de CLR en nuestro SQL vamos por creación de nuestro
assembly y el store procedure que hará el llamado de nuestra función.

Create Assembly SimpleDeliveryNotification Authorization dbo From

'C:\CLR\SimpleDeliveryNotification.dll'

With Permission_Set = Unsafe

Go

Create Proc USP_SendDelivery_Notification

@Email nvarchar(100),

@OrderNumber nvarchar(100),

@msg nvarchar(max) Output

As External Name SimpleDeliveryNotification.


[SimpleDeliveryNotification.DeliveryNotification].SendNotification

Go

Una vez que llegamos a este punto hemos casi finalizado, nuestro siguiente paso seria
probarlo y esto lo podemos hacer de la siguiente manera dentro de nuestro SQL probandolo
de la siguiente manera

declare @msg nvarchar(max)

exec USP_SendDelivery_Notification 'li.edgaravena@gmail.com', 'Order 123', @msg output

print @msg

Ahora bien una vez que ya llegamos a este punto solo falta modificar nuestro
SBO_SP_TransactionNotification y lo vamos hacer agregando el siguiente código.

if(@transaction_type = 'A' and @object_type = '15'=

begin

declare @email nvarchar(100)

set @email = (select T0.E_Mail from OCRD T0 inner join ODLN T1

on T0.CardCode = T1.CardCode where T1.DocEntry = @list_of_cols_val_tab_del)

declare @NumCard nvarchar(100) )


set @NumCard = (select T0.NumAtCard From ODLN T0 where T0.DocEntry =
@list_of_cols_val_tab_del)

if ISNULL(@email,'') != '' and ISNULL(@NumCard,'') != 0

begin

declare @msg nvarchar(max)

exec USP_SendDelivery_Notification @email, @NumCard, @msg output

if ISNULL(@msg,'') != ''

begin

set @error = -1

set @error_message = @msg

end

end

end

También podría gustarte