+ Start a Discussion
nani@rnani@r 

I WANT TO INSERT 20 FIELDS IN DATA MODEL AT ONCE?

IAM TRYING TO DO LIKE THIS BUT THAT DOES'T WORK IN DEVELOPER CONSOLE WHY SO.IT SHOWS ERROR(INSERT FAILED)


@istest
public class rajinsert{
static testmethod void raj(){
rajesh__c a = new rajesh__c();
a.name = 'Mumbai';
a.price__c=9000;
a.subject__c='sfdc';
rajesh__c b=new rajesh__c();
b.name = 'raj';
b.price__c=4000;
b.subject__c='c';
rajesh__c c=new rajesh__c();
c.name = 'raj';
c.price__c=4400;
c.subject__c=C++';
list<rajesh__c> ab=new list<rajesh__C>();
ab=[select name,price__C,subject__c from rajesh__C ];
ab.add(a);
ab.add(b);
ab.add(c);
insert ab;
}
}

Best Answer chosen by Admin (Salesforce Developers) 
goabhigogoabhigo

Starting from Spring'12 (if I am not wrong), access to Org data is not available through test classes by default. To access the data (basically through SOQL), you need to set @isTest(SeeAllData=true).

 

But here, your code will not work unless you change it:

list<rajesh__c> ab=new list<rajesh__C>();
// remove this --- ab=[select name,price__C,subject__c from rajesh__C ];
ab.add(a);
ab.add(b);
ab.add(c);
insert ab;

 

So if you write ab=[select....], ab will be populated with existing data (provided you have set SeeAllData=true), and then you are adding new data to it, then inserting whole list. This causes the error - existing records are already inserted, hence Id's will be created, now when you try to insert again, inserting record with an Id is not allowed - hence the error/exception.

 

Let me know if you need more clarification..

All Answers

goabhigogoabhigo

Starting from Spring'12 (if I am not wrong), access to Org data is not available through test classes by default. To access the data (basically through SOQL), you need to set @isTest(SeeAllData=true).

 

But here, your code will not work unless you change it:

list<rajesh__c> ab=new list<rajesh__C>();
// remove this --- ab=[select name,price__C,subject__c from rajesh__C ];
ab.add(a);
ab.add(b);
ab.add(c);
insert ab;

 

So if you write ab=[select....], ab will be populated with existing data (provided you have set SeeAllData=true), and then you are adding new data to it, then inserting whole list. This causes the error - existing records are already inserted, hence Id's will be created, now when you try to insert again, inserting record with an Id is not allowed - hence the error/exception.

 

Let me know if you need more clarification..

This was selected as the best answer
goabhigogoabhigo

Also, there is no connection between the title of the post and code you have posted !!

 

One more thing, you can modify the code to insert multiple records in a better way as below:

 

@istest
public class rajinsert{
 static testmethod void raj(){
  List<rajesh__c> lstRaj = new List<rajesh__C>();
  for(Integer i=0;i<3;i++) {
   lstRaj.add(new rajesh__c(a.name = 'Rajesh-'+i; price__c=9000,subject__c= i+ ' sfdc'));
  }
insert lstRaj;
}
}

 

It will insert 3 records. You can increase the loop limit accordingly.