Sorry, kind of talking to myself here again , but this is what I have come up with - maybe someone else will find it useful (or just very confusing!). It seems to work OK, but I have no idea if there is a much easier/quicker/nicer way of doing it; so I'd love to hear if anyone has any better way of doing this.
The idea is if the Customer attribute on a CI record is populated, then if the CI's Location and Organisation is blank, or is different to the Customer's Location and Organisation it will automatically update the CI's Location and Organisation to match the Customers when our SCCM integration runs.
#EXECUTE #SQLSERVER FEDCMDBUPDATE STORED PROC
CREATE PROCEDURE FEDCMDBUPDATE
@MODE VARCHAR(255),
@SOURCE_REF INT,
@TYPE_REF INT,
@GROUP_REF INT,
@MAPPING_REF INT,
@RESOURCE_REF INT,
@RESOURCE_ID VARCHAR(MAX),
@RESOURCE_BACKUPID VARCHAR(MAX),
@RESOURCE_TYPEID VARCHAR(MAX),
@RESOURCE_DISPLAY VARCHAR(MAX),
@LINKED_CORE_ENTITY INT,
@LINKED_ITEM_REF INT,
@STOP INT
AS
DECLARE @PERSON_REF INT
DECLARE @PERSON_LOCATION_REF INT
DECLARE @PERSON_ORG_REF INT
DECLARE @CI_LOCATION_REF INT
DECLARE @CI_ORG_REF INT
-- Audit variables
DECLARE @LOCATION_UPDATED INT = 0
DECLARE @ORG_UPDATED INT = 0
DECLARE @SU_CMDB_HISTORY_REF INT
DECLARE @SU_EXTENSION_AUDIT_REF INT
DECLARE @NEW_LOCATION_TEXT VARCHAR(MAX)
DECLARE @OLD_LOCATION_TEXT VARCHAR(MAX)
DECLARE @NEW_ORG_TEXT VARCHAR(MAX)
DECLARE @OLD_ORG_TEXT VARCHAR(MAX)
BEGIN
IF (@MODE = 'POST')
BEGIN
IF (@SOURCE_REF IN (4, 5) AND @LINKED_CORE_ENTITY = 4 AND @LINKED_ITEM_REF <> 0)
BEGIN
SELECT @PERSON_REF = USER_REF FROM AR_PRIMARY_ASSET WHERE ITEM_REF = @LINKED_ITEM_REF
SELECT @PERSON_LOCATION_REF = LOCATION_REF FROM AR_PERSON WHERE REF = @PERSON_REF
SELECT @CI_LOCATION_REF = LOCATION_REF FROM AR_PRIMARY_ASSET WHERE ITEM_REF = @LINKED_ITEM_REF
IF ((@CI_LOCATION_REF = 0 OR (@PERSON_LOCATION_REF <> @CI_LOCATION_REF)) AND @PERSON_REF <> 0)
BEGIN
UPDATE AR_PRIMARY_ASSET SET LOCATION_REF = @PERSON_LOCATION_REF WHERE ITEM_REF = @LINKED_ITEM_REF AND (LOCKED_BY = 0 OR LOCKED_BY = -1)
SELECT @LOCATION_UPDATED = 1
END
SELECT @PERSON_ORG_REF = ORGANIZATION_REF FROM AR_PERSON WHERE REF = @PERSON_REF
SELECT @CI_ORG_REF = CLIENT_COMPANY_REF FROM AR_PRIMARY_ASSET WHERE ITEM_REF = @LINKED_ITEM_REF
IF ((@CI_ORG_REF = 0 OR (@PERSON_ORG_REF <> @CI_ORG_REF)) AND @PERSON_REF <> 0)
BEGIN
UPDATE AR_PRIMARY_ASSET SET CLIENT_COMPANY_REF = @PERSON_ORG_REF WHERE ITEM_REF = @LINKED_ITEM_REF AND (LOCKED_BY = 0 OR LOCKED_BY = -1)
SELECT @ORG_UPDATED = 1
END
-- Update Audit
IF ((@LOCATION_UPDATED = 1) OR (@ORG_UPDATED = 1))
BEGIN
SELECT @SU_CMDB_HISTORY_REF = NUMBER_ALLOC FROM SU_NUMBER_ALLOC WHERE TABLE_ALLOC = 'SU_CMDB_HISTORY'
UPDATE SU_NUMBER_ALLOC SET NUMBER_ALLOC = (@SU_CMDB_HISTORY_REF + 1) WHERE TABLE_ALLOC = 'SU_CMDB_HISTORY'
INSERT INTO SU_CMDB_HISTORY (REF, CORE_ENTITY, REF_NO, HISTORY_TYPE, ACTION_HEADLINE, ACTION_DESC, ACTION_DESC_HTML, FORMAT, ACTION_DATETIME_USER, ACTION_DATETIME_PHYS, OFFICER_REF, SOURCE) VALUES (@SU_CMDB_HISTORY_REF, 4, @LINKED_ITEM_REF, 0, 'Updated', '','', 2, GETDATE(), GETDATE(), 2, -22)
SELECT @SU_EXTENSION_AUDIT_REF = NUMBER_ALLOC FROM SU_NUMBER_ALLOC WHERE TABLE_ALLOC = 'SU_EXTENSION_AUDIT'
UPDATE SU_NUMBER_ALLOC SET NUMBER_ALLOC = (@SU_EXTENSION_AUDIT_REF + 4) WHERE TABLE_ALLOC = 'SU_EXTENSION_AUDIT'
-- Add Location Audit
IF (@LOCATION_UPDATED = 1)
BEGIN
SELECT @NEW_LOCATION_TEXT = LOCATION_NAME FROM AR_LOCATIONS WHERE LOCATION_REF = @PERSON_LOCATION_REF
SELECT @OLD_LOCATION_TEXT = LOCATION_NAME FROM AR_LOCATIONS WHERE LOCATION_REF = @CI_LOCATION_REF
INSERT INTO SU_EXTENSION_AUDIT (REF, ACTION_TYPE, REF_NO, HISTORY_ORDER, VALUE_REF, OLD_VALUE_REF, FIELD_REF, VALUE_DISPLAY, OLD_VALUE_DISPLAY, CORE_ENTITY) VALUES (@SU_EXTENSION_AUDIT_REF, 0, @LINKED_ITEM_REF, @SU_CMDB_HISTORY_REF, @PERSON_LOCATION_REF, @CI_LOCATION_REF, 206, @NEW_LOCATION_TEXT, @OLD_LOCATION_TEXT, 4)
SELECT @SU_EXTENSION_AUDIT_REF = @SU_EXTENSION_AUDIT_REF + 1
END
-- Add Organisation Audit
IF (@ORG_UPDATED = 1)
BEGIN
SELECT @NEW_ORG_TEXT = CLIENT_CO_NAME FROM AR_CLIENT_COMPANY WHERE CLIENT_CO_REF = @PERSON_ORG_REF
SELECT @OLD_ORG_TEXT = CLIENT_CO_NAME FROM AR_CLIENT_COMPANY WHERE CLIENT_CO_REF = @CI_ORG_REF
INSERT INTO SU_EXTENSION_AUDIT (REF, ACTION_TYPE, REF_NO, HISTORY_ORDER, VALUE_REF, OLD_VALUE_REF, FIELD_REF, VALUE_DISPLAY, OLD_VALUE_DISPLAY, CORE_ENTITY) VALUES (@SU_EXTENSION_AUDIT_REF, 0, @LINKED_ITEM_REF, @SU_CMDB_HISTORY_REF, @PERSON_ORG_REF, @CI_ORG_REF, 211, @NEW_ORG_TEXT, @OLD_ORG_TEXT, 4)
SELECT @SU_EXTENSION_AUDIT_REF = @SU_EXTENSION_AUDIT_REF + 1
END
END
END
SELECT @STOP = 0
END
ELSE
SELECT @STOP = 0
END
The only issue I have with this now is it only works when the Integration setting is set to Full, when we ideally want it set to Incremental, so just going to try and work on that now...