function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Waleed AlkhudairiWaleed Alkhudairi 

how to add prefix before field value in salesforce using dml

Hi All, 

I have phone field that i need to add "+" or 00 to dial for country how can i add this prefix using dml statments. here is my current one: 
 
list <lead>cs = [select name, id, phone, MobilePhone from lead]; 
for (lead c:cs) {
    c.Phone= c.MobilePhone; 
}
Update cs;

i want to add ' + then  c.MobilePhone 
NagendraNagendra (Salesforce Developers) 
Hi Waleed,

May I suggest you please refer to below link with a similar discussion which might help you further. Please let us know if this helps.

Thanks,
Nagendra
mritzimritzi
Following is updated code:
list<lead> cs = [select name, id, phone, MobilePhone from lead]; 
for (lead c:cs) {
    //if mobile phone doesn't has + or 00, then add + in the beginning
    if(String.valueOf(c.MobilePhone).left(1) != '+' && String.valueOf(c.MobilePhone).left(2) != '00')
        c.Phone= '+' + c.MobilePhone; 
}
Update cs;

Please mark this as Best Answer, if this helps solve your problem.
 
Raj VakatiRaj Vakati
You have to do it like this .. .


Step 1 : - Create a Custom Metadat with the Country Name and Its Prefix .
Step 2 : In the trigger get the all the custom metadata type ang get the contact country and do the perfix assigmenet 

try this code
list<lead> cs = [select name, id, phone, MobilePhone from lead];
Map<String,String> countryTOPre = new Map<String,String>() ;
// Get the values from Custome metadata and add them to map  
for (lead c:cs) {
   if(c.Country!=null){
	   c.MobilePhone = countryTOPre.get(c.Country)+c.MobilePhone ;
   }
}
Update cs;

 
Waleed AlkhudairiWaleed Alkhudairi
@mritzi your code is close. however, i need to be able to say, if i have 00 in the mobile field then i want to replace with +. also if there is no 00 or + i want to add +.  based on the field itself not the phone field. for example, i want to look into the mobilephone field and say, if there is no + then add it. and if there is 00 in the mobile field, i want to make it +. 
mritzimritzi
This is the updated code:
list<lead> cs = [select name, id, phone, MobilePhone from lead]; 
for (lead c:cs) {
    //if mobile phone doesn't has + or 00, then add + in the beginning
    if(String.valueOf(c.MobilePhone).left(1) != '+' && String.valueOf(c.MobilePhone).left(2) != '00')
        c.Phone= '+' + c.MobilePhone; 
	//if Mobile phone has 00, replace it with +
	else if(String.valueOf(c.MobilePhone).left(2) == '00')
        c.Phone= '+' + c.MobilePhone.right(c.MobilePhone.length-2); 
}
Update cs;

Please mark this as BEST ANSWER, if this helps solve your problem.
Waleed AlkhudairiWaleed Alkhudairi
but this looks at the phone field to update the mobile. what if there is a value of + or 00 already in the phone field ?
mritzimritzi

I modified the code you posted in code.
Please change field API names if required.

in the code that I provided, any value in Phone will be overwritten by the value from MobilePhone field. However, if you want data to be copied in the same field, simply use

c. Phone = '+' + c.Phone;
or 
c.MobilePhone = '+' + c.MobilePhone;

similarly make changes at other places as well

mritzimritzi
If the answer posted above has solved your issue, please mark it as best answer, or add details of any problems you faced after implementing suggestions