You need to sign in to do that
Don't have an account?
Vikram Singh 157
Hi ! Trigger gives error while save contact record
Req :Create the field called “Contact Relationship” checkbox on the Contact Object and create the object called “Contact Relationship” which is related list to the Contacts. (Look Up Relationship).
Now logic is when we create contact by checking contact relationship checkbox, then Contact Relationship will be created automatically for that contact.
Trigger :
trigger CreateChild on Contact (after insert) {
set<id> conid = new set<id>();
list<Contact_Relationship__c> con1 = new list<Contact_Relationship__c>();
for (contact c : trigger.new){
conid.add(c.id);
}
List<contact> conlist = [select id,name from contact where id in : conid];
for (contact c : conlist){
if(c.Contact_Relationship__c == true){
Contact_Relationship__c cr = new Contact_Relationship__c();
cr.name = c.lastname;
cr.contact__c= c.id;
con1.add(cr);
}
}
insert con1;
}
Now logic is when we create contact by checking contact relationship checkbox, then Contact Relationship will be created automatically for that contact.
Trigger :
trigger CreateChild on Contact (after insert) {
set<id> conid = new set<id>();
list<Contact_Relationship__c> con1 = new list<Contact_Relationship__c>();
for (contact c : trigger.new){
conid.add(c.id);
}
List<contact> conlist = [select id,name from contact where id in : conid];
for (contact c : conlist){
if(c.Contact_Relationship__c == true){
Contact_Relationship__c cr = new Contact_Relationship__c();
cr.name = c.lastname;
cr.contact__c= c.id;
con1.add(cr);
}
}
insert con1;
}
try this
let me know if it works
All Answers
Error :
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger CreateChild caused an unexpected exception, contact your administrator: CreateChild: execution of AfterInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contact.Contact_Relationship__c: Trigger.CreateChild: line 9, column 1
try this
let me know if it works
List<contact> conlist = [select id,name from contact where id in : conid];.
Change it to: List<contact> conlist = [select id,name, Contact_Relationship__c from contact where id in : conid];
List<contact> conlist = [select id,name, Contact_Relationship__c from contact where id in : conid];
Hi Vikram,
Thanks for your confirmation.
Keep learning!!!
Thanks,
Devanshu