0

I have to retrieve Contact records, from CRM using AMPScript, based on the mobilePhone field. Now the data could be stored in international format or without the country code. In order to do so I had something like

%%[
    SET @contactRows = RetrieveSalesforceObjects("Contact","Id","MobilePhone","=",CONCAT("+",MOBILE_NUMBER))
    SET @contactRows2 = RetrieveSalesforceObjects("Contact","Id","MobilePhone","=",MOBILE_NUMBER)
]%%

I'm looking at a way to do something like

@contactRows.addAll(@contactRows2)

I'm trying to avoid doing a loop twice for each resultSet and executing the same code

Thanks for any help in advance

2

There's no equivalent of the JavaScript array1.concat(array2) for AMPScript rowsets, so any method of building a single rowset from two rowsets is a bit clunky and will involve a couple of loops. Here's a suggested method that builds a delimited string containing your Ids from both rowsets and then turns it back into a single rowset that you can execute your business logic on.

%%[
    SET @contactRows = RetrieveSalesforceObjects("Contact","Id","MobilePhone","=",CONCAT("+",MOBILE_NUMBER))
    SET @contactRows2 = RetrieveSalesforceObjects("Contact","Id","MobilePhone","=",MOBILE_NUMBER)

    SET @totRows = Add(RowCount(@contactRows),RowCount(@contactRows2))
    SET @ids = ''
    SET @offset = 0
    SET @rowSet = @contactRows

    FOR @i = 1 TO @totRows DO
        IF @i > RowCount(@contactRows) THEN
            /* Move to next rowset */
            SET @rowSet = @contactRows2
            SET @offset = RowCount(@contactRows)
        ENDIF

        SET @row = Row(@rowset,Subtract(@i,@offset))

        /* Add Current Id to the @ids string */
        SET @ids = Concat(@ids,Field(@row,'Id'))
        IF @i < @totRows THEN 
            SET @ids = Concat(@ids,',')
        ENDIF
    NEXT @i

    /* Create a single rowset containing ids from your string */
    SET @rowset = BuildRowSetFromString(@ids,',')

]%%
1

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.