Skip to main content
added 855 characters in body
Source Link
Boris Bachovski
  • 16.3k
  • 8
  • 50
  • 88

Since you have the merging field at the end of the string, wouldn't be easier to append it rather than replace it? You can create a method that will append strings:

public String mergeString(sObject recordObject, String eixstingString)
{
    // Some sObjects like Contact and Lead have FirstName and others have Name field
    return eixstingString + (recordObject.get('Name') == null ? (String)recordObject.get('FirstName') : (String)recordObject.get('Name'));
}

Then you can call it like this:

String sTemp = 'Name belongs to ';
Account oAccount = [select Id, Name FROM Account LIMIT 1];
String mergedString = mergeString(oAccount, sTemp); 

If you had the placeholder in the middle of the string:

String sTemp = 'Name belongs to {recordName} and there is more static text here';

Then you could use the replace method:

public String mergeString(sObject recordObject, String eixstingString)
{
    // Some sObjects like Contact and Lead have FirstName and others have Name field
    return eixstingString.replace('{recordName}', (recordObject.get('Name') == null ? (String)recordObject.get('FirstName') : (String)recordObject.get('Name')));
}

UPDATE: Based on your comments below it looks like you're trying to dynamically replace a placeholder with the field specified there. I think this final method should get you going:

public String mergeString(sObject recordObject, String eixstingString)
{
    String templateString = eixstingString.substring(eixstingString.indexOf('{'), eixstingString.indexOf('}') + 1); // results in {Account.AccountNumber}
    String fieldName = eixstingString.substring(eixstingString.indexOf('.') + 1, eixstingString.indexOf('}')); // results in "AccountNumber"
    return eixstingString.replace(templateString, (String)recordObject.get(fieldName));
}

Keep in mind that this will not work if you have multiple ., { or } characters in your template. Also you must query the field that is specified in the template.

Since you have the merging field at the end of the string, wouldn't be easier to append it rather than replace it? You can create a method that will append strings:

public String mergeString(sObject recordObject, String eixstingString)
{
    // Some sObjects like Contact and Lead have FirstName and others have Name field
    return eixstingString + (recordObject.get('Name') == null ? (String)recordObject.get('FirstName') : (String)recordObject.get('Name'));
}

Then you can call it like this:

String sTemp = 'Name belongs to ';
Account oAccount = [select Id, Name FROM Account LIMIT 1];
String mergedString = mergeString(oAccount, sTemp); 

If you had the placeholder in the middle of the string:

String sTemp = 'Name belongs to {recordName} and there is more static text here';

Then you could use the replace method:

public String mergeString(sObject recordObject, String eixstingString)
{
    // Some sObjects like Contact and Lead have FirstName and others have Name field
    return eixstingString.replace('{recordName}', (recordObject.get('Name') == null ? (String)recordObject.get('FirstName') : (String)recordObject.get('Name')));
}

Since you have the merging field at the end of the string, wouldn't be easier to append it rather than replace it? You can create a method that will append strings:

public String mergeString(sObject recordObject, String eixstingString)
{
    // Some sObjects like Contact and Lead have FirstName and others have Name field
    return eixstingString + (recordObject.get('Name') == null ? (String)recordObject.get('FirstName') : (String)recordObject.get('Name'));
}

Then you can call it like this:

String sTemp = 'Name belongs to ';
Account oAccount = [select Id, Name FROM Account LIMIT 1];
String mergedString = mergeString(oAccount, sTemp); 

If you had the placeholder in the middle of the string:

String sTemp = 'Name belongs to {recordName} and there is more static text here';

Then you could use the replace method:

public String mergeString(sObject recordObject, String eixstingString)
{
    // Some sObjects like Contact and Lead have FirstName and others have Name field
    return eixstingString.replace('{recordName}', (recordObject.get('Name') == null ? (String)recordObject.get('FirstName') : (String)recordObject.get('Name')));
}

UPDATE: Based on your comments below it looks like you're trying to dynamically replace a placeholder with the field specified there. I think this final method should get you going:

public String mergeString(sObject recordObject, String eixstingString)
{
    String templateString = eixstingString.substring(eixstingString.indexOf('{'), eixstingString.indexOf('}') + 1); // results in {Account.AccountNumber}
    String fieldName = eixstingString.substring(eixstingString.indexOf('.') + 1, eixstingString.indexOf('}')); // results in "AccountNumber"
    return eixstingString.replace(templateString, (String)recordObject.get(fieldName));
}

Keep in mind that this will not work if you have multiple ., { or } characters in your template. Also you must query the field that is specified in the template.

Source Link
Boris Bachovski
  • 16.3k
  • 8
  • 50
  • 88

Since you have the merging field at the end of the string, wouldn't be easier to append it rather than replace it? You can create a method that will append strings:

public String mergeString(sObject recordObject, String eixstingString)
{
    // Some sObjects like Contact and Lead have FirstName and others have Name field
    return eixstingString + (recordObject.get('Name') == null ? (String)recordObject.get('FirstName') : (String)recordObject.get('Name'));
}

Then you can call it like this:

String sTemp = 'Name belongs to ';
Account oAccount = [select Id, Name FROM Account LIMIT 1];
String mergedString = mergeString(oAccount, sTemp); 

If you had the placeholder in the middle of the string:

String sTemp = 'Name belongs to {recordName} and there is more static text here';

Then you could use the replace method:

public String mergeString(sObject recordObject, String eixstingString)
{
    // Some sObjects like Contact and Lead have FirstName and others have Name field
    return eixstingString.replace('{recordName}', (recordObject.get('Name') == null ? (String)recordObject.get('FirstName') : (String)recordObject.get('Name')));
}