You need to sign in to do that
Don't have an account?
swaran
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.
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.
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.
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...
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.
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).