(defun my/org-insert-link-and-ref ()
"effectively duplicates the org-insert-link function and adds a ref (a link to the file the
linked-entry is pasted into) in the linked-entries property drawer."
(interactive)
(let* ((buf (current-buffer))
(current-heading-id (org-id-get-create))
(current-heading (format "%s" (org-get-heading t t t t)))
(heading-of-inserted-link (format "%s" (cadr (car org-stored-links))))
(id-of-inserted-link (substring-no-properties (car (car org-stored-links))))
(file-of-inserted-link (car (org-id-find (cadr (split-string id-of-inserted-link "id:")))))
(buffer-of-inserted-link (org-find-base-buffer-visiting file-of-inserted-link))
(marker-to-inserted-link (org-find-exact-headline-in-buffer heading-of-inserted-link buffer-of-inserted-link))
(marker-to-current-link (org-find-exact-headline-in-buffer current-heading buf)))
(with-current-buffer (get-buffer-create buffer-of-inserted-link)
;; check the REFS for existence/enries
(if (org-entry-get marker-to-inserted-link "REFS")
;; check if the id has already been inserted (avoid placing the same ref)
(if (not (string-match-p current-heading-id (format "%s" (org-entry-get marker-to-inserted-link "REFS"))))
;; if there's no ref already in the property, append it
(org-entry-put marker-to-inserted-link "REFS"
(format "%s //// [[id:%s][%s]]"
(org-entry-get marker-to-inserted-link "REFS") current-heading-id current-heading)))
;; else, place the ref
(org-entry-put marker-to-inserted-link "REFS"
(format "[[id:%s][%s]]" current-heading-id current-heading)))
(save-buffer))
(insert (format "[[id:%s][%s]]" id-of-inserted-link heading-of-inserted-link))))