String, Extract Values using Line-Begin Matching

String, Extract Values using Line-Begin Matching
Matches the string at the beginning of the line and extracts the following string.
Configs
  • A: Select TextArea DATA to Parse *
  • B:EDIT String for index and fieldName eg.Corporation:q_cname; *
Script (click to open)
// GraalJS Script (engine type: 2)
// Configuration Format
// Format: {index}:{Questetra Data Items Field Name};
// eg. {index$1}:{Questetra Data Items Field Name$1};{index$2}:{Questetra Data Items Field Name$2};...
// eg. Corporate Name:q_corp_name;Person Name:q_person_name;
// index: String (the beginning of a sentence is {index}.
// Questetra Data Items Field Name: String eg. q_corp_name
// config delimiter: ":" for {index} and {Questetra Data Items Field Name}
// configs delimiter: ";" for configs
// Target Row Example : Corporate Name:Example Inc
//////// START "main()" /////////////////////////////////////////////////////////////////
main();
function main(){
//// == 工程コンフィグの参照 / Config Retrieving ==
const confObjA = configs.getObject( "conf_DataIdA" ); //required
const strConfigs = configs.get( "conf_DataIdB" ); //required
//// == ワークフローデータの参照 / Data Retrieving ==
const mailbody = engine.findData(confObjA);

engine.log("mailbody:" + mailbody);
engine.log("strConfigs:" + strConfigs);

//// == 演算 / Calculating ==
const configsSep = ";";
const configSep = ":";
const sep = ":";
const textEndSepForMultipleRow = "---------------";

const arrConfigs = strConfigs.split(configsSep);
let line = new Array();
let pos = "";
let dat = "";
let isParsingMultipeRow = false;
let multipleRowText = "";
let fieldNameForMultipleRowText = "";
line = mailbody.split("\n");
for(var i=0; i < line.length; i=i+1){
pos = line[i].indexOf(sep);
dat = line[i].slice(pos+sep.length);
for (var j=0; j < arrConfigs.length; j++){
let arrConfig = arrConfigs[j].split(configSep);
if (line[i].slice(0,pos) === arrConfig[0]){
let results = checkObjectDataType(arrConfig[1]);
if (results[0]){ // Valid DataType
if (results[1] === "STRING_TEXTAREA"){
engine.log("this row is STRING_TEXTAREA: " + dat);
isParsingMultipeRow = true;
fieldNameForMultipleRowText = arrConfig[1];
}else{
setDataToItem(dat.replace(/^ /,"") , arrConfig[1] , results[1]);
}
}else{ // Invalid DataType
engine.log("FileName: " + arrConfig[1] + "is not target DataType.");
}
}
}
if (isParsingMultipeRow){ // Multiple Text parsing
if (line[i] === textEndSepForMultipleRow){
engine.setDataByVarName(fieldNameForMultipleRowText , multipleRowText);
isParsingMultipeRow = false;
multipleRowText = "";
fieldNameForMultipleRowText = "";
}else{
multipleRowText += dat.replace(/^ /,"") + "\n";
}
}
}
} //----- End Of main()
/* checkObjectDataType(fieldName)
Check item's field name for valid data types. Get check result and data type name.
param: "fieldName"
return: Array [0]: check result(boolean) [1]: item's DataType(String)
*/
function checkObjectDataType(fieldName){
const defTypes = ["STRING_TEXTFIELD","STRING_TEXTAREA","DECIMAL","DATE_Y","DATE_YM","DATE_MD","DATE_YMD","DATETIME","SELECT_SINGLE","SELECT_CHECKBOX","QUSER"];
let def = engine.findDataDefinitionByVarName( fieldName );
let results = [false , ""];
for (let i=0; i < defTypes.length; i++){
if (def.matchDataType(defTypes[i])){
results[0] = true;
results[1] = defTypes[i];
}
}
return results;
} //----- End Of convertObjectForTargetDataType()
/*
setDataToItem(value,fieldName,dataType)
Set value to data item of "fieldName".
param1: value
param2: fieldName: data item's field name
param3: dataType : data item's data type name
*/
function setDataToItem(value,fieldName,dataType){
if (value === null){ return; }
const DATE_TYPES = ["DATE_Y","DATE_YM","DATE_MD","DATE_YMD"];
engine.log("datatye: " + dataType);
if (dataType === "SELECT_SINGLE"){
engine.setDataByVarName( fieldName,[value]);
return;
}
if (dataType === "SELECT_CHECKBOX"){
const arry = value.split(",");
engine.setDataByVarName( fieldName,arry);
return;
}
if (dataType === "QUSER"){
const quser = quserDao.findByEmail( value );
if (quser === null){
engine.log( fieldName + " Not Set. email" + value + " is not exist.");
}else{
engine.setDataByVarName( fieldName,quser);
}
return;
}
if (DATE_TYPES.indexOf(dataType) !== -1){
const date = new Date(value);
engine.setDataByVarName( fieldName , new java.sql.Date(date.getTime()));
return;
}
if (dataType === "DATETIME"){
const date = new Date(value);
engine.setDataByVarName( fieldName , new java.sql.Timestamp(date.getTime()));
return;
}
if (dataType === "DECIMAL"){
if (value === null){ return; }
engine.setDataByVarName( fieldName , new java.math.BigDecimal(value));
return;
}
engine.setDataByVarName( fieldName,value);
} //----- End Of setDataToItem()

Download

2022-08-11 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/string-extract-values-using-line-begin-matching-2022/
The Addon-import feature is available with Professional edition.
Freely modifiable JavaScript (ECMAScript) code. No warranty of any kind.

Notes

  • Setup Item “B: Provide the title at the beginning of the line and the name of the field for storage”
    • Format: {text search string}:{field name};{text search string}:{field name};…
      • Separator between “prefix search string” and “field name” “:” (half-width colon)
      • Delimiter of “”prefix search string”/”field name”” setting “;” (half-width semicolon)
    • Notes on Data Items to be Stored
      • User-type Data Item
        • Data Format: Email Address
      • Datetime-type Data Item
        • Format: YYYY/MM/DD HH:mm GMT[+-][0-99]
      • Select-type Data Item
        • Data content: Choice ID
      • String-type (multi-line) Data Item
        • Data contents: “---------------” string in the last line of data
  • Notes on text to be parsed
    • Format: {text search string:{data to be stored}
      • Separator “:” (half-width colon) between “search string at beginning of sentence” and “{data to be stored}”.

Capture

Appendix

  • Example of captured text
Example Text (click to open)
Hubspot has sent you information about a user who has met the Inside Sales Response Flag.

Email address: taro.yamada@example.com

Company Name: Example Inc.

Contact Person: Taro Yamada
Phone Number: 075-1234-5689
Contract Type: Paid

Contracted services: service1, service2

Sales Representative: satoh@example2.com
Contract start date: 2021/04/16 13:44 GMT+9

Web Site Visit Date: 2022/08/16 15:59 GMT+9

Customer Message: We are looking to expand the scope of our business use.
We will use the trial infrastructure to design the application.
Please provide suggestions for integration with HubSpot.
---------------

Contact ID: 12345678
User Score: 23

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading