0

I am getting the Internal Server Error 500 when using the UpdateSingleSalesforceObject on a CloudPage while attempting to retrieve and output Salesforce Fields such as 'Email' and 'FirstName' from a known contactKey found in SalesForce. When I run the block of code below without attempting to output the fields 'Email' and 'FirstName' the code results in displaying a count of the number of records found. Both Salesforce and SFMC are connected via the connector. Is there anything in this code that would prevent displaying of SF field data from within Marketing Cloud? What about any special permissions needing to be enabled? Are there any needed to display fields?

%%[ var @cntr, @rowset, @row, @numberOfRows, @id, @email
    Set @rowset = RetrieveSalesforceObjects("Contact","Id,Email,FirstName","Id", "=","0033h00000k2sBYZZZ")
    set @numberOfRows = RowCount(@rowset)
    IF @numberOfRows == 1 then
    set @row = Row(@rs, @cntr)
    ]%%
Email: %%=v(Field(@row, "Email"))=%%:<br>
FirstName: %%=v(Field(@row, "FirstName"))=%%:<br>
Total Records: %%=v(@numberOfRows)=%%<br>
%%[ endif ]%%
1
  • 1
    where are you setting the @rs and @cntr variables? In your example, I do not see them being set which means it would be null or error on @row. It also has no output if @numberofRows is > 1 in the example. Commented Jan 26, 2022 at 18:10

1 Answer 1

1

Looks like you intended to loop through the results. Here's how I'd approach it -- though you'll likely never have more than one row on the retrieve of a Contact by Id.

<script runat="server" type="text/javascript">

Platform.Load("core","1");

try {

</script>


%%[ 

set @rowset = RetrieveSalesforceObjects("Contact","Id,Email,FirstName","Id", "=","0033h00000k2sBYZZZ")
set @numberOfRows = RowCount(@rowset)

if @numberOfRows > 0 then

  output(concat("<br>numberOfRows: ", @numberOfRows))

  for @cntr = 1 to @numberOfRows do

        set @row = Row(@rowset, @cntr)
        set @Email = field(@row, "Email")
        set @FirstName = field(@row, "FirstName")

        output(concat("<br>Email: ", @email))
        output(concat("<br>FirstName: ", @FirstName))
      
  next @cntr

endif

]%%


<script runat="server" type="text/javascript">

} catch (e) {

    Write("<br>e:", Stringify(e))
}

</script>
4
  • Yeah I had the for loop earlier but changed it to an if. Unfortunately, I tried the code just as you wrote it and I still am getting the '500 - Internal server error'. If the syntax is correct and I confirmed the 18-digit CRM Contact ID is still valid, can you think of anything that might be causing it? I've retrieved from SFMC DE's plenty with with DE lookup functions but have yet to see SalesForce specific functions retrieve from SalesCloud. Commented Jan 26, 2022 at 20:22
  • general debugging for this sort of thing 1) paste it to ampscript.io and check for errors. in this case I got "Variable @rs has not been set" -> replace @rs by @rowset ; 2) use a try catch around the function: salesforce.stackexchange.com/questions/296953/… Commented Jan 26, 2022 at 21:17
  • I fixed the undefined var and added a try/catch. Commented Jan 26, 2022 at 21:29
  • The correction worked! Thank you both. Commented Jan 27, 2022 at 18:56

You must log in to answer this question.

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