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
swaranswaran 

Uniquness check

I want to check the uniquness of the contact object based on multiple fields.

I am loading the data from csv file to Contact object.Here i don't have Id to update the existing contact record.So I am using the combination of fields.(like last name,email,phone).I have written a trigger to update the record instead of insert.Trigger code is below.
trigger uniquetrigger on Contact (before insert){
Contact objuniqueCon;
for(Contact objCon:Trigger.new){
objuniqueCon=[select id,email,phone from Contact where email=:objCon.email and phone=:objCon.phone];
if(objuniqueCon.Id!=null) {
system.debug(objuniqueCon.Id);
objuniqueCon.email=objCon.email;
objuniqueCon.phone=objCon.phone;
update objuniqueCon;
}

}
}

but every time the record is been inserted instead of update.

lamayclamayc
The problem is that the trigger is a 'before insert'. That alone signifies that the end result will be an inserted record.
The question is, after this, do you have a newly inserted record plus an updated record?
 
Anyway, what it sounds like you really want is an upsert. This will insert the record if non-existent, otherwise it will do an update.
 
swaranswaran
Thanx lamayc

My requirement is as follows.

From CSv file I have to load records to Contact Object based on External ID.If the record is created in SFDC for First time it does not have any external id.When the same record is coming with the external id(in the CSV file), the record should get updated with the values in the CSV file.But, has SFDC don't have that external ID it will be an insert.
Can you suggest any method to implement this.


andresperezandresperez

Hi,

I recently had the a similar problem. I wanted to check uniqueness using more than one field.

For your problem, I would sugges doing somehting like this:

On the aObject you are inserting data, add one field that will be your key. You could have a fields like this:

'['+Field1+']['+Field2+']['+ ... +']['+FieldN+']'

Then use UPSERT. The system will detect automatically if it needs to insert or update based on this field. The field must be a text field and you should set the value in the "before Insert, before update" trigger.

Hope this helps...

swaranswaran
lamayc I want only to update the record.I have an external unique id.My main aim is to update this external unique id.
But, I am unable to stop the record to be inserted.
First it is updating the record then it is trying to insert- as that external id is unique it is through an error.Even it is rollbacking the updated record also.
Please help in this.


andresperezandresperez

Hi,

Trying doing the UPSERT with this unique key.

What you are trying to do, stopping the insert and doing an update can not be done. The only way to stop the insert from happening is to invoke the obj.addError() method. This will effectively cancel the insert from happening but it will also rollback any other DML transaction that was launched by this trigger (like the update).