Script to set fields from CSV using FAFR.

Script to set fields from CSV using FAFR.

Works in 10604


Requirement:
Load Data from CSV depending on the value selected in an additional field.

Usecase:
Assume there are 3 additional fields for a request template, namely, "Reference number", "Payment type and "Vendor".
"Reference number" is a picklist field, and has all the information pre-populated. 
When the ‘Reference number’ field is filled by the Requester, the respective ‘Payment type’ along with the 'Vendor' field has to be populated from a CSV file.

Steps to follow:
1.  Create 3 Additional fields and include these in the required template
Reference number - Picklist additional field  - Content has to be pre populated with all the client codes.
Payment type  - Single line additional field - Content will be populated from the CSV File
Vendor- Single line additional field - Content will be populated from the CSV File.
2.  Compose a CSV file with first column as the "Reference number", then "Payment type" and finally "Vendor".   Place this CSV File in "<SDP_Home>\custom" Folder

3.  Use the attached script and update the UDF Field names in the script.  Also, update the CSV file headers in the script.

4.  Configure the script on Field change for the first additional field : Reference number.

5.  Other fields will be auto populated.

Script:
var primary_fieldName = "WorkOrder_Fields_UDF_CHAR1";
var primary_key="Ref Number";
var mapping={"WorkOrder_Fields_UDF_CHAR3":"Payment Type",
             "WorkOrder_Fields_UDF_CHAR2":"Vendor / Payee Name"};
var template_map_file="/custom/sample.csv";

var primary_data = $CS.getValue(primary_fieldName);

function csvJSON(csv){
  var lines=csv.split("\n");
  var result = {};
  var headers=lines[0].split(",");
  for(var i=1;i<lines.length;i++){
      var obj = {};
      var currentline=lines[i].split(",");
      for(var j=0;j<headers.length;j++){
          obj[headers[j]] = currentline[j];
      }
       if(obj[primary_key]){
         result[obj[primary_key]]=obj;
       }
  }
  return result; //JSON
}

function mapping_process(data){
   var ref_number= primary_data;
   var ref_data=data[ref_number];
   for(var key in mapping){
   $CS.setValue(key,ref_data[mapping[key]]);
   }
}
jQuery.get({url:template_map_file,success:function(data){mapping_process(csvJSON(data));}})

                    New to ADSelfService Plus?

                      • Related Articles

                      • How to diff of two Date& Time fields using FAFR

                        If you have two date and time additional fields or default fields, and if you want to capture the difference between those two fields in Hours and minutes, then use the below script to capture the value in an additional field, Under the respective ...
                      • Set SLA automatically for a service request using FAFR

                        Note: The below FAFR script will work only on 10600 MSP and above Requirement: Set SLA automatically for a service request depending on the Priority. Usecase: Let us consider a scenario where SLA has to be set depending on the requester's associated ...
                      • Script to Populate User Additional Field under Incident Template using FAFR

                        The script load the Requester Name under Incident Additional field ( As a pick list), by choosing the requester name under additional field the script populates the user additional field value. * Kindly create two additional fields under Admin -> ...
                      • Dynamic loading of field data from an external CSV file

                        Requirement: Load data to additional fields dynamically from an external CSV file based on another field value. Usecase: Assume, a request template includes three additional fields: 'Client Code,' 'Client Name,' and 'Client Address.' When a pick-list ...
                      • Import and update Requester from CSV (Comma Separated Value) Files

                        Import Requester from CSV (Comma Separated Value) Files You can also add requesters by importing from CSV files. To import requesters from CSV file   Step 1: Locate the CSV file Click Import from CSV link in the Requester List page. The Import ...