Insert the xref first to get the name, then create / modify the layer accordingly.
Code:
(defun c:InsertXref ( / oldlayer dxf_ent name_xref tbl_layu )
(setq oldlayer (getvar "CLAYER")) ;get the current layer first
(initdia)
(command "_.-XREF" "_Overlay")
(if (> (getvar "CMDACTIVE") 0)
(progn
(command "_None" '(0.0 0.0 0.0))
(while (> (getvar "CMDACTIVE") 0) (command ""))
)
)
(setq name_xref (cdr (assoc 2 (setq dxf_ent (entget (entlast))))))
(entmod (subst (cons 8 (strcat "Zz_30_30_ExternalReferences_" name_xref)) (assoc 8 dxf_ent) dxf_ent))
(setq tbl_lay (entget (tblobjname "LAYER" (strcat "Zz_30_30_ExternalReferences_" name_xref))))
(entmod (subst (cons 62 7) (assoc 62 tbl_lay) tbl_lay))
(princ)
(setvar "CLAYER" oldlayer)
(command "_.regenall")
(command "_.zoom" "_Extent")
)