import("Sql_lib"); import("Entity_lib"); import("Employee_lib"); import("Context_lib"); import("system.vars"); import("system.result"); var canDelete = false; var personId = vars.get("$field.PERSONID"); var personCount = newSelect("count(*)") .from("CONTACT") .join("PERSON", "PERSON.PERSONID = CONTACT.PERSON_ID") .where("PERSON.PERSONID", personId) .cell(); //only delete when the contact doesnt have any functions, because that would currently lead to the person and all his functions being deleted, // since there is only one person contact behind all the contacts. //ToDo: come back to this and implement the solution for deleting a function (#1059167) once it'S decided how that's being implemented if(personCount == 1) { var contactId = vars.get("$field.CONTACTID"); var currentContext = ContextUtils.getCurrentContextId(); canDelete = new HasLinkedObjectTester() .andNoEntityRows("Activity_entity", "LinkedObjects", {ObjectId_param : currentContext, RowId_param : contactId}) //Activities .andNoEntityRows("Task_entity", "Tasks", {ObjectId_param : currentContext, RowId_param : contactId}) //Tasks .andNoEntityRows("Document_entity", "Documents", {AssignmentTable_param : "CONTACT", AssignmentRowId_param : contactId}) //Documents .and(function () {return !EmployeeUtils.isUser(contactId);}) .andNoEntityRows("Offer_entity", "ContactOffers", {ContactId_param : contactId}) //Offers .andNoEntityRows("Contract_entity", "Contracts", {ContactId_param : contactId}) //Contracts .andNoEntityRows("ObjectTree_entity", "TreeProvider", {ObjectIds_param : JSON.stringify([contactId, vars.get("$field.PERSON_ID")]), ObjectTypes_param : JSON.stringify([currentContext, "PrivatePerson"])}) .andNoEntityRows("CampaignParticipant_entity", "CampaignParticipantsProvider", {ContactId_param : contactId}) .andNoEntityRows("Order_entity", "Orders", {ContactId_param : contactId}) .validate(); } else if (personCount == 0) //special case in QuickEntry where it should be possible to remove a new Person that is not saved in the db yet canDelete = true; result.string(canDelete);