0

I have the following code in a Cloud Page

%%[
var
@avisoprivet,
@llamadas,/*Autorizo Autorizo llamadas*/
@terceros, /*Autorizo terceros*/
@sms,/*Autorizo sms*/
@email,/*Autorizo E-mail*/
@estado_cliente

SET @avisoprivet = RequestParameter("avisoprivet")
output(concat("<br>autorizo habeas: ", @avisoprivet))

SET @llamadas = RequestParameter("llamadas") 
/*output(concat("<br>llamadas: ", @llamadas))*/

SET @sms = RequestParameter("sms") 
/*output(concat("<br>sms: ", @sms))*/

SET @terceros = RequestParameter("terceros") 
/*output(concat("<br>terceros: ", @terceros))*/

SET @email = RequestParameter("email") 
/*output(concat("<br>email: ", @email))*/


/* Validacion Estados del Cliente*/

IF NOT EMPTY(@avisoprivet) AND @avisoprivet == "siacep" THEN
    SET @llamadas = "True"
    SET @sms = "True"
    SET @terceros = "True"
    SET @email = "True"
    SET @estado_cliente = "1"


output(concat("<br>Estado del Cliente: ", @estado_cliente))
output(concat("<br>llamadas: ", @llamadas))
output(concat("<br>sms: ", @sms))
output(concat("<br>terceros: ", @terceros))
output(concat("<br>email: ", @email))

ELSEIF  @avisoprivet == "noacep" THEN
    
    SET @llamadas = "False"
    SET @sms = "False"
    SET @terceros = "False"
    SET @email = "False"
    SET @estado_cliente = "2"
    
    
    

output(concat("<br>Estado del Cliente: ", @estado_cliente))
output(concat("<br>llamadas: ", @llamadas))
output(concat("<br>sms: ", @sms))
output(concat("<br>terceros: ", @terceros))
output(concat("<br>email: ", @email))


ELSEIF @avisoprivet == "Parcial" THEN
       
    IF @email == "True" AND @sms == "False" AND @llamadas == "False" AND @terceros == "False" THEN
       SET @estado_cliente = "3"
    output(concat("<br>Estado del Cliente: ", @estado_cliente))
    
    ELSEIF @email == "False" AND @sms == "True" AND @llamadas == "False" AND @terceros == "False" THEN
       SET @estado_cliente = "4"
    output(concat("<br>Estado del Cliente: ", @estado_cliente))
    
    ELSEIF @email == "False" AND @sms == "False" AND @llamadas == "True" AND @terceros == "False" THEN
       SET @estado_cliente = "5"
    output(concat("<br>Estado del Cliente: ", @estado_cliente))
    
    ELSEIF @email == "False" AND @sms == "False" AND @llamadas == "False" AND @terceros == "True" THEN
       SET @estado_cliente = "6"
    output(concat("<br>Estado del Cliente: ", @estado_cliente))
    
    ELSEIF @email == "True" AND @sms == "True" AND @llamadas == "False" AND @terceros == "False" THEN
       SET @estado_cliente = "7"
    output(concat("<br>Estado del Cliente: ", @estado_cliente))
    
    ELSEIF @email == "True" AND @sms == "False" AND @llamadas == "True" AND @terceros == "False" THEN
       SET @estado_cliente = "8"
    output(concat("<br>Estado del Cliente: ", @estado_cliente))
    
    ELSEIF @email == "True" AND @sms == "False" AND @llamadas == "False" AND @terceros == "True" THEN
       SET @estado_cliente = "9"
    output(concat("<br>Estado del Cliente: ", @estado_cliente))
    
    ELSEIF @email == "True" AND @sms == "True" AND @llamadas == "True" AND @terceros == "False" THEN
       SET @estado_cliente = "10"
    output(concat("<br>Estado del Cliente: ", @estado_cliente))
    
     ELSEIF @email == "True" AND @sms == "True" AND @llamadas == "False" AND @terceros == "True" THEN
       SET @estado_cliente = "11"
    output(concat("<br>Estado del Cliente: ", @estado_cliente))
    
     ELSEIF @email == "True" AND @sms == "False" AND @llamadas == "True" AND @terceros == "True" THEN
       SET @estado_cliente = "12"
    output(concat("<br>Estado del Cliente: ", @estado_cliente))
    
    ELSEIF @email == "False" AND @sms == "True" AND @llamadas == "True" AND @terceros == "False" THEN
       SET @estado_cliente = "13"
    output(concat("<br>Estado del Cliente: ", @estado_cliente))
    
    ELSEIF @email == "False" AND @sms == "True" AND @llamadas == "False" AND @terceros == "True" THEN
       SET @estado_cliente = "14"
    output(concat("<br>Estado del Cliente: ", @estado_cliente))
    
    ELSEIF @email == "False" AND @sms == "False" AND @llamadas == "True" AND @terceros == "True" THEN
       SET @estado_cliente = "15"
    output(concat("<br>Estado del Cliente: ", @estado_cliente))
    
    ELSEIF @email == "False" AND @sms == "True" AND @llamadas == "True" AND @terceros == "True" THEN
       SET @estado_cliente = "16"
    output(concat("<br>Estado del Cliente: ", @estado_cliente))
    
    
    
    ENDIF
       

ENDIF

]%%

]%%

and I want to reuse it for the following variables

Is there a function to reuse that code and not rewrite it for every variable?

html sends me 30 variables, 5 for each option, that's why I want to reuse the code and not rewrite it for all the variables

1
  • 2
    I've made an attempt to translate the question title to English. Feel free to update it if you think I got it wrong. – Derek F Aug 10 '20 at 14:43
0

I think if all you are doing is outputting some stuff, you can wrap it in a for loop and just set the 'end' number to the options count you have. Then you just have the RequestParameter() inside the loop do a concat to add the iterator to it to pull each iteration grabbed from the page.

So, have it begin like:

%%[

SET @loop = 6

FOR @i=1 TO @loop DO

  SET @avisoprivet = RequestParameter(CONCAT("avisoprivet", @i))
  output(concat("<br>autorizo habeas: ", @avisoprivet))

  SET @llamadas = RequestParameter(CONCAT("llamadas", @i)) 
  /*output(concat("<br>llamadas: ", @llamadas))*/

  ......

and then close with:

      ENDIF
         

  ENDIF

NEXT @i

]%%

This will then have the full code run 6 times (or however many times you set @loop to) and grab each of those values 1 through 6 while reusing the same variables, meaning you do not need to adjust code.

E.g. avisoprivet1, avisoprivet2, avisoprivet3, avisoprivet4, avisoprivet5 and then avisoprivet6 would be pulled in the first SET @avisoprivet in the corresponding iteration.

If you need to store the values for an output outside of the loop, then you likely would want to create dynamic variables like @JonasLamberty said. Below is a quick snippet example I usually use:

%%[
SET @deRows = LookupRows(...)

FOR @i = 1 TO RowCount(@deRows) DO

  SET @row = Row(@deRows,@i)
  SET @tempValue = Field(@Row,'Field')

  SET @FieldSet = TreatAsContent(CONCAT('%','%[SET @Field', @i, ' = @tempValue]%', '%'))

NEXT @i 
]%%

You would need to adjust accordingly for your needs of course.

Here is an example I have used in a similar situation that may help you as well:

IF Rowcount(@customer_rows) > 0 THEN
    SET @customer_row = ROW(@customer_rows, 1)
    SET @product_count = 0
    SET @productStr = 'productId1|productId2|productId3|productId4|productId5|productId6|productId7|productId8|productId9|productId10'
    SET @productRowSet = BuildRowsetFromString(@productStr)
    FOR @i = 1 TO Rowcount(@productRowSet) DO
        IF @product_count < 6 THEN /* Limit returned products to 6 total */
            SET @tempProduct = '' /* Reset prior to next iteration */
            SET @productRow = Row(@productRowSet,@i)
            SET @fieldName = Field(@productRow,1)
            SET @tempProduct = Field(@customer_row,@fieldName, 0)

            IF NOT EMPTY(@tempProduct) THEN
                SET @product_count = ADD(@product_count,1)
                SET @FieldSet = TreatAsContent(CONCAT('%','%[SET @Product', @product_count, ' = @tempProduct]%', '%'))
            ENDIF
        ENDIF
    NEXT @i 
ENDIF
1
  • 1
    thank you very much for your help – Jefersson Gomez Aug 13 '20 at 20:01
0

Edit: out of date since OP has been edited; this solves an earlier version of the problem.

you need to dynamically declare your variables. This can be a bit of a mindtwister, but if you have the basic trick down (it involves TreatAsContent), it just comes down to a nested loop and some logic.

I'll provide some functional code with your logic at the core, for you to try it out, this has been tested back to back on a cloudpage and yields the expected results.

Start with a data extension called VariableDefinitions:

schema:

name length nullable primary key
variableClass 50 No PK  
variableName 50 No PK
valueYes 50 Yes 
valueNo 50 Yes

Now import your definitions:

variableClass,variableName,valueYes,valueNo
avisoprivet,llamadas,True,False
avisoprivet,sms,True,False
avisoprivet,terceros,True,False
avisoprivet,email,True,False
avisoprivet,estado_cliente,1,2

This is the base for your following dynamic variable definitions, and you will loop through some definitions three times in the following code. You can turn it up to 6 (5x6 = 30) as indicated in the code example.

Put the AMPScript below on a cloudpage and you are ready to go. You can just hit preview and you should see the value of your variables for "siacep", and you can force it to "noacep" where indicated.

%%[
SET @schemaDE = "variableDefinitions"
SET @rowsetRetrieved = LookupRows(@schemaDE,"variableClass","avisoprivet")
SET @rowCountRetrieved = rowCount(@rowsetRetrieved)

/* change this number for more sets of variables. e.g. if you set this to 6, you can use and print the variable '@llamadas6' etc. */
SET @numberOfVariableSets = 3

FOR @i = 1 TO @numberOfVariableSets DO
    FOR @j = 1 TO @rowCountRetrieved DO
        SET @row = row(@rowsetRetrieved,@j) /* get row based on loop counter */
        SET @variableClass = concat(field(@row,"variableClass"),@i)
        SET @createdVariableName = concat(field(@row,"variableName"),@i)
        SET @createdVariableValueYes = field(@row,"valueYes")
        SET @createdVariableValueNo = field(@row,"valueNo")

/* you need to declare this "variable" (@@variableclass is the same as avisoprivet1,avisoprivet2,avisoprivet3 etc. depending on @i with "siacep" or "noacep" , and the output will change accordingly */
        SET @@variableClass = "siacep"
    

        IF NOT EMPTY(@@variableClass) AND @@variableClass == "siacep" THEN
/* this is where the magic happens. This set a dynamically built variable to a dynamic value from the data extension - once for "siacep"... */
        ]%%
            %%=TreatAsContent(Concat("%","%[SET @",@createdVariableName," = '",@createdVariableValueYes,"']%","%"))=%%
        %%[
        ELSE
        ]%%
/*... and once for "noacep" */
            %%=TreatAsContent(Concat("%","%[SET @",@createdVariableName," = '",@createdVariableValueNo,"']%","%"))=%%
        %%[
        ENDIF
     NEXT @j
NEXT @i  

/* you now have all your variables without ever writing them before this line.
Just print them, see some examples below: */
]%%

%%=v(@llamadas1)=%%
%%=v(@llamadas3)=%%
%%=v(@email2)=%%
%%=v(@estado_cliente1)=%%
%%=v(@estado_cliente2)=%%
%%=v(@estado_cliente3)=%%
2
  • thanks for your help, this is useful for the options that I put in the code, the truth is that I miss to write in the question the other combinations since what I want is that from the combinations I give a number of result in the variable state of the client and are 16 combinations as shown below thanks for your help, this is useful for the options that I put in the code, the truth is that I need to write in the question the other combinations because what I want is that from the combinations I get a number of result in the variable state of the client and are 16 combinations – Jefersson Gomez Aug 10 '20 at 18:13
  • We can help you with answers, the questions you have to write yourself :) – Jonas Lamberty Aug 11 '20 at 6:40

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.