Friday, October 5, 2018

Prevent Customer Record Email/Phone Fields to be Updated from Details coming from the Case Record using SuiteScript

Current behavior in relation to Email/Phone fields on Customer Record is that if they are blank then they will be updated with the details entered in these fields from the Case Record when it is save.

 

The following open Enhancement exists asking that this behavior be changed:

Enhancement 191845: List > Support > Cases > Select Customer > No Contact – Or a Contact with no email/phone selected > Manually enter Email(s)/Phone > Save > If Customer record's Email/Phone is blank it will be populated with the information entered in the Case form.  

In order to prevent the Email/Phone Fields of a Customer Record from being updated, if they are blank, from the Email/Phone field values entered on the Case Record the following Alternate Solution can be considered.

Note: Usual use case of these are companies that don't store contact information (Phone/Email) on the Customer Record itself hence all contact information are in the Contact Records attached to the Customer Record, or entered manually on a Case Record.

The provided solution below is a combination of Custom Fields and SuiteScript

1. Create 3 custom fields on the Case record

2. Navigate to Customization > Lists, Records, & Fields > CRM Fields > New
    Name: Customer Email Blank
    ID: custevent_cust_email_blank  
    Type: Check Box
    Applies To: Case
    Display Type: Hidden
    Click Save

    Name: Customer Phone Blank
    ID: custevent_cust_phone_blank
    Type: Check Box
    Applies To: Case
    Display Type: Hidden
    Click Save
 
    Name: Customer Record
    ID: custevent_is_cust
    Type: Check Box
    Applies To: Case
    Display Type: Hidden
    Click Save

3. Save the script below in a .js file and upload it under Document > Files > SuiteScripts

4. Create a new User Event Script Customization > Scripting > Scripts > New > User Event
    Script File: "Select the uploaded file in the Suitescripts folder"
    Name: Case Prevent Contact Info Copy
    ID: customscript_case_prev_cont_copy  
    Before Submit: beforeSubmitCheckContactInfo
    After Submit: afterSubmitCheckContactInfo  
    Click Save & Deploy

5. On the deployment page
    Applies to: Case
    ID: customdeploy_case_prev_cont_copy_dep    
    Deployed: Checked
    Execute as admin: Checked
    Status: Released
    Log Level: Debug
    Audience: Check All Roles
    Click Save

 

function beforeSubmitCheckContactInfo(type, form)

{

      //set the custom field which tags the record if it is a customer record or not

    nlapiSetFieldValue('custevent_is_cust','F');

     

      //created a try catch statement for the company field

      //the company field can be a partner  or a project record

      //this will prevent errors from a mismatch load of record type

      try {

            //retrieves the company field and loads the record

            var newRec = nlapiGetNewRecord();

            var custid = newRec.getFieldValue('company');

            var custRec = nlapiLoadRecord('customer', custid);

     

            nlapiSetFieldValue('custevent_is_cust', 'T');

           

            if (custRec != null && custRec != '') {

                  //checks the email value  on the customer record

                  //checks the checkbox if  it's blank else it wlll be unchecked

                  var email =custRec.getFieldValue('email');

                  if (email != '' && email  != null)

                        nlapiSetFieldValue('custevent_cust_email_blank', 'F');

                  else

                        nlapiSetFieldValue('custevent_cust_email_blank', 'T');

                 

                  //checks the phone value  on the customer record

                  //checks the checkbox if  it's blank else it wlll be unchecked

                  var phone =custRec.getFieldValue('phone');

                  if (phone != '' && phone  != null)

                        nlapiSetFieldValue('custevent_cust_phone_blank', 'F');

                  else

                        nlapiSetFieldValue('custevent_cust_phone_blank', 'T');

            }          

      }

      catch (e) {

          // check if the record error is a mismatch on the load

            // log the id of the record and company field for audit purposes

            // else it will throw an error  in the UI with the actual error and log it

            if (e.code == 'SSS_RECORD_TYPE_MISMATCH') {

                  nlapiLogExecution('AUDIT', 'Company is not a customer record', 'Case ID: ' +nlapiGetRecordId() +

                  ' Company ID: ' +

                  custid);

                 

            }

            else {

 

                  nlapiLogExecution('ERROR', 'Error Details', e.code + ' , ' + e.details);

                  throw e.code + ' : ' + e.details;

            }

      }

 

}

 

 

function afterSubmitCheckContactInfo(type, form)

{

      //retrieves the new record and checks if it's a customer type of company

      var newRec = nlapiGetNewRecord();  

      if (newRec.getFieldValue('custevent_is_cust') == 'T')

      {

         //retrieves the id of the company  field for the submission of the field

         var custid = newRec.getFieldValue('company');

        

        

         //checks the tag if the phone is true

         //then it will update the phone field on the customer record as blank

         if (nlapiGetFieldValue('custevent_cust_email_blank') == 'T')

               nlapiSubmitField('customer',custid,'email','');

                   

         //checks the tag if the phone is true

         //then it will update the phone field on the customer record as blank

         if (nlapiGetFieldValue('custevent_cust_phone_blank') == 'T')

               nlapiSubmitField('customer',custid,'phone','');

             

      }

}

No comments:

Post a Comment