use case mandatory and conditional sciript.

sudhars
Tera Contributor

 

If the user has the role project_manager, additional fields like budget_approval and risk_assessment should be displayed. For users without this role, these fields should be hidden.

If the project_type field is set to external, then the client_contact field should become mandatory. If the project_type is internal, the internal_sponsor field should be mandatory instead.

 

3 ACCEPTED SOLUTIONS

Zach Koch
Giga Sage

You would need an onChange client script that would look something like this

 

 

 

 

  if (g_user.hasRole('project_manager')) {
        if (newValue == "external") {
            if (oldValue == 'internal') {
                g_form.setDisplay('internal_sponsor', false);
                g_form.setMandatory('internal_sponsor', false);
            }
            g_form.setDisplay('client_contact', true);
            g_form.setMandatory('client_contact', true);
        } else if (newValue == "internal") {
            if (oldValue == 'external') {
                g_form.setDisplay('client_contact', false);
                g_form.setMandatory('client_contact', false);
            }
            g_form.setDisplay('internal_sponsor', true);
            g_form.setMandatory('internal_sponsor', true);
        }
    }

 

 

 

 

And if you wanted those fields hidden from the start, you'd need another Client script onLoad to hide them. You could also use a UI policy and uncheck the Reverse if false

View solution in original post

Its_Azar
Mega Sage

Hi There @sudhars 

I think you can use a single client script for both.

function onLoad() {
  
    var isProjectManager = g_user.hasRole('project_manager');
  
    g_form.setDisplay('budget_approval', isProjectManager);
    g_form.setDisplay('risk_assessment', isProjectManager);
    
   
    setMandatoryFields(g_form.getValue('project_type'));
}

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    
 
    setMandatoryFields(newValue);
}

function setMandatoryFields(projectType) {
    if (projectType === 'external') {
        g_form.setMandatory('client_contact', true);
        g_form.setMandatory('internal_sponsor', false);
    } else if (projectType === 'internal') {
        g_form.setMandatory('client_contact', false);
        g_form.setMandatory('internal_sponsor', true);
    } else {
       
        g_form.setMandatory('client_contact', false);
        g_form.setMandatory('internal_sponsor', false);
    }
}

If his helps kindly accept the response thanks.

 

View solution in original post

Its_Azar
Mega Sage

Hi There @sudhars 

I think you can use a single client script for both.

function onLoad() {
  
    var isProjectManager = g_user.hasRole('project_manager');
  
    g_form.setDisplay('budget_approval', isProjectManager);
    g_form.setDisplay('risk_assessment', isProjectManager);
    
   
    setMandatoryFields(g_form.getValue('project_type'));
}

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    
 
    setMandatoryFields(newValue);
}

function setMandatoryFields(projectType) {
    if (projectType === 'external') {
        g_form.setMandatory('client_contact', true);
        g_form.setMandatory('internal_sponsor', false);
    } else if (projectType === 'internal') {
        g_form.setMandatory('client_contact', false);
        g_form.setMandatory('internal_sponsor', true);
    } else {
       
        g_form.setMandatory('client_contact', false);
        g_form.setMandatory('internal_sponsor', false);
    }
}

If his helps kindly accept the response thanks.

View solution in original post

4 REPLIES 4

Zach Koch
Giga Sage

You would need an onChange client script that would look something like this

 

 

 

 

  if (g_user.hasRole('project_manager')) {
        if (newValue == "external") {
            if (oldValue == 'internal') {
                g_form.setDisplay('internal_sponsor', false);
                g_form.setMandatory('internal_sponsor', false);
            }
            g_form.setDisplay('client_contact', true);
            g_form.setMandatory('client_contact', true);
        } else if (newValue == "internal") {
            if (oldValue == 'external') {
                g_form.setDisplay('client_contact', false);
                g_form.setMandatory('client_contact', false);
            }
            g_form.setDisplay('internal_sponsor', true);
            g_form.setMandatory('internal_sponsor', true);
        }
    }

 

 

 

 

And if you wanted those fields hidden from the start, you'd need another Client script onLoad to hide them. You could also use a UI policy and uncheck the Reverse if false

Not applicable

Hi @sudhars ,

1) For role specific visibility of fields you can use this script.

(function() {
    // Check if the user has the project_manager role
    var hasProjectManagerRole = g_user.hasRole('project_manager');

    // If the user has the role, display budget_approval and risk_assessment fields
    if (hasProjectManagerRole) {
        g_form.setDisplay('budget_approval', true);
        g_form.setDisplay('risk_assessment', true);
    } 
    // If the user does not have the role, hide these fields
    else {
        g_form.setDisplay('budget_approval', false);
        g_form.setDisplay('risk_assessment', false);
    }
})();

 

2) For The related fields i.e making fields conditionally mandatory, use the below script logic:

(function executeRule(current, previous /*null when async*/) {
    // Get the current project type
    var projectType = g_form.getValue('project_type');

    // If the project type is external, make client_contact mandatory and internal_sponsor not mandatory
    if (projectType === 'external') {
        g_form.setMandatory('client_contact', true);
        g_form.setMandatory('internal_sponsor', false);
    } 
    // If the project type is internal, make internal_sponsor mandatory and client_contact not mandatory
    else if (projectType === 'internal') {
        g_form.setMandatory('client_contact', false);
        g_form.setMandatory('internal_sponsor', true);
    } 
    // If the project type is something else or empty, set both fields to not mandatory
    else {
        g_form.setMandatory('client_contact', false);
        g_form.setMandatory('internal_sponsor', false);
    }
})(current, previous);

 

Thanks,

Hope this helps.

If my response turns helpful, mark it helpful and accept solution.

   

Its_Azar
Mega Sage

Hi There @sudhars 

I think you can use a single client script for both.

function onLoad() {
  
    var isProjectManager = g_user.hasRole('project_manager');
  
    g_form.setDisplay('budget_approval', isProjectManager);
    g_form.setDisplay('risk_assessment', isProjectManager);
    
   
    setMandatoryFields(g_form.getValue('project_type'));
}

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    
 
    setMandatoryFields(newValue);
}

function setMandatoryFields(projectType) {
    if (projectType === 'external') {
        g_form.setMandatory('client_contact', true);
        g_form.setMandatory('internal_sponsor', false);
    } else if (projectType === 'internal') {
        g_form.setMandatory('client_contact', false);
        g_form.setMandatory('internal_sponsor', true);
    } else {
       
        g_form.setMandatory('client_contact', false);
        g_form.setMandatory('internal_sponsor', false);
    }
}

If his helps kindly accept the response thanks.

 

Its_Azar
Mega Sage

Hi There @sudhars 

I think you can use a single client script for both.

function onLoad() {
  
    var isProjectManager = g_user.hasRole('project_manager');
  
    g_form.setDisplay('budget_approval', isProjectManager);
    g_form.setDisplay('risk_assessment', isProjectManager);
    
   
    setMandatoryFields(g_form.getValue('project_type'));
}

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    
 
    setMandatoryFields(newValue);
}

function setMandatoryFields(projectType) {
    if (projectType === 'external') {
        g_form.setMandatory('client_contact', true);
        g_form.setMandatory('internal_sponsor', false);
    } else if (projectType === 'internal') {
        g_form.setMandatory('client_contact', false);
        g_form.setMandatory('internal_sponsor', true);
    } else {
       
        g_form.setMandatory('client_contact', false);
        g_form.setMandatory('internal_sponsor', false);
    }
}

If his helps kindly accept the response thanks.