1

I need help to write a method which will do some kind of string merge. I have an object having some data. It may be any object.Like this:

Account oAccount = [select id,Name from Account limit 1];

And I have a certain string like this:

String sTemp = "Name belongs to {oAccount.Name}";

I need to call a method. Suppose the method name is mergeString.

mergeString(oAccount, sTemp);

The method should be able to return merged strings like :

"Name belongs to Kumar" {If Account name is Kumar}

How to achieve this?

3 Answers 3

1

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.

8
  • Thanks for help but i want to pass a string which i got from sObject's field after soql Account = [select Name, accountnumber, billingcity, billingstate, billingcountry, billingpostalcode, phone,Fax,Rating,CreatedBy.companyname from Account where Id =: oId LIMIT 1] ; if(Account != null){ for(Document tempDoc : lstSelectedDoc){ sTemp = tempDoc.Template_Info__c; oppText.value += sTemp ; oppText.value += '</tBody></table>'; } } Note: Template_Info__c value is like {Name is '+Account.accountnumber+'} Commented Aug 4, 2015 at 6:04
  • mergeString(oAccount, oAccount.Template_Info__c); - Just pass the string that you want to use as a base as a second parameter Commented Aug 4, 2015 at 6:10
  • I have tried this but actually i return whole string as it is its not renders object's value to string Commented Aug 4, 2015 at 6:13
  • Make sure your template placeholder matches the first parameter in the replace method. E.g. Template = 'Name is {account.Name}', your method should be return existingString.replace('{account.Name}', (String)recordObject.get('Name')); Commented Aug 4, 2015 at 6:21
  • 1
    Thanks I think this logic gonna work. I really appreciate your valuable time for me. Thanks a lot ... Keep going brother .... Commented Aug 4, 2015 at 6:46
1

This is a very simple piece of code

public string mergeString(oAccount, sTemp) {
  string str = 'Name belongs to '+oAccount; //or str = sTemp+' '+oAccount;
  return str;
}
2
  • Thanks for help but string should not be inside of method we have just pass string to method . One thing more i want to pass a string which i got from sObject's field after soql Commented Aug 4, 2015 at 6:00
  • update my answer see the comment part. You can pass as many argument as you want and then add them. Commented Aug 4, 2015 at 6:02
0

you can try below code in the merge string method

String mergedString = sTemp + oAccount.Name;
Return mergedString;

You must log in to answer this question.

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