SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION SAILPOINT.ES_V2_REMOVE_MAPICS_USER ( 
	ENVID CHAR(2) , 
	USERNAME CHAR(10) ) 
	RETURNS CLOB(2147483647)   
	LANGUAGE SQL 
	SPECIFIC SAILPOINT.V2_ESRMVMUSR 
	NOT DETERMINISTIC 
	MODIFIES SQL DATA 
	CALLED ON NULL INPUT 
	NOT FENCED 
	SET OPTION  ALWBLK = *ALLREAD , 
	ALWCPYDTA = *OPTIMIZE , 
	COMMIT = *NONE , 
	DECRESULT = (31, 31, 00) , 
	DLYPRP = *NO , 
	DYNDFTCOL = *NO , 
	DYNUSRPRF = *USER , 
	SRTSEQ = *HEX   
	BEGIN 
DECLARE LIBL VARCHAR ( 1024 ) DEFAULT '' ; 
DECLARE CUR_USR VARCHAR ( 10 ) DEFAULT '' ; 
SET CISTOOLS . LIBL = CISTOOLS . SET_LIBRARY_LIST ( ENVID ) ; 
-- Remove XA user record 
CALL SAILPOINT . DELETE_ES_USER ( P_USER => USERNAME ) ; 
-- Release XA License 
CALL SAILPOINT . REVOKE_XA_LICENSE ( ) ; 
-- Delete user from Mapics files if exists 
DELETE FROM MZGUCPP WHERE GUBUCD = USERNAME ; 
DELETE FROM MZF0REP WHERE F0BUCD = USERNAME ; 
-- Write a history record 
INSERT INTO ESDETAIL 
VALUES ( ENVID , USERNAME , '' , '' , 'ACCMAPICS' , '' , 'R' , '10' , 
CURRENT_DATE , CURRENT_TIME , CURRENT_USER , 'ESRMVMUSR' , 
'SAILPOINT' , CISTOOLS . TO_CYMD ( ) , ZONED ( CURRENT_TIME ) , 
'Deleted via Sailpoint API' , '' , '' , '' , '' , '' ) ; 
RETURN 
( 
VALUES JSON_OBJECT ( 
'data' : JSON_ARRAY ( JSON_OBJECT ( 
'message' VALUE 
'Mapics user record deleted and license revoked' 
) ) , 
'errors' : JSON_ARRAY ( ) 
) ) ; 
END  ; 

GRANT ALTER , EXECUTE   
ON SPECIFIC FUNCTION SAILPOINT.V2_ESRMVMUSR 
TO AMAPICS WITH GRANT OPTION ; 

GRANT EXECUTE   
ON SPECIFIC FUNCTION SAILPOINT.V2_ESRMVMUSR 
TO PUBLIC ; 

