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 ADManager Plus?

                    New to ADSelfService Plus?