1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
CREATE TRIGGER [x_lead_links_xml_imports]
ON [SYSADM].[X_Lead_Links]
AFTER INSERT
AS
DECLARE
@LeaNrid DECIMAL(15,0),
@cpt_nrid int,
@test VARCHAR(50),
@CompanyName VARCHAR(50),
@CompanyPC VARCHAR(50),
@ContactFN VARCHAR(50),
@ContactLN VARCHAR(50),
@ExistingCompany decimal, -- linked company/contact information
@ExistingCompanyKN decimal,
@ExistingContactFN decimal,
@ExistingContactLN decimal,
@TestExistingCompany varchar(10),
@TestExistingContact varchar(10),
@strTable varchar(3), --fctRID parameters
@societe varchar(50),
@societe_type varchar(50),
@societe_city varchar(50),
@societe_street varchar(50),
@so0_nrid decimal,
@societe_PC varchar(10),
@personne varchar(50),
@pe0_nrid decimal,
@nrid decimal
BEGIN
DECLARE curs_Ins CURSOR FOR
SELECT nrid, lea0_nrid, societe_lead , societe_exist, personne_lead, personne_exist, societe_street from inserted
OPEN curs_Ins
FETCH curs_Ins INTO @nrid, @LeaNrid, @CompanyName, @CompanyPC, @ContactFN, @ContactLN, @test
WHILE @@FETCH_STATUS = 0
BEGIN
IF (@test = 'xml_trigger')
BEGIN
SET @ExistingCompany = null
SET @ExistingCompanyKN = null
SET @ExistingContactFN = null
SET @ExistingContactLN = null
SET @ExistingCompany = (select top 1 nrid from sysadm.so0 where societe = @CompanyName and code_post = @CompanyPC and template is null)
SET @ExistingCompanyKN = (select top 1 nrid from sysadm.so0 where SOCIETE_2 = @CompanyName and code_post = @CompanyPC and template is null)
SET @ExistingContactFN = (select top 1 nrid from sysadm.pe0 where prenom = @ContactFN and template is null)
SET @ExistingContactLN = (select top 1 nrid from sysadm.pe0 where personne = @ContactLN and template is null)
-- Insert data initialization
SET @TestExistingCompany=(select lay.message from sysadm.set_lay lay where lay.identify = 'No' and applic_id=100000000000001)
SET @TestExistingContact=(select lay.message from sysadm.set_lay lay where lay.identify = 'Ignore' and applic_id=100000000000001)
SET @societe=null
SET @so0_nrid=null
SET @personne=null
SET @pe0_nrid=null
SET @societe_PC=null
IF @ExistingCompany is not null
BEGIN
SET @TestExistingCompany=(select lay.message from sysadm.set_lay lay where lay.identify = 'Yes' and applic_id=100000000000001)
SET @societe=@CompanyName
SET @so0_nrid=@ExistingCompany
SET @societe_type=(select type from sysadm.so0 where nrid=@ExistingCompany)
SET @societe_city=(select loc from sysadm.so0 where nrid=@ExistingCompany)
SET @societe_street=(select adresse from sysadm.so0 where nrid=@ExistingCompany)
SET @societe_PC=@CompanyPC
END
ELSE IF @ExistingCompanyKN is not null
BEGIN
SET @TestExistingCompany=(select lay.message from sysadm.set_lay lay where lay.identify = 'Ignore' and applic_id=100000000000001)
SET @societe=@CompanyName
SET @so0_nrid=@ExistingCompany
SET @societe_type=(select type from sysadm.so0 where nrid=@ExistingCompany)
SET @societe_city=(select loc from sysadm.so0 where nrid=@ExistingCompany)
SET @societe_street=(select adresse from sysadm.so0 where nrid=@ExistingCompany)
SET @societe_PC=@CompanyPC
END
IF @ExistingContactFN is not null
BEGIN
IF @ExistingContactLN is not null
BEGIN
SET @TestExistingContact=(select lay.message from sysadm.set_lay lay where lay.identify = 'Yes' and applic_id=100000000000001)
SET @personne=@ContactFN + ' ' + @ContactLN
SET @pe0_nrid=@ExistingContactLN
END
ELSE
BEGIN
SET @TestExistingContact=(select lay.message from sysadm.set_lay lay where lay.identify = 'Ignore' and applic_id=100000000000001)
SET @personne=@ContactFN + ' ' + (SELECT personne FROM sysadm.pe0 where nrid = @ExistingContactFN)
SET @pe0_nrid=@ExistingContactLN
END
END
ELSE IF @ExistingContactLN is not null
BEGIN
SET @TestExistingContact=(select lay.message from sysadm.set_lay lay where lay.identify = 'Ignore' and applic_id=100000000000001)
SET @personne=(SELECT prenom FROM sysadm.pe0 where nrid = @ExistingContactFN) + ' ' + @ContactLN
SET @pe0_nrid=@ExistingContactLN
END
/* print 'Lead NRID : ' + convert(char,@LeaNrid)
print 'Company : ' + @CompanyName
print 'Company PC : ' + @CompanyPC
print 'Contact FN : ' + @ContactFN
print 'Contact LN : ' + @ContactLN
print 'Test Company : ' + @TestExistingCompany
print 'Test Contact : ' + @TestExistingContact */
/* print 'NRID : '+ convert(char,@nNRID)
print 'RID : '+ @strRID
print 'RMOD : '+ @strRMOD
print '@strDMOD' + convert(char,@strDMOD) */
UPDATE sysadm.x_lead_links SET societe = @societe,
societe_exist = @TestExistingCompany,
so0_nrid = @so0_nrid,
societe_type =@societe_type,
societe_postalcode = @societe_PC,
societe_city = @societe_city,
societe_street = @societe_street,
personne_exist = @TestExistingContact,
personne = @personne,
personne_lead = (@ContactFN + ' ' + @ContactLN),
pe0_nrid = @pe0_nrid
where nrid = @nrid
END
FETCH curs_Ins INTO @nrid, @LeaNrid, @CompanyName, @CompanyPC, @ContactFN, @ContactLN, @test
END
CLOSE curs_Ins
DEALLOCATE curs_Ins
END |
Partager