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
RahulRahul 

hello friends, Iam getting this error while saving the record.System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Please find the full error below. Need your help

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, bucket1associatetobucket2: execution of AfterInsert caused by: System.TypeException: Invalid id value for this SObject type: a14p000000275tEAAQ Trigger.bucket1associatetobucket2: line 35, column 1: []

Bucket1__c is parent and Bucket_2__c is child.Iam trying to create buckel1 record and when borower_name__c is null , then create bucket_2__C record and associate it with parent which is bucket1__c.
​​​​​​​Please help to solve this error.

Please find my code below :-

trigger bucket1associatetobucket2 on Bucket1__c (after insert) {
  map<string,id> conEmailVsId = new map<string,id>();
List<Bucket_2__c> newbuck2 = new list<Bucket_2__c>();
    if(Trigger.isAfter && Trigger.isInsert) {
            for(Bucket1__c a1:trigger.new){
if(a1.Borrower_Name__c == null){

Bucket_2__c bb = new Bucket_2__c();
bb.id=a1.id;
bb.Addhar_Number__c = a1.Aadhar_Number__c;
bb.Name=a1.Name;
newbuck2.add(bb);
system.debug('values in newbuck2'+newbuck2);
}
    
    }
    if(newbuck2.size()>0){
    insert newbuck2;
    
    }
}
}
Best Answer chosen by Rahul
yogesh_patilyogesh_patil

In that case you will be having lookup of bucket 1 on bucket 2 right.

so while inserting the record you write the below line

bb.LOOKUPFIELD_APINAME=a1.id;

LOOKUPFIELD_APINAME is the api name of the lookup field on bucket 2 referencing bucket 1.

All Answers

yogesh_patilyogesh_patil
Hi ,

Here you are trying to insert Bucket_2__c bb  on insert of Bucket1__c. Please the comment the below line in your code.
bb.id=a1.id;

You cannot assign Id to a record which is being inserted from trigger

Mark the answer if it solves your query.
RahulRahul
hi yogesh, but i want to create the bucket2 record and link it to bucket1.how will it link if i dont make the bucket1 id equal to bucket2. There is a lookup between bucket1 and bucket2 where bucket2 is child.
yogesh_patilyogesh_patil

In that case you will be having lookup of bucket 1 on bucket 2 right.

so while inserting the record you write the below line

bb.LOOKUPFIELD_APINAME=a1.id;

LOOKUPFIELD_APINAME is the api name of the lookup field on bucket 2 referencing bucket 1.

This was selected as the best answer
RahulRahul
Thanks Yogesh, It worked :)
yogesh_patilyogesh_patil
In that case please like the answer