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
raysfdc1988raysfdc1988 

want to write a trigger which helps to create a new contact record on creating of custom object record,

trigger createcontact on student_profile__c (after insert) { 
    List<contact> listcontact = new List<contact>();
  for (Student_Profile__c oStudent : trigger.new) {      
contact oContact = new contact();
   oContact.AccountId = oStudent.Current_Institution__c;
   oContact.firstName = oStudent.First_Name__c;
    oContact.LastName =oStudent.Last_Name__c;
    oContact.Attendance_Points__c = oStudent.Attendance_Points__c;
    oContact.Birthday__c = oStudent.Birthday__c;
    oContact.City__c = oStudent.City__c;
    oContact.Country__c = oStudent.Country__c;
    oContact.Course_Program__c = oStudent.Course_Program__c;
    oContact.Course_Name__c = oStudent.Course_Name__c;
    oContact.Course_No__c = oStudent.Course_No__c;
 
    oContact.imageURL__c = oStudent.imageURL__c;
    oContact.Institution_Name__c = oStudent.Institution_Name__c;
  
   oContact.Student_ID__c=ostudent.Student_ID__c;
    oContact.Mentor_Points__c = oStudent.Mentor_Points__c;
    oContact.Mentor_Points_2__c = oStudent.Mentor_Points_2__c;



        listcontact.add(oContact);
    }

    if (listcontact.isEmpty() == false) {
        Database.insert(listcontact);
    }

here student.Student_ID__c is unique  custom ffield,  Now i can insert a record in a contact but I cant update it.. Means if i make any changes in custom object record then it not poulating in contact object record,,,

Please help me to update a contact records on change a change of custom record. ,,
Pablo_RoldanPablo_Roldan
Hi subramanya,

Have you tried to use the following line:
upsert(listcontact);
Instead of using:
Database.insert(listcontact);
 
Thanks,
Pablo
 
raysfdc1988raysfdc1988
hi Pablo,..

I tried that.. but its not working...

Now i insert a records but i cant update it, may b i have use field mapping using unique field student_ID__c, But i dont know how to do that
hitesh90hitesh90
Hello,

You have to create trigger on after update also to update contact record on updation of student record. and create student_Id__c field in contact to external Id.
see below updated trigger code.

Apex Trigger:
trigger createcontact on student_profile__c (after insert, after update) {
    List<contact> listcontact = new List<contact>();
    for (Student_Profile__c oStudent : trigger.new) {     
        contact oContact = new contact();
        oContact.LastName =oStudent.Name;
        oContact.Student_ID__c =oStudent.Student_ID__c;
        oContact.AccountId = oStudent.Current_Institution__c;
        oContact.firstName = oStudent.First_Name__c;
        oContact.LastName =oStudent.Last_Name__c;
        oContact.Attendance_Points__c = oStudent.Attendance_Points__c;
        oContact.Birthday__c = oStudent.Birthday__c;
        oContact.City__c = oStudent.City__c;
        oContact.Country__c = oStudent.Country__c;
        oContact.Course_Program__c = oStudent.Course_Program__c;
        oContact.Course_Name__c = oStudent.Course_Name__c;
        oContact.Course_No__c = oStudent.Course_No__c;       
        oContact.imageURL__c = oStudent.imageURL__c;
        oContact.Institution_Name__c = oStudent.Institution_Name__c;       
        oContact.Student_ID__c=ostudent.Student_ID__c;
        oContact.Mentor_Points__c = oStudent.Mentor_Points__c;
        oContact.Mentor_Points_2__c = oStudent.Mentor_Points_2__c;
        listcontact.add(oContact);
    }   
    if (listcontact.isEmpty() == false) {
        Database.upsert(listcontact,contact.Student_ID__c);
    }
}

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
Email :- hiteshpatel.aspl@gmail.com
My Blog:- http://mrjavascript.blogspot.in/
raysfdc1988raysfdc1988
thanks hitesh,


already i have created custom field student_id__c in contact object. and Student_id__c is a custom field in student object which is Autonumber ..


so when i create this student record then automaticataly  new contact record shoud be created, its working fine now,, but if i update any field value in student record then related contact field is not updating..

ex: if i create student record as First name: Hitesh Last name:Patel
now automaticaly Contact record is created : FN: Hitesh  and LS:Patel

now in student if change First name as: Hitesh99 then it should be automaticaly update in contact recors... student and contact r non-related objects..


hitesh90hitesh90
If this field is auto number then how can you set the value from your trigger?

oContact.Student_ID__c=ostudent.Student_ID__c;

it should give you compile error.
Error: Compile Error: Field is not writeable: Contact.Student_ID__c at line
raysfdc1988raysfdc1988
hi hitesh,

just i want to populate student record i contact object...

so i have  created custom field Stuent_ID__c in contact with Text as a DATA TYPE..
raysfdc1988raysfdc1988
in student object Student_ID is custom field with data type autonumber,

but in Contact object it is Text field which just populate the value of student ID field value...

hitesh90hitesh90
Make that Text field to "External Id" and use my above trigger code. so you will get your requirement.