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
r nareshr naresh 

how provide same data Account to contact using after insert trigger

im getting this error please give me the solutuion Thank i advance
------------------------------------------------------------------------------------------------------
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger samedata caused an unexpected exception, contact your administrator: samedata: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0 with id 0039000001KC02vAAD; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]: Trigger.samedata: line 22, column 1


trigger samedata on Account (before insert,after insert) 
{
    Account a1=new Account();
    if(trigger.isbefore&&trigger.isinsert)
    {
for(Account a:trigger.new)
{
    a.name='naresh';
    a.phone='9989099890';
    a.email__c='naresh@axa.com';
    a1=a;
}
    } 
    else if(trigger.isafter&&trigger.isinsert)
    {
       list<contact> lc= new list<contact>();
        for(contact c:[select account.name,phone,email from contact where account.id=:a1.id])
        {
            
            lc.add(c);
        }
     insert lc;
    }
}
sharathchandra thukkanisharathchandra thukkani
Because when you query you get Id of the record also.

for this "select account.name,phone,email from contact where account.id=:a1.id" query you also get the Id of the contact.

So you need to create new instance. try below code.

trigger samedata on Account (before insert,after insert) 
{
    Account a1=new Account();
    if(trigger.isbefore&&trigger.isinsert)
    {
for(Account a:trigger.new)
{
    a.name='naresh';
    a.phone='9989099890';
    a.email__c='naresh@axa.com';
    a1=a;
}
    } 
    else if(trigger.isafter&&trigger.isinsert)
    {
       list<contact> lc= new list<contact>();
        for(contact c:[select account.name,AccountId, phone,email from contact where account.id=:a1.id])
        {
            Contact c1 = new Contact(phone = c.Phone, email = c.email,AccountId = c.AccountId);
            lc.add(c1);
        }
     insert lc;
    }
}
Amit Chaudhary 8Amit Chaudhary 8
Please try below code.
trigger samedata on Account (before insert,after insert) 
{
    Account a1=new Account();
    if(trigger.isbefore&&trigger.isinsert)
    {
		for(Account a:trigger.new)
		{
			a.name='naresh';
			a.phone='9989099890';
			a.email__c='naresh@axa.com';
			a1=a;
		}
    } 
    else if(trigger.isafter && trigger.isinsert)
    {
       list<contact> lc= new list<contact>();
        for(contact c:[select account.name,phone,email from contact where account.id=:a1.id])
        {
            
            lc.add(c);
        }
		update lc;
    }
}
Issue is that you was trying insert the same record again. You should update that one. If you want to create same contact again then please try like below
trigger samedata on Account (before insert,after insert) 
{
    Account a1=new Account();
    if(trigger.isbefore&&trigger.isinsert)
    {
		for(Account a:trigger.new)
		{
			a.name='naresh';
			a.phone='9989099890';
			a.email__c='naresh@axa.com';
			a1=a;
		}
    } 
    else if(trigger.isafter && trigger.isinsert)
    {
       list<contact> lc= new list<contact>();
        for(contact c:[select accountid,account.name,phone,email,firstName,LastName from contact where accountid =:a1.id])
        {
            Contact cont = new Contact();
			cont.firstName =c.firstName;
			cont.phone =c.phone;
			cont.email =c.email;
			cont.accountid =c.accountid;
			cont.LastName =c.LastName;
            lc.add(cont);
        }
		if(lc.size() > 0)
		insert lc;
    }
}
Please  let us know if this will help u

 
siddarth rajsiddarth raj
All good. Just change the 'insert' to 'upsert'.
r nareshr naresh
hi thanks to all for responding ,still im i have one doubt
    The data is inserted in to Account but not inserted in contact (eventhough data is doubled in contact when i trying to inserted data in account)

please give me the solutuion