Using DeleteRecord in a table-level script

This example uses the DeleteRecord() function in a table-level script. The script checks whether there are any outstanding leads associated with a person when a Person record is deleted. If there are any outstanding leads, a message is displayed.

  1. Go to <My Profile> | Administration | Customization | Person | TableScripts.
  2. Click New, and then create a new table-level script.
    For more information about the options you need to complete, see Creating a script.
    When creating your script, do the following:
    • From Script Type, select Table Level.
    • In Table Script, within the function DeleteRecord section, enter the following script:

  3. function DeleteRecord() {
    var ThisPersonId = CRM.GetContextInfo('person','pers_personid');
    var sCriteria = "lead_primarypersonid="+ThisPersonId+" and
    lead_deleted is null and lead_status <> 'Opportunity'";
    var LeadRecord = CRM.FindRecord("Lead",sCriteria);
    if(!LeadRecord.eof) { CheckLocks=false;
    ErrorStr='There are outstanding leads';
    Valid = false; } }
  4. Click Save.

Open a Person record that has an associated Lead and then delete the Person record. The warning message is displayed.