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
JBabuJBabu 

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.

Best Answer chosen by Admin (Salesforce Developers) 
JBabuJBabu

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

JBabuJBabu

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.

 

Srikant JoshiSrikant Joshi

Hey,

 

Just was going through the code,interesting!, are you able to perform DML(insert) on oesNew(which is a set)?

 

 

JBabuJBabu

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??

Srikant JoshiSrikant Joshi

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.

 

 


 

 

 


JBabuJBabu

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.

 

Srikant JoshiSrikant Joshi

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.

JBabuJBabu

Hi,

 

This didnot work. It is again inserting duplicates.

 

Thanks,

JBabu.

JBabuJBabu

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;

This was selected as the best answer