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
Vikram Singh 157Vikram 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;
}
Best Answer chosen by Vikram Singh 157
Devanshu soodDevanshu sood
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,Contact_Relationship__c  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

Devanshu soodDevanshu sood
can you specify the error?
Vikram Singh 157Vikram Singh 157
Hi ! Devanshu ,
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
 
Devanshu soodDevanshu sood
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,Contact_Relationship__c  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 

This was selected as the best answer
Vijay AdusumilliVijay Adusumilli
You are not retrieving Contact_Relationship__c from Contact in:
  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];
Devanshu soodDevanshu sood
you have to fetch the feilds you are going to use in code like below
List<contact> conlist = [select id,name, Contact_Relationship__c from contact where id in : conid];
Vikram Singh 157Vikram Singh 157
Thanks Devanshu , now it works .
Devanshu soodDevanshu sood

Hi Vikram,

Thanks for your confirmation.

Keep learning!!!

Thanks,
​Devanshu