+ Start a Discussion
SHAIK MOHAMMAD YASEENSHAIK MOHAMMAD YASEEN 

How Skip a record from trigger.new and insert remaining records in trigger.new

Hi All,

I am new to salesforce development. Need all your help here.

I have a  scenario to write a trigger where while creating List of records in Account object it should check if there are any duplicates with reference to account name.
If exist then it should skip those records and insert the remaing records. 
so my requirement is no need to displayt any error. just have to skip the unfavourable records from trigger.new and save the favourable records in trigger.new alone. 

Please assist me here


 
Best Answer chosen by SHAIK MOHAMMAD YASEEN
ManojSankaranManojSankaran
Hi Shaik,

Now i understand your requirement clearly,

As i mentioned above we will have any one of the solution in place to avoid the duplicates(Either unique field or a trigger).

When inserting a list of account you have to use Database.insert instead of Insert

So that it will insert only the success records and leave the duplicate records. Later if you want to see why some records has been failed you have use the save result methods.

See how to use Database.Insert
http://blog.biswajeetsamal.com/post/2015/11/23/difference-between-insert-and-database-insert-in-salesforce/

Thanks
Manoj S


 

All Answers

ManojSankaranManojSankaran
Hi Shaik,

Below is my assumption, That we are loading multiple account into Salesforce from either dataloader or through ETL Tool.

This can be acomplised in many ways.
Configuration
1. you can create a simple text field and make that text field unique
2. Create a workflow rule to populate the account name in that text field. 
3. Since that text field is unique, system will restrict those records to get inserted.

You can set duplicate rules in salesforce to restrict  the account creation.

Customization
1. You need to create a trigger and call "AddError" Method in case you find a duplicate.


In both the case we need to add error message. Then only we will know that we have restricted some accounts based on the duplicate criteria. Since i am assuming that data has been loaded to salesforce, we will get the error file and we can check the data once agian.


Please mark it as answer if it really help you solve your problem.

Thanks
Manoj S



 
SHAIK MOHAMMAD YASEENSHAIK MOHAMMAD YASEEN
Hi @ManojSankaran,

Thanks for your rsponse!!!!

But my criteria here is different i dont want to display any error message. only the records which are not repetitive(considering name field in account as unique for comparision) must be inserted skipping the repetitive records. i will create multiple records through anonymous window.

I should achieve this through triggers.

for example if i am inserting 10 records (so those 10 records will be present in trigger.new) in those 10 records two records already exist in database and remaining 8 records are new. then i should skip inserting those two records and insert only remaining 8 records and no need to display error. in other words we can say this like partial insertion must happen.

can you please help me with piece of in order to achieve this. 

Thanks,
Shaik.

 
ManojSankaranManojSankaran
Hi Shaik,

Now i understand your requirement clearly,

As i mentioned above we will have any one of the solution in place to avoid the duplicates(Either unique field or a trigger).

When inserting a list of account you have to use Database.insert instead of Insert

So that it will insert only the success records and leave the duplicate records. Later if you want to see why some records has been failed you have use the save result methods.

See how to use Database.Insert
http://blog.biswajeetsamal.com/post/2015/11/23/difference-between-insert-and-database-insert-in-salesforce/

Thanks
Manoj S


 
This was selected as the best answer
Mina Michel GorgyMina Michel Gorgy
Hello Shaik Mohammed,

Does this solution fit your needs?
https://developer.salesforce.com/forums/?id=906F000000090GnIAI

Please mark my answer as accepted, if it works for you. Thanks!
SHAIK MOHAMMAD YASEENSHAIK MOHAMMAD YASEEN
Hi ManojSankaran,

you are very correct i got my requirement done. Thanks for your help

Best Regards,
shaik
SHAIK MOHAMMAD YASEENSHAIK MOHAMMAD YASEEN
Hi Mina Michel Gorgy,

Thanks for your valuable time to help me!!!!

But my requirement is not to throw error my requiremnt is if we have list of records I should be able to do partial insertion. 

If we write the trigger to throw error and try to insert list of records with out using database.insert then we will get error but another favourable records will not be inserted. 

Best Regards,
Shaik.