I'm pulling in a field from Salesforce to Marketing Cloud from the Campaign Member object. The Campaign Member field points to a sales user who is assigned to the Account of the Campaign Member.

I'm referencing the "Full Name" field in the User object and I get a value that is: "LastName, FirstName"

Is there any way to flip the name to be "Firstname Lastname" with AMPScript? I can do this in Javascript like so:

    var name = "Donald, Duck D".split(",").reverse().join(" ").trim();
if(name.split(" ").length > 2) {
    name = name.split(" ");
    name.splice(1, 1);
    name = name.join(" ");    

Is there any other way to do this via AMPScript?


If you are 100% certain that it will always be in the format "Last, First" and there will be no other commas or other formats the name will come in, you can use BuildRowsetFromString() function utilizing the comma as delimiter.

Sample: (Lets assume the value of Full Name is 'Michael, George')


  SET @FullName = AttributeValue('Full Name')
  SET @Rowset = BuildRowsetFromString(@FullName,",")

  SET @fnamerow = Row(@Rowset, 2)
  SET @lnamerow = Row(@Rowset, 1)

  SET @FirstName = TRIM(Field(@fnamerow, 1)) /* To remove excess spacing at front or back */
  SET @LastName = TRIM(Field(lnamerow, 1))

<!---sample SFMC Variable Output to place in your content -->
%%=CONCAT(@FirstName, " ", "@LastName)=%%

OUTPUT: George Michael

Now be aware that this relies on consistent and accurate data, so if there is an accidental comma or incorrectly formatted name, it will break, and likely break big.

I would highly recommend if you need First and Last name as separate variables that you talk with your data people and have them make them as separate fields in your available data.

| improve this answer | |

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.