0

I have a doozy scenario here. With some AMPscript help, using lookuporderedrows i have a cloud page with rows presented in a table. In this table there will be two input fields which i want passed to a DE along with other attributes when it is submitted. I am using InsertDE in the handler page

Where there is a single row in the table, when i submit the values are passed however when there are multiple rows it is throwing a 500 resource error.

I have been reading Adam's solution here but i am not getting the desired outcome. How can I add multiple rows to a data extension?

Below is some screenshots of a single row table and multi row table in Cloud page.

enter image description here

enter image description here

Code in my handler

%%[
var @date, @reading

set @date = RequestParameter('date')
set @reading = RequestParameter('reading')
set @Serial = RequestParameter('serial')
set @ICP = RequestParameter('icp')
set @Meter = RequestParameter('meter')
set @Reg = RequestParameter('reg')

if RequestParameter("submitted") == "submitted" then
InsertDE('Consec_responses',"SERIAL",@Serial,"ICP",@ICP,"DATE",@date,"METER",@Meter,"REG",@Reg,"READING",@reading)
endif
]%%

Updated code in my handler after reading Adam's solution Note: Serial is a unique ID created at Send_De which is being passed from the email.

%%[
set @date = RequestParameter('date')
set @reading = RequestParameter('reading')
set @Serial = RequestParameter('serial')
set @ICP = RequestParameter('icp')
set @Meter = RequestParameter('meter')
set @Reg = RequestParameter('reg')

set @Set_of_rows = Lookuprows ("Consec_Send_DE", 0, "PARTY_CODE, METER, ADDRESS, ICP, REG, SERIAL")

set @Number_of_rows= RowCount(@Set_of_rows)

FOR @i = 1 TO @Number_of_rows DO

  SET @row = Row(@Set_of_rows,@i)
   SET @ud =UpdateDE("Data_extension_city",1,"ID",@id,"Counter",@i)
   
  if RequestParameter("submitted") == "submitted" then
  InsertDE('Consec_responses',"SERIAL",@Serial,"ICP",@ICP,"DATE",@date,"METER",@Meter,"REG",@Reg,"READING",@reading,"Counter",@i)

NEXT @i

endif
]%%

2 Answers 2

0

You're getting the error for a couple of reasons:

  1. Because your ENDIF and FOR loop have an overlapping process...your IF statement needs to complete within the loop because that's where you've started it.
  2. You're using the InsertDE function, however that is only for use in emails - you should be using InsertData in a Cloud Page.

And given you don't really want to run any of that code unless the form has been submitted I'd suggest you change it to

%%[
set @date = RequestParameter('date')
set @reading = RequestParameter('reading')
set @Serial = RequestParameter('serial')
set @ICP = RequestParameter('icp')
set @Meter = RequestParameter('meter')
set @Reg = RequestParameter('reg')

set @Set_of_rows = Lookuprows ("Consec_Send_DE", 0, "PARTY_CODE, METER, ADDRESS, ICP, REG, SERIAL")

set @Number_of_rows= RowCount(@Set_of_rows)

if RequestParameter("submitted") == "submitted" then
  FOR @i = 1 TO @Number_of_rows DO

    SET @row = Row(@Set_of_rows,@i)
    SET @ud =UpdateDE("Data_extension_city",1,"ID",@id,"Counter",@i)     
    
    InsertData('Consec_responses',"SERIAL",@Serial,"ICP",@ICP,"DATE",@date,"METER",@Meter,"REG",@Reg,"READING",@reading,"Counter",@i)

  NEXT @i
endif
]%%

Even given all of the above, your form should process but it will still be wrong. When you auto-generate each of the Reading and Date form fields, you need their names to have a sequential number on them because you need to treat them separately eg Reading1, Date1, Reading2, Date2 etc. Then when you process the form you need to run through your loop as you have it above but have

SET @reading = RequestParameter(Concat("Reading", @i))

That will pull in the value for Reading1 the first time through the loop, then Reading2 the second time through etc

1
  • I am going to try this out and revert....will post the code when i have it working.
    – Rahul G
    Jun 8, 2021 at 19:13
0

here is the code that works. Basically we have a script writing it to the DE. This code is placed in the Cloud page so no need for form handler.

VAR @cid, @addr
SET @submitted=RequestParameter('submitted')
IF NOT EMPTY(@submitted) THEN]%%
<script runat="server">
Platform.Load("Core","1.1.1")
var cid=Request.GetFormField("cid");
var dataRows = Platform.Function.LookupRows('Consec_Send_DE','ID',cid);
if(dataRows && dataRows.length > 0) {
for(var i=0; i<dataRows.length; i++) {
var serial=dataRows[i]["SERIAL"];
var reading_var = serial+'__reading';
var date_var = serial+'__date';
var reading_val = Request.GetFormField(reading_var);
var date_val = Request.GetFormField(date_var);
var rows = Platform.Function.UpdateData("Consec_Send_DE",["SERIAL"],[serial],["READING","DATE"],[reading_val,date_val]);
}
Write("Thank you. Your readings have been recevied. We will contact you if there are any issues.");
}
else Write("No Row to update");
</script>

%%[ELSE]%%
<form action="" method="post">
FORM<br>
%%[set @cid = RequestParameter('cid')
set @addr = RequestParameter('addr')

SET @cRows = LookupOrderedRows("Consec_Send_DE", 0, "PARTY_CODE, METER, ADDRESS, ICP, REG, SERIAL", "ID", @cid)

IF RowCount(@cRows) > 0 THEN
FOR @i = 1 TO RowCount(@cRows) DO
IF Field(Row(@cRows, @i), "ADDRESS") != @addr THEN
IF @i > 1 THEN
]%%
%%[ENDIF]%%

<table align="center" cellspacing="0" cols="4" border="0" style="margin:15px 0px 0px 10px;float:center">
<tr>
<td>ADDRESS: %%=Field(Row(@cRows, @i), "ADDRESS")=%%</td>
</tr>
<tr>
<td>ICP: %%=Field(Row(@cRows, @i), "ICP")=%%</td>
</tr>
</table>
<table cellspacing="0" cols="4" align="center" border="1" style="margin:15px 0px 0px 10px;float:center">
<tr>
<td style="text-align:center;"><b>Meter number</b></td>
<td style="text-align:center;"><b>Register</b></td>
<td style="text-align:center;"><b>Reading</b></td>
<td style="text-align:center;"><b>Date</b></td>
</tr>
%%[
ENDIF

SET @addr = Field(Row(@cRows, @i), "ADDRESS")
SET @ICP = Field(Row(@cRows, @i), "ICP")
SET @Meter = Field(Row(@cRows, @i), "METER")
SET @Reg = Field(Row(@cRows, @i), "REG")
SET @Reading = Field(Row(@cRows, @i), "READING")
SET @Date = Field(Row(@cRows, @i), "DATE")
SET @Serial = Field(Row(@cRows, @i), "SERIAL")
SET @reading_var=Concat(@Serial, "__reading")
SET @date_var=Concat(@Serial, "__date")
SET @formattedDate = IIF(EMPTY(@Date), "", Format(@Date, "yyyy-MM-dd"))
]%%

<tr>
<td>%%=v(@Meter)=%%</td>
<td>%%=v(@Reg)=%%</td>
<td><input type="text" name="%%=v(@reading_var)=%%" placeholder="insert reading here" value="%%=v(@Reading)=%%" required=""></td>
<td><input type="date" name="%%=v(@date_var)=%%" placeholder="reading date" value="%%=v(@formattedDate)=%%" required=""></td>
</tr>

%%[
NEXT

ELSE
]%%
<tr><td colspan="4">No rows found</td></tr>
%%[ENDIF]%%

</table> <br>
<input type="hidden" name="cid" value="%%=v(@cid)=%%">
<input id="submitted" type="hidden" name="submitted" value="submitted">
<input type="submit" value="Submit">

</form>
%%[ENDIF]%%

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.