You need to sign in to do that
Don't have an account?
JBabu
to create records checking the existing records
Hi,
I have a requirement where I need to check the database for existing records(like printer, scanner,xerox for object officeequip) if any of the three mentioned are not present in the database, then I need to create them.
Can some one please help me on this?
Thanks,
JBabu.
I tried this method and it is working fine;
List<officeEquip__c> oesNew = new List<officeEquip__c>();
// map<id,officeEquip__c> oesExisting= new Map<id,officeEquip__c>([select id,name from officeEquip__c]);
map<string,id> oesName = new map<string,id>();
for(officeEquip__c oe : [select name,id from officeEquip__c])
{
oesName.put(oe.name,oe.id);
}
if(!oesName.containsKey('Printer')){
oesNew.add(new officeEquip__c(name='Printer'));
}
if(!oesName.containsKey('Xerox')){
oesNew.add(new officeEquip__c(name='Xerox'));
}
if(!oesName.containsKey('Scanner')){
oesNew.add(new officeEquip__c(name='Scanner'));
}
insert oesNew;
All Answers
I have written the below code. Please check and let me know if there is any better way :
Set<officeEquip__c> oesNew = new Set<officeEquip__c>();
Set<officeEquip__c> oesExisting= [select name from officeEquip__c];
//Lets assume officeEquip__c has one record named 'Xerox'
for(officeEquip__c oes : oesExisting) {
if(oes.name!='Printer'){
oesNew.add(new officeEquip__c(name='Printer'));
}
}
for(officeEquip__c oes : oesExisting) {
if(oes.name!='Xerox'){
oesNew.add(new officeEquip__c(name='Xerox'));
}
}
for(officeEquip__c oes : oesExisting) {
if(oes.name!='Scanner'){
oesNew.add(new officeEquip__c(name='Scanner'));
}
}
insert oesNew;
Thanks,
JBabu.
Hey,
Just was going through the code,interesting!, are you able to perform DML(insert) on oesNew(which is a set)?
Hi,
I am getting following error message:
line 2, column 1: Illegal assignment from LIST<OfficeEquip__c> to SET<OfficeEquip__c>.
Can you suggest a way for this??
Yeah Sure,
Instead of these two lines,
Set<officeEquip__c> oesNew = new Set<officeEquip__c>();
Set<officeEquip__c> oesExisting= [select name from officeEquip__c];
Use List,
List<officeEquip__c> oesNew = new List<officeEquip__c>();
List<officeEquip__c> oesExisting= [select name from officeEquip__c];
For the concepts on List/Set, read,
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_core_concepts.htm
Mark it Solved, if the above solution helps.
Hi,
I tried using list it worked if I execute the above piece for first time. But I would be executing the class multiple times. But my requirement is, "Name" field (default field of any custom object here) should not take duplicate values..
Can you help me on this?
Thanks,
JBabu.
Ok,
Check this out,
Set<officeEquip__c> oesNew = new Set<officeEquip__c>();
List<officeEquip__c> oesExisting= [select name from officeEquip__c];
//Lets assume officeEquip__c has one record named 'Xerox'
for(officeEquip__c oes : oesExisting) {
if(oes.name!='Printer'){
oesNew.add(new officeEquip__c(name='Printer'));
}
}
for(officeEquip__c oes : oesExisting) {
if(oes.name!='Xerox'){
oesNew.add(new officeEquip__c(name='Xerox'));
}
}
for(officeEquip__c oes : oesExisting) {
if(oes.name!='Scanner'){
oesNew.add(new officeEquip__c(name='Scanner'));
}
}
List<officeEquip__c> LstoesNew = new List<officeEquip__c>(oesNew);// Convert the Set to List for DML.
insert LstoesNew //Insert DML.
Hi,
This didnot work. It is again inserting duplicates.
Thanks,
JBabu.
I tried this method and it is working fine;
List<officeEquip__c> oesNew = new List<officeEquip__c>();
// map<id,officeEquip__c> oesExisting= new Map<id,officeEquip__c>([select id,name from officeEquip__c]);
map<string,id> oesName = new map<string,id>();
for(officeEquip__c oe : [select name,id from officeEquip__c])
{
oesName.put(oe.name,oe.id);
}
if(!oesName.containsKey('Printer')){
oesNew.add(new officeEquip__c(name='Printer'));
}
if(!oesName.containsKey('Xerox')){
oesNew.add(new officeEquip__c(name='Xerox'));
}
if(!oesName.containsKey('Scanner')){
oesNew.add(new officeEquip__c(name='Scanner'));
}
insert oesNew;