1

I am trying to work on below requirement in SFMC,

I have a Data Extension which consist of records like below :

enter image description here

And I have a CloudPage with search option as below :

enter image description here

Now what my ask is , When I input in search as 1 I need to fetch the first row from the DataExtension and display that in table row.

Something like this:

enter image description here

Ignore the action buttons.

When trying to publish getting below error:

enter image description here

Below is the code i am using:

<!DOCTYPE html>
<html>
<head>
<title>Search Record in DataExtension</title>
</head>
<body>
<h1>Search Record in DataExtension</h1>
<form method="post">
<label for="searchTerm">Enter ID :</label>
<input type="text" name="searchTerm" id="searchTerm">
<input type="submit" value="Search">
</form>
<br>
<div id="resultsDiv"></div>

<script runat="server">
   // Retrieve search query from request parameters
  var searchQuery = Request.GetQueryStringParameter("searchTerm");

  // Initialize data extension object
  var dataExtensionName = "TestRecords";
  var data = DataExtension.Init(dataExtensionName);

  // Build filter based on search query
  var filter = {
    Property: "ID",
    SimpleOperator: "equals",
    Value: searchTerm
  };

  // Retrieve rows based on filter
  var rows = data.Rows.Retrieve(filter);

  // Output row data in HTML table format
  if (rows && rows.length > 0) {
    Write('<table>');
    Write('<tr><th>Fname</th><th>Lname</th><th>Fullname</th></tr>');
    for (var i = 0; i < rows.length; i++) {
      var row = rows[i];
      Write('<tr>');
      Write('<td>' + row.Fname + '</td>');
      Write('<td>' + row.Lname + '</td>');
      Write('<td>' + row.Fullname + '</td>');
      Write('</tr>');
    }
    Write('</table>');
  } else {
    Write('<p>No records found.</p>');
  }
</script>

</body>
</html>

Can you please let me know what mistake I am making here..

Thanks in Advance :-)

0

2 Answers 2

1

I believe you are just missing the call to the Core library in order to use the Core Data Extension functions. (ref)

So likely by adding in: Platform.Load("Core","1.1.1"); to the top of your script should remove that error.


<script runat="server">
  Platform.Load("Core","1.1.1");

  // Retrieve search query from request parameters
  var searchQuery = Request.GetQueryStringParameter("searchTerm");

  // Initialize data extension object
  var dataExtensionName = "TestRecords";
  var data = DataExtension.Init(dataExtensionName);
  ....
2
  • Thanks for the response, After adding the above syntax the error was still coming , However when I published the page and opened it in browser the HTMl format was working fine but the code logic was not working ( I was unable to fetch the records by passing value in the textbox) . Now I have modified bit my code by replacing SSJS to Ampscript logic and its meeting my requirement. May 13 at 13:21
  • @omkarkagati - Glad you got it to work in an alternative way! May 15 at 14:18
1

Below is the new logic I have used to meet my requirement

<!DOCTYPE html>
<html>
<head>
  <title>Retrieve Record from Data Extension</title>
  <head>
<script>
function editRow(button) {
  var row = button.parentNode.parentNode;
  var cells = row.getElementsByTagName("td");
  var fname = cells[0].innerText;
  var lname = cells[1].innerText;
  var fullname = cells[2].innerText;
  

  // Replace the row cells with input fields
  
  cells[0].innerHTML = "<input type='text' value='" + fname + "'>";
  cells[1].innerHTML = "<input type='text' value='" + lname + "'>";
  cells[2].innerHTML = "<input type='text' value='" + fullname + "'>";
  cells[3].innerHTML = "<button onclick='saveRow(this)'>Save</button>";
  
}

function saveRow(button) {
  var row = button.parentNode.parentNode;
  var cells = row.getElementsByTagName("td");
 
  var fname = cells[0].getElementsByTagName("input")[0].value;
  var lname = cells[1].getElementsByTagName("input")[0].value;
  var fullname = cells[2].getElementsByTagName("input")[0].value;

  // Replace the input fields with the updated data
 
  cells[0].innerHTML = fname;
  cells[1].innerHTML = lname;
  cells[2].innerHTML = fullname;
  cells[3].innerHTML = "<button onclick='editRow(this)'>Edit</button>";
}
</script>
</head>
</head>
<body>
  <h1>Retrieve Record from Data Extension</h1>
  <form method="post">
    <label for="searchTerm">Enter ID:</label>
    <input type="text" name="searchTerm" id="searchTerm">
    <input type="submit" value="Search">
  </form>
  <br>
  <table>
    <thead>
      <tr>
        <th>Fname</th>
        <th>Lname</th>
        <th>FullName</th>
        <th>Action</th>
        <th>ID</th>
      </tr>
    </thead>
<tbody>
    %%[
    var @deRows, @rowCount, @i, @row, @column1, @column2
    set @id = RequestParameter("searchTerm")
    set @deRows = LookupRows("TestRecords", "ID", @id)
    set @rowCount = rowcount(@deRows)

    if @rowCount > 0 then
      for @i = 1 to @rowCount do
        set @row = row(@deRows, @i)
        set @column1 = field(@row, "Fname") 
        set @column2 = field(@row, "Lname")
        set @column3 = field(@row, "ID")
         set @fullName  = Concat(@column1, " ", @column2)
        ]%%
        <tr>
          
          <td>%%=v(@column1)=%%</td>
          <td>%%=v(@column2)=%%</td>
          <td>%%=v(@fullName)=%%</td>
          <td><button onclick="editRow(this)">Edit</button><button onclick="">Save</button></td>
          <!-- Add additional columns as needed -->
          <td>%%=v(@column3)=%%</td>
        </tr>
   
        %%[
      next @i
    endif
    ]%%
  </tbody>
  </table>
</body>
</html>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .