while i am uploading from csv file i need to create account and contact i have achieved this using apex code. i need to check whether the account name already exist or not. Whether i need to write apex trigger for this. kindly give me any idea to solve this. i am not using Dataloader i need to do this using programming.


 public Pagereference ReadFile()
    filelines = nameFile.split('\n');
    accstoupload = new List<Account>();
    contoupload = new List<Contact>();
    opptoupload = new List<Opportunity>();
    custtoupload = new List<CustomLead__c>();
            for (Integer i=1;i<filelines.size();i++)
        String[] inputvalues = new String[]{};
        inputvalues = filelines[i].split(',');

        Account a = new Account();
        a.Name = inputvalues[0];

    insert accstoupload;
    catch (Exception e) {
        ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later');

Create a Workflow Action when a record is created, and every time it’s edited to copy the Account Name into a unique field, say Name_Copy__c.

Edit/Addition: Please ensure you have set Rule Criteria = OR ( ISNEW(), ISCHANGED ( Name ) )

Optionally, you might want to hide Name_Copy__c from the page layout.

|improve this answer|||||
  • This won't work, your Workflow Field update is causing a second update to fire, and your workflow will fail if a duplicate record is found, however, your record will already be inserted, read: salesforce.com/us/developer/docs/apexcode/Content/… – pjcarly Apr 18 '13 at 6:49
  • @PJC Thank you for your reply. I have implemented this and it does work. A failed workflow in case of a duplicate record does ensure me that the record is not inserted. A second update will not fire because of the Rule Criteria which I have just added. Thank you for pointing this out. – Shumon Saha Apr 18 '13 at 7:07

Easiest way to accomplish this is creating a custom field on account named Name_Copy__c Set it to:

  • External Id
  • Unique (Case Sensitive or not, up to you)

Best thing you can do is create a trigger on Account, because I guess you would want the uniqueness to be checked versus all data entries, and not only records you uploaded through your CSV file. Then, in your trigger, in the BeforeInsert and BeforeUpdate set the value of the Name_Copy__c field with the value of the Name field. This way your insert/update will fail when the Account Name would already exist.

|improve this answer|||||
  • thanks for the reply.can you just tell me same code for this. – Lavanya Sanathkumar Apr 18 '13 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.