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
Rahul Jain 262Rahul Jain 262 

Facing errors while excuting the code. Please help what should I do so that I can get Code excuted.

List<Account> lstAccrecords = new list<Account>();
for (integer counter = 1; counter <= 200; counter++)
{
    Account Acc = new Account();
    
        Acc.name = 'Testing for bulkification';
        Acc.AnnualRevenue = '2000'+counter;
        Acc.BillingAddress = 'Delhi';
        Acc.Phone= '898231851'+counter;
    
}
if(!lstAccrecords.isempty())
{
    insert lstAccrecords;
}
Best Answer chosen by Rahul Jain 262
Ruwantha BulegodaRuwantha Bulegoda
You are trying to assign invalid variable types for Annual Revenue and Billing Address. Annual Revenue is an integer value but with your '2000' (with quotes) it is assigned as strings. Remove the quotes. Then the address is a composite field. This means it is created with multiple fields as mentioned below. So you have to populate the city, street, and other values separately. Here is the working code for you. Also please mark this as the correct answer after checking.
List<Account> lstAccrecords = new list<Account>();
for (integer counter = 1; counter <= 200; counter++)
{
    Account Acc = new Account();

    Acc.name = 'Testing for bulkification';
    Acc.AnnualRevenue = 2000+counter;
    Acc.BillingCountry = 'India';
    Acc.BillingCity = 'Bombe';
    Acc.BillingPostalCode = '111';
    Acc.BillingStreet = '';
    Acc.billingCity = '';
    Acc.Phone= '898231851'+counter;

}
if(!lstAccrecords.isempty())
{
    insert lstAccrecords;
}

 

All Answers

Ruwantha BulegodaRuwantha Bulegoda
You are trying to assign invalid variable types for Annual Revenue and Billing Address. Annual Revenue is an integer value but with your '2000' (with quotes) it is assigned as strings. Remove the quotes. Then the address is a composite field. This means it is created with multiple fields as mentioned below. So you have to populate the city, street, and other values separately. Here is the working code for you. Also please mark this as the correct answer after checking.
List<Account> lstAccrecords = new list<Account>();
for (integer counter = 1; counter <= 200; counter++)
{
    Account Acc = new Account();

    Acc.name = 'Testing for bulkification';
    Acc.AnnualRevenue = 2000+counter;
    Acc.BillingCountry = 'India';
    Acc.BillingCity = 'Bombe';
    Acc.BillingPostalCode = '111';
    Acc.BillingStreet = '';
    Acc.billingCity = '';
    Acc.Phone= '898231851'+counter;

}
if(!lstAccrecords.isempty())
{
    insert lstAccrecords;
}

 
This was selected as the best answer
PriyaPriya (Salesforce Developers) 
Hey Rahul,

Address is the composilt field in salesforce. To learn more about it, refer this Link (https://developer.salesforce.com/docs/atlas.en-us.236.0.object_reference.meta/object_reference/compound_fields_address.htm)

Also refer this similar type of issue has been explained :- 
https://salesforce.stackexchange.com/questions/84288/address-field-set-address-via-apex

Kindly mark it as the best answer if it works for you.

 

Thanks & Regards,

Priya Ranjan

 


 
prabhakar Thummojiprabhakar Thummoji
Hi Rahul,
In your case AnnualRevenue field is "Integer type".So, you have to remove quotes for 2000 as below.
The below logic will be helpful for you to resolve your issue.

List<Account> lstAccrecords = new list<Account>();
for (integer counter = 1; counter <= 200; counter++)
{
    Account Acc = new Account();
    
        Acc.name = 'Testing for bulkification';
        Acc.AnnualRevenue = 2000+counter;
        Acc.BillingAddress = 'Delhi';
        Acc.Phone= '898231851'+counter;
    
}
if(!lstAccrecords.isempty())
{
    insert lstAccrecords;
}


Please mark as best answer,If its helped you on code execution.

Regards
Prabhakar.T
Jalagam KumarJalagam Kumar
List<Account> lstAccrecords = new list<Account>();
for (integer counter = 1; counter <= 200; counter++)
{
    Account Acc = new Account();
    
        Acc.name = 'Testing for bulkification';
        Acc.AnnualRevenue = 2000+counter;
        Acc.BillingAddress = 'Delhi';
        Acc.Phone= '898231851'+counter;
        lstAccrecords.add(Acc);
}
if(!lstAccrecords.isempty())
{
    insert lstAccrecords;
}