Saturday, May 4, 2019

Set a Custom Form on View Mode Depending on a Value of a Record Field

Users may have a need to show or hide fields in a record depending on a value of a particular field in the same record. For scenarios that there are multiple fields and form elements, user may opt in creating different Custom Entry or Transaction Forms. These custom forms should be consistent regardless of the Type. For Types Create and Edit, users can easily utilize Client Scripts particularly the Page Init Function, however it is not triggered when the record is in View type.

This sample can be used to manage using a Custom Entry Form based on a value of a certain field.

PREREQUISITE: Knowledge in SuiteScript

In this scenario is a Case Record when the status is Escalated a particular Custom Entry form will be used:

STEP A. Prepare a Custom Entry Field for a flag while will prevent undesired looping

1. Navigate to (Administrator)  Customization> Lists, Records, & Fields> CRM Fields> New
2. Set the following values:

Label = Case_Status_Flag
ID = _case_type_flag
Type = Free-Form-Text
Store Value = checked
Applies To> Case = checked
Display> Subtab = Main
Display> Display Type = Hidden

3. Click on Save

STEP B. Create a User Event Script

1. Navigate to (Administrator)  Customization> Scripting>Scripts> New
2. Click on User Event
3. Set the following values:

Name = UES_Set_Case_Form_Conditional
Deployments> Applies To = Case
Scripts> Before Load Function = caseBeforeLoad
Script File = upload a javascript file with the following content

/* -------------------- START -------------------- */
function updateStatus_Form_Flag(newStatus, _recentRecord){
var record = nlapiLoadRecord('supportcase', _recentRecord);
nlapiSubmitRecord(record, null, true);
}//end updateStatus_Form_Flag

function caseBeforeLoad(type){
var fldStatus = nlapiGetFieldText('status');
var _recentRecord = nlapiGetRecordId();
var caseFormID;

if ((type == 'view') && (fldStatus != nlapiGetFieldValue('custevent_case_type_flag'))) {//if1
case 'Escalated': //the following value 143 is the Internal ID of the Custom Entry Form to set
caseFormID = '143'; break;
//other case Status here
}//end switch

if ((type == 'view') && (fldStatus != nlapiGetFieldValue('custevent_case_type_flag'))) {//if2
updateStatus_Form_Flag(fldStatus, _recentRecord);
nlapiSetRedirectURL('RECORD', nlapiGetRecordType(), nlapiGetRecordId(), null, {'cf' : caseFormID});
}//end if2
}//end if1
}//end caseBeforeLoad
/* -------------------- END -------------------- */

4. Click on Save

With the default setup, these Steps will run in Testing Status. To test this, when the sample case is saved with a Status of Escalated, the assigned Custom Entry Form will be used. This can be verified when the same case record is viewed. When the page is in View type, in this example Status = Escalated, when you click on the Edit button it will be in the same assigned Custom Entry Form.

Users may need to create a separate Client Script to handle proper Custom Form assignment if going to Edit mode is not from the button in View mode (e.g. Edit link from the list of Cases or the Global Search result).

DISCLAIMER: The sample code described herein is provided on an "as is" basis, without warranty of any kind, to the fullest extent permitted by law. Netsuite Inc. does not warrant or guarantee the individual success developers may have in implementing the sample code on their development platforms or in using their own Web server configurations.

Netsuite Inc. does not warrant, guarantee or make any representations regarding the use, results of use, accuracy, timeliness or completeness of any data or information relating to the sample code. Netsuite Inc. disclaims all warranties, express or implied, and in particular, disclaims all warranties of merchantability, fitness for a particular purpose, and warranties related to the code, or any service or software related thereto.

Netsuite Inc. shall not be liable for any direct, indirect or consequential damages or costs of any type arising out of any action taken by you or others related to the sample code.

1 comment:

  1. I wish to show thanks to you just for bailing me out of this particular trouble.As a result of checking through the net and meeting techniques that were not productive, I thought my life was done.
    CRM with Invoicing