- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2023 04:22 AM
Hi experts,
I am trying to retrieve variables and fields of a HR case form to create a pdf.
I am trying it from a Business rule.
I have saved the name of fields in a property and want to run a loop to print the field label and field value.
However the script stops working at line-
fieldText += "\n" + current.getElement(currentField).getLabel() + ": " + current.getValue(currentField) + "\n";
Can anyone suggest what is wrong with this statement in below BR script?
var displayFields = gs.getProperty('sn_hr_core.pdf_case_form_fields');
var fieldArray = displayFields.split(",");
gs.info('fieldArray: ' + fieldArray);
for (var a = 0; a < fieldArray.length; a++) {
currentField = fieldArray[a];
fieldText += "\n" + current.getElement(currentField).getLabel() + ": " + current.getValue(currentField) + "\n"; // for each field in the loop, capture the Label and value with a Line Break
}
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2023 04:09 AM
Hi experts,
The mistake here was the missing variable declaration for fieldText . Somehow, I missed to notice that.
The script runs as expected after the variable declaration.
var fieldText='';
var displayFields = gs.getProperty('sn_hr_core.pdf_case_form_fields');
var fieldArray = displayFields.split(",");
gs.info('fieldArray: ' + fieldArray);
for (var a = 0; a < fieldArray.length; a++) {
currentField = fieldArray[a];
fieldText+= "\n" + current.getElement(currentField).getLabel() + ": " + current.getValue(currentField) + "\n"; // for each field in the loop, capture the Label and value with a Line Break
}
Thanks to a friend whose fine eyes noticed it !😁
Keeping this question for reference to others.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2023 03:44 AM
Hi,
getElement(String fieldName) for global application --->Retrieves the GlideElement for a specified field.
getElement(String column name) for scoped application --->Retrieves the GlideElement object for the specified field.
GlideRecord has a getElement() method. This returns an array (if in scope) or an ArrayList (if in global) of all of the elements on the record. You can then loop through the array (in scope) or iterate through it (in global) to get things like the label, field name, and value.
Refer the below example
var gr=new GlideRecord('incident');
gr.addActiveQuery();
gr.query();
gr.next();
var elements=gr.getElement();
if(typeof elements.size !='undefined')
{
// In Global scope ,so iterate
for(var i=0 ; i<elements.size() ;i++)
{
var ele=elements.get(i);
gs.info("ele.getLabel() , ele.getName() ,gr.getValue(ele.getName()));
}
}
else
{
// For scope ,loop through array
for(var i=0;i<elements.length;i++)
{
var ele=elements[i];
gs.info("ele.getLabel() , ele.getName() ,gr.getValue(ele.getName()));
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2023 04:09 AM
Hi experts,
The mistake here was the missing variable declaration for fieldText . Somehow, I missed to notice that.
The script runs as expected after the variable declaration.
var fieldText='';
var displayFields = gs.getProperty('sn_hr_core.pdf_case_form_fields');
var fieldArray = displayFields.split(",");
gs.info('fieldArray: ' + fieldArray);
for (var a = 0; a < fieldArray.length; a++) {
currentField = fieldArray[a];
fieldText+= "\n" + current.getElement(currentField).getLabel() + ": " + current.getValue(currentField) + "\n"; // for each field in the loop, capture the Label and value with a Line Break
}
Thanks to a friend whose fine eyes noticed it !😁
Keeping this question for reference to others.