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
saikrishna ranginenisaikrishna rangineni 

Inserting Phone field in account record from method instance.

This is my Class


public class AccountHandler {
public static Account insertNewAccount(String name,INTEGER Phn) {
Account a = new Account();
a.Name = name;
a.phone = 'Phn';    
try
{
insert a;
} catch (Exception e) {
return null;
}
return a;
}
}



Calling from Execute Anonymous

AccountHandler.insertNewAccount('RKNN',12345);



In Account record page iam getting Phone field value as Phn . Instead of 12345 , which i had passed in method. How to get phone field value as 12345 as passed in execute anonymous block.
 
Best Answer chosen by saikrishna rangineni
Navin Selvaraj23Navin Selvaraj23
Hi Sai,

So, you have to pass phone number as String instead of Integer as below.
public class AccountHandler {
public static Account insertNewAccount(String name,String phn) {
Account a = new Account();
a.Name = name;
a.phone =  phn; 
try
{
insert a;
} catch (Exception e) {
return null;
}
return a;
}
}

Then calling from anonymous as below:
 
Calling from Execute Anonymous

AccountHandler.insertNewAccount('RKNN','12345');

Please check and let me know if it helps. If it  helps mark it as best answer.


Regards,
Navin S

All Answers

GovindarajGovindaraj
Hi Sai,

Remove the quotes in the variable like below,

a.phone = Phn;

If you put quotes, then compiler will consider it as string.

Thanks,
Govindaraj.S
Navin Selvaraj23Navin Selvaraj23
Hi Sai,
Please find below code.
public class AccountHandler {
public static Account insertNewAccount(String name,INTEGER phn) {
Account a = new Account();
a.Name = name;
a.phone =  phn; 
try
{
insert a;
} catch (Exception e) {
return null;
}
return a;
}
}

From Execute Anonymous Window, Please use the below code as your code.
 
AccountHandler.insertNewAccount('RKNN',12345);

Please mark it as the best answer if it helps you.

Best Regards,
Navin S
saikrishna ranginenisaikrishna rangineni
Hi Navin S,

 When i saved class as specified by you, means removing quotes('') to phn. Iam getting a error as Illegal Assignment from Integer to String.
saikrishna ranginenisaikrishna rangineni
Hi Govindaraj.S,


                         When I removed quotes('') to phn. Iam getting a error as Illegal Assignment from Integer to String.

   
Navin Selvaraj23Navin Selvaraj23
Hi Sai,

So, you have to pass phone number as String instead of Integer as below.
public class AccountHandler {
public static Account insertNewAccount(String name,String phn) {
Account a = new Account();
a.Name = name;
a.phone =  phn; 
try
{
insert a;
} catch (Exception e) {
return null;
}
return a;
}
}

Then calling from anonymous as below:
 
Calling from Execute Anonymous

AccountHandler.insertNewAccount('RKNN','12345');

Please check and let me know if it helps. If it  helps mark it as best answer.


Regards,
Navin S
This was selected as the best answer
saikrishna ranginenisaikrishna rangineni
HI Navin S,
 
Thank you so much. It works fine.
Regards ,
Saikrishna.