Clear the value of a field on a form if a relationship is deleted

matthew_hughes
Kilo Sage

On the below form, I'm wanting to a create a business rule that removes the value of the Primary Application field if the there isn't a relationship for that selected Primary Application within the Business Applications tab:

 

matthew_hughes_0-1702541368060.png

 

The Business Applications tab automatically creates an entry between the markets request and a business application if the Primary Application field is populated. However, I'm now looking at a way for Primary Application field to be cleared out if the relationship is removed. I've written the following after business rule:

 

matthew_hughes_2-1702541606753.png

 

 

matthew_hughes_1-1702541574598.png

 

I was just wondering if somebody could advise what I need to do to empty the Primary Application field when the relationship between the markets request and the Primary Application field is deleted.

6 REPLIES 6

Gurpreet07
Mega Sage

select advanced and then select When to run = before and delete checked.

Hi @Gurpreet07 

 

I've tried the following code, but I can't get it to work:

(function executeRule(current, previous /*null when async*/ ) {

    //Search within the Markets Request-Business Applications table
    var deleteRelations = new GlideRecord('x_lbg_mr_markets_request');
    //Search for records where the 'Markets Request' field is the current Markets Request
    deleteRelations.addQuery('sys_id', current.getValue('markets_request'));
    deleteRelations.query();
    //Clear the Primary Application field
    while(deleteRelations.next()) {
        deleteRelations.u_primary_application = "";
        deleteRelations.u_application_not_applicable = true;
        deleteRelations.update();
    }

   
})(current, previous);
 
This is the field name from the relationships table:
 
matthew_hughes_0-1702545147104.png