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
chinna r 5chinna r 5 

triggers errors

Hi,
Can anyone help me plz?I am trying to insert account record so that related created will b created.Below is the code:

Trigger triggername on Account(after insert){
List<Contact> cclist = new list<Contact>();
for(Account acc:trigger.new){
Contact c = new Contact();
c.LastName__c = acc.LastName__c;
c.accountid = acc.id;
cclist.add(c);
}
insert cclist;
}

I am not able to save the Account record and below is the error.No other Triggers are activated too.User-added image
ANUTEJANUTEJ (Salesforce Developers) 
Hi Chinna,

So as the error says the issue is with duplicate rules there might be already records with the details so it is not possible to reinsert record.

You need to make sure the records entered are not duplicates i.e., they should not be already present in the org, so to make it unique you can make use of a counter or concatinate a unique detail so as to make sure the contact record is unique.
Trigger triggername on Account(after insert){
List<Contact> cclist = new list<Contact>();
Integer Count=0;
for(Account acc:trigger.new){
Count+=1;
Contact c = new Contact();
c.LastName__c = acc.LastName__c+Count;
c.accountid = acc.id;
cclist.add(c);
}
insert cclist;
}

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Malika Pathak 9Malika Pathak 9
Hi,
Normally, this error relates to the Salesforce's Standard Duplicate rule.
If you have this rule enabled, it'll be showing an alert every time you are trying to create a duplicate record.
And you also haven't filled the required field of contact i.e last name of contact.
This code is working fine in my org.
 if(trigger.isafter && trigger.isinsert){
    List<Contact> cclist = new list<Contact>();
    for(Account acc:trigger.new){
        Contact c = new Contact();
        c.lastname__c = acc.last_name__c;
        c.lastname ='sas';
        c.accountid = acc.id;
        cclist.add(c);
    }
    insert cclist;
    }