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
mw6mw6 

Help on after insert and after update trigger

I have the below trigger to create Person Account when a new record is created in a custom object Student__c

I would like to seek some expert help on how to modify the trigger to have 'after update' if the record on Student__c is modified, the changes also should be done on the corresponding person account.

The external ID field on Student__c is Student_id__c and also unique field is NRIC__c. I also store the Id from Student__c into Person Account (Student_Id_from_Student_Prospects__pc,NRIC_PC, Salesforce_Id_from_Student_Object__pc

Please help to modify the below trigger to have 'after insert' and 'after update'
-------------------
trigger CreatePersonAccountfromStudent on Student__c (after insert)
{
    List<account> acc=new List<account>();
    Map<Id, String> m = new Map<Id, String>();
    RecordType rt = [select id from RecordType where SobjectType='Account' and Name='Student' Limit 1];
    for(student__c stud:Trigger.New)
    {
      acc.add(new account(
        Autocreated__pc=True,
        Block__pc=Stud.Block__c,
        Bulk_Load__pc=Stud.Bulk_Load__c,
        Current_Acedamic_Year__pc=Stud.Current_Acedamic_Year__c,
        Date_of_Collection__pc=Stud.Date_of_Collection__c,
        Discount_Percentage__pc=Stud.Discount_Percentage__c,
        Discount_Policy__pc=Stud.Discount_Policy__c,
        Discount_Remarks__pc=Stud.Discount_Remarks__c,
        Discount_Type__pc=Stud.Discount_Type__c,
        English__pc=Stud.English__c,
        Event_Name__pc=Stud.Event_Name__c,
        Exclude_Registration__pc=Stud.Exclude_Registration__c,
        Father_Email__pc=Stud.Father_Email__c,
        Father_Name__pc=Stud.Father_Name__c,
        Father_NRIC__pc=Stud.Father_NRIC__c,
        Father_Office_Phone__pc=Stud.Father_Office_Phone__c,
        Father_Primary_Contact__pc=Stud.Father_Primary_Contact__c,
        Father_Remark__pc=Stud.Father_Remark__c,
        Father_Staff__pc=Stud.Father_Staff__c,
        Gender__pc=Stud.Gender__c,
        GEP__pc=Stud.GEP__c,
        Guardian_Email__pc=Stud.Guardian_Email__c,
        Guardian_Mobile__pc=Stud.Guardian_Mobile__c,
        Guardian_Name__pc=Stud.Guardian_Name__c,
        Guardian_NRIC__pc=Stud.Guardian_NRIC__c,
        Guardian_Office_Phone__pc=Stud.Guardian_Office_Phone__c,
        Guardian_Primary_Contact__pc=Stud.Guardian_Primary_Contact__c,
        Guardian_Relationship__pc=Stud.Guardian_Relationship__c,
        Guardian_Remarks__pc=Stud.Guardian_Remark__c,
        Guardian_Staff__pc=Stud.Guardian_Staff__c,
        How_did_you_hear_about_us__pc=Stud.How_did_you_hear_about_us__c,
        Is_Mind_Stretcher_Staff_Mother__pc=Stud.Is_Mind_Stretcher_Staff_Mother__c,
        LastName=Stud.name__c,
        Level__pc=Stud.Level__c,
        Level_Promoted_DateTime__pc=Stud.Level_Promoted_DateTime__c,
        Maths__pc=Stud.Maths__c,
        Mobile_No_Father__pc=Stud.Mobile_No_Father__c,
        Mother_Email__pc=Stud.Mother_Email__c,
        Mother_Name__pc=Stud.Mother_Name__c,
        Mother_No_Mobile__pc=Stud.Mobile_No_Mother__c,
        Mother_NRIC__pc=Stud.Mother_NRIC__c,
        Mother_Office_Phone__pc=Stud.Mother_Office_Phone__c,
        Mother_Primary_Contact__pc=Stud.Mother_Primary_Contact__c,
        Mother_Remark__pc=Stud.Mother_Remark__c,
        Mother_Staff__pc=Stud.Mother_Staff__c,
        MS_Start_Kit_Bag__pc=Stud.MS_Start_Kit_Bag__c,
        Nationality__pc=Stud.Nationality__c,
        NRIC__pc=Stud.NRIC__c,
        NTUC_10_Discount__pc=Stud.NTUC_10_Discount__c,
        NTUC_Card_Expiry__pc=Stud.NTUC_Card_Expiry__c,
        NTUC_Remark__pc=Stud.NTUC_Remark__c,
        Others__pc=Stud.Others__c,
        Others_How_did_you_hear_about_us__pc=Stud.Others_How_did_you_hear_about_us__c,
        Person_Contact__pc=Stud.Person_Contact__c,
        PersonBirthdate=Stud.DOB__c,
        PersonHomePhone=Stud.Home_No__c,
        PersonMailingPostalCode=Stud.Postal_Code__c,
        PersonMailingStreet=Stud.Street_Temp__C,
        Primary_Contact_Number__pc=Stud.Contact_number__c,
        Primary_Email__pc=Stud.Primary_Email__c,
        Primary_Name__pc=Stud.Primary_Name__c,
        Primary_Remark__pc=Stud.Primary_Remark__c,
        Promotion__pc=Stud.Promotion__c,
        Reading_Oceans_Email__pc=Stud.Reading_Oceans_Email__c,
        Reading_Oceans_End_Date__pc=Stud.Reading_Oceans_End_Date__c,
        Reading_Oceans_Start_Date__pc=Stud.Reading_Oceans_Start_Date__c,
        Reading_Oceans_Subscrip__pc=Stud.Reading_Oceans_Subscrip__c,
        recordtypeid=rt.id,
        Registration_Centre__pc=Stud.Registration_Centre__c,
        Related_Centres__pc=Stud.Related_Centres__c,
        Remarks__pc=Stud.Remarks__c,
        RO_Date_Send_User__pc=Stud.RO_Date_Send_User__c,
        RO_Remark__pc=Stud.RO_Remark__c,
        RO_Type__pc=Stud.RO_Type__c,
        Safra_Card_Expiry__pc=Stud.Safra_Card_Expiry__c,
        Safra_Card_Holding__pc=Stud.Safra_Card_Holding__c,
        Safra_Remark__pc=Stud.Safra_Remark__c,
        Salesforce_Id_from_Student_Object__pc=Stud.ID,
        School__pc=Stud.School__c,
        Science__pc=Stud.Science__c,
        Secondary_Stream__pc=Stud.Secondary_Stream__c,
        Start_Date__pc=Stud.Start_Date__c,
        Street__pc=Stud.Street__c,
        Student_Id_from_Student_Prospects__pc=Stud.name,
        Student_Old_Reference_ID__pc=Stud.Student_Old_Reference_ID__c,
        Student_Status__pc=Stud.Student_Status__c,
        Student_Type__pc='Student',
        Tick_to_activate_inter_branch_discount__pc=Stud.Tick_to_activate_inter_branch_discount__c,
        Type_Course_Fee__pc=Stud.Type_Course_Fee__c,
        Unit_Number__pc=Stud.Unit_Number__c
    ));
 }
    insert acc;
}
AvaneeshAvaneesh
Hi 

check this way u will help you here studnet_Id_from_student_Prospects__pc field have student__c id Right
trigger CreatePersonAccountfromStudent on Student__c (after insert,after update)
{
    List<account> acc=new List<account>();
    Map<Id, Id> m = new Map<Id, Id>();
    RecordType rt = [select id from RecordType where SobjectType='Account' and Name='Student' Limit 1];
 
   if(trigger.isUpdate){
 
   for(account acc:[select id, studnet_Id_from_student_Prospects__pc from account where studnet_Id_from_student_Prospects__pc IN : trigger.new]){
   
      m.put(studnet_Id_from_student_Prospects__pc , acc.id);    

}
}

    for(student__c stud:Trigger.New){    
      account ac = new account();
        if(trigger.isupdate){ // add this line here 
      ac.id = m.get(stud.id).id;
      }
      ac.Autocreated__pc=True,
       ac.Block__pc=Stud.Block__c,
        ac.Bulk_Load__pc=Stud.Bulk_Load__c,
         ac.Current_Acedamic_Year__pc=Stud.Current_Acedamic_Year__c,
         ac.Date_of_Collection__pc=Stud.Date_of_Collection__c,
         ac.Discount_Percentage__pc=Stud.Discount_Percentage__c,
         ac.Discount_Policy__pc=Stud.Discount_Policy__c,
         ac.Discount_Remarks__pc=Stud.Discount_Remarks__c,
    -----------------------------------------------------------
          write your other line which you want update 

    -----------------------------------------------------------
     acc.add(ac);
     }

   if(acc.size>0){
   upsert acc;
}







Thank you 
Avaneesh Singh
mw6mw6
Hi Avaneesh,

Thank you very much. 

I have followed your insturction and code and my trigger is as below. I have an error message "Error: Compile Error: Invalid bind expression type of Student__c for column of type String at line 9 column 20"   On the Student__c object this field is auto number, but on Person Account it is a text field, any idea, on how to correct this in trigger.

trigger CreatePersonAccountfromStudent on Student__c (after insert,after update)
{
    List<account> acc=new List<account>();
    Map<Id, Id> m = new Map<Id, Id>();
    RecordType rt = [select id from RecordType where SobjectType='Account' and Name='Student' Limit 1];
 
   if(trigger.isUpdate){
 
   for(account acc:[select id, nric__pc, student_Id_from_student_Prospects__pc from account where student_Id_from_student_Prospects__pc IN : trigger.new]){
       m.put(student_Id_from_student_Prospects__pc , acc.id);   
}
}
    for(student__c stud:Trigger.New){   
      account ac = new account();
        if(trigger.isupdate){ // add this line here
      ac.id = m.get(stud.id).id;
      }
        ac.Autocreated__pc=True;
        ac.Block__pc=Stud.Block__c;
        ac.Bulk_Load__pc=Stud.Bulk_Load__c;
        ac.Current_Acedamic_Year__pc=Stud.Current_Acedamic_Year__c;
        ac.Date_of_Collection__pc=Stud.Date_of_Collection__c;
        ac.Discount_Percentage__pc=Stud.Discount_Percentage__c;
        ac.Discount_Policy__pc=Stud.Discount_Policy__c;
        ac.Discount_Remarks__pc=Stud.Discount_Remarks__c;
        ac.Discount_Type__pc=Stud.Discount_Type__c;
        ac.English__pc=Stud.English__c;
        ac.Event_Name__pc=Stud.Event_Name__c;
        ac.Exclude_Registration__pc=Stud.Exclude_Registration__c;
        ac.Father_Email__pc=Stud.Father_Email__c;
        ac.Father_Name__pc=Stud.Father_Name__c;
        ac.Father_NRIC__pc=Stud.Father_NRIC__c;
        ac.Father_Office_Phone__pc=Stud.Father_Office_Phone__c;
        ac.Father_Primary_Contact__pc=Stud.Father_Primary_Contact__c;
        ac.Father_Remark__pc=Stud.Father_Remark__c;
        ac.Father_Staff__pc=Stud.Father_Staff__c;
        ac.Gender__pc=Stud.Gender__c;
        ac.GEP__pc=Stud.GEP__c;
        ac.Guardian_Email__pc=Stud.Guardian_Email__c;
        ac.Guardian_Mobile__pc=Stud.Guardian_Mobile__c;
        ac.Guardian_Name__pc=Stud.Guardian_Name__c;
        ac.Guardian_NRIC__pc=Stud.Guardian_NRIC__c;
        ac.Guardian_Office_Phone__pc=Stud.Guardian_Office_Phone__c;
        ac.Guardian_Primary_Contact__pc=Stud.Guardian_Primary_Contact__c;
        ac.Guardian_Relationship__pc=Stud.Guardian_Relationship__c;
        ac.Guardian_Remarks__pc=Stud.Guardian_Remark__c;
        ac.Guardian_Staff__pc=Stud.Guardian_Staff__c;
        ac.How_did_you_hear_about_us__pc=Stud.How_did_you_hear_about_us__c;
        ac.Is_Mind_Stretcher_Staff_Mother__pc=Stud.Is_Mind_Stretcher_Staff_Mother__c;
        ac.LastName=Stud.name__c;
        ac.Level__pc=Stud.Level__c;
        ac.Level_Promoted_DateTime__pc=Stud.Level_Promoted_DateTime__c;
        ac.Maths__pc=Stud.Maths__c;
        ac.Mobile_No_Father__pc=Stud.Mobile_No_Father__c;
        ac.Mother_Email__pc=Stud.Mother_Email__c;
        ac.Mother_Name__pc=Stud.Mother_Name__c;
        ac.Mother_No_Mobile__pc=Stud.Mobile_No_Mother__c;
        ac.Mother_NRIC__pc=Stud.Mother_NRIC__c;
        ac.Mother_Office_Phone__pc=Stud.Mother_Office_Phone__c;
        ac.Mother_Primary_Contact__pc=Stud.Mother_Primary_Contact__c;
        ac.Mother_Remark__pc=Stud.Mother_Remark__c;
        ac.Mother_Staff__pc=Stud.Mother_Staff__c;
        ac.MS_Start_Kit_Bag__pc=Stud.MS_Start_Kit_Bag__c;
        ac.Nationality__pc=Stud.Nationality__c;
        ac.NRIC__pc=Stud.NRIC__c;
        ac.NTUC_10_Discount__pc=Stud.NTUC_10_Discount__c;
        ac.NTUC_Card_Expiry__pc=Stud.NTUC_Card_Expiry__c;
        ac.NTUC_Remark__pc=Stud.NTUC_Remark__c;
        ac.Others__pc=Stud.Others__c;
        ac.Others_How_did_you_hear_about_us__pc=Stud.Others_How_did_you_hear_about_us__c;
        ac.Person_Contact__pc=Stud.Person_Contact__c;
        ac.PersonBirthdate=Stud.DOB__c;
        ac.PersonHomePhone=Stud.Home_No__c;
        ac.PersonMailingPostalCode=Stud.Postal_Code__c;
        ac.PersonMailingStreet=Stud.Street_Temp__C;
        ac.Primary_Contact_Number__pc=Stud.Contact_number__c;
        ac.Primary_Email__pc=Stud.Primary_Email__c;
        ac.Primary_Name__pc=Stud.Primary_Name__c;
        ac.Primary_Remark__pc=Stud.Primary_Remark__c;
        ac.Promotion__pc=Stud.Promotion__c;
        ac.Reading_Oceans_Email__pc=Stud.Reading_Oceans_Email__c;
        ac.Reading_Oceans_End_Date__pc=Stud.Reading_Oceans_End_Date__c;
        ac.Reading_Oceans_Start_Date__pc=Stud.Reading_Oceans_Start_Date__c;
        ac.Reading_Oceans_Subscrip__pc=Stud.Reading_Oceans_Subscrip__c;
        ac.recordtypeid=rt.id;
        ac.Registration_Centre__pc=Stud.Registration_Centre__c;
        ac.Related_Centres__pc=Stud.Related_Centres__c;
        ac.Remarks__pc=Stud.Remarks__c;
        ac.RO_Date_Send_User__pc=Stud.RO_Date_Send_User__c;
        ac.RO_Remark__pc=Stud.RO_Remark__c;
        ac.RO_Type__pc=Stud.RO_Type__c;
        ac.Safra_Card_Expiry__pc=Stud.Safra_Card_Expiry__c;
        ac.Safra_Card_Holding__pc=Stud.Safra_Card_Holding__c;
        ac.Safra_Remark__pc=Stud.Safra_Remark__c;
        ac.Salesforce_Id_from_Student_Object__pc=Stud.ID;
        ac.School__pc=Stud.School__c;
        ac.Science__pc=Stud.Science__c;
        ac.Secondary_Stream__pc=Stud.Secondary_Stream__c;
        ac.Start_Date__pc=Stud.Start_Date__c;
        ac.Street__pc=Stud.Street__c;
        ac.Student_Id_from_Student_Prospects__pc=Stud.name;
        ac.Student_Old_Reference_ID__pc=Stud.Student_Old_Reference_ID__c;
        ac.Student_Status__pc=Stud.Student_Status__c;
        ac.Student_Type__pc='Student';
        ac.Tick_to_activate_inter_branch_discount__pc=Stud.Tick_to_activate_inter_branch_discount__c;
        ac.Type_Course_Fee__pc=Stud.Type_Course_Fee__c;
        ac.Unit_Number__pc=Stud.Unit_Number__c;
        acc.add(ac);
     }
   if(acc.size>0){
   upsert acc;
}
}

 
AvaneeshAvaneesh
Hi


Remove these line of code 
if(trigger.isUpdate){
 
   for(account acc:[select id, nric__pc, student_Id_from_student_Prospects__pc from account where student_Id_from_student_Prospects__pc IN : trigger.new]){
       m.put(student_Id_from_student_Prospects__pc , acc.id);   
}
}


replace with this code and this will work I am sure ...
 
Set<id> allnewRecordIdset = new Set<id>();

for(Student__c st :trigger.new){
  allnewRecordIdset.add(st.id);
}

  if(trigger.isUpdate){
 
   for(account acc:[select id, nric__pc, student_Id_from_student_Prospects__pc from account where student_Id_from_student_Prospects__pc IN : allnewRecordIdset]){
       m.put(student_Id_from_student_Prospects__pc , acc.id);   
}
}



and let me know one thing  student_Id_from_student_Prospects__pc  this field has related student id or not please confirm it.
if still any problem then let me know and if this was helpful mark as best answer.

Thank you
Avaneesh Singh
 
mw6mw6
Now I have an error as below

Error: Compile Error: Variable does not exist: student_Id_from_student_Prospects__pc at line 17 column 17
On Student__c the field is 'Name' and this auto number is what I am storing in the student_Id_from_student_Prospects__pc  .I also store ID field from Student__c into Salesforce_Id_from_Student_Object__pc
 
AvaneeshAvaneesh
Hi 

A simple mistake I didn't see that 
m.put(acc.student_Id_from_student_Prospects__pc , acc.id);
Let me know more 
AvaneeshAvaneesh
Here I found one more thing u said that Salesforce_Id_from_Student_Object__pc this field is in account and in that field you have Student__c id
if this is true then you have to make one more change

 Replace
"student_Id_from_student_Prospects__pc " this filed with "Salesforce_Id_from_Student_Object__pc"
because i need id not autonumber field so in Query and map replace with this 
 

 I hope now your problem is solved if still then let me know more  
mw6mw6
Hi Avanesh,

Thanks for all your help,  I still have some error after the suggested changes, attached my trigger. please advice
The error is "Error: Compile Error: Variable does not exist: Salesforce_Id_from_Student_Object__c at line 12 column 11"

trigger CreatePersonAccountfromStudent on Student__c (after insert,after update)
{
    List<account> acc=new List<account>();
    Map<Id, Id> m = new Map<Id, Id>();
    RecordType rt = [select id from RecordType where SobjectType='Account' and Name='Student' Limit 1];
Set<id> allnewRecordIdset = new Set<id>();
for(Student__c st :trigger.new){
  allnewRecordIdset.add(st.id);
}
  if(trigger.isUpdate){
   for(account acc:[select id,nric__pc,student_Id_from_student_Prospects__pc,Salesforce_Id_from_Student_Object__pc from account where Salesforce_Id_from_Student_Object__pc IN : allnewRecordIdset]){
    m.put(Salesforce_Id_from_Student_Object__c, acc.id);   
}
}
    for(student__c stud:Trigger.New){  
      account ac = new account();
        if(trigger.isupdate){ // add this line here
      ac.id = m.get(st.id).id;
      }
        ac.Autocreated__pc=True;
        ac.Block__pc=Stud.Block__c;
        ac.Bulk_Load__pc=Stud.Bulk_Load__c;
        ac.Current_Acedamic_Year__pc=Stud.Current_Acedamic_Year__c;
        ac.Date_of_Collection__pc=Stud.Date_of_Collection__c;
        ac.Discount_Percentage__pc=Stud.Discount_Percentage__c;
        ac.Discount_Policy__pc=Stud.Discount_Policy__c;
        ac.Discount_Remarks__pc=Stud.Discount_Remarks__c;
        ac.Discount_Type__pc=Stud.Discount_Type__c;
        ac.English__pc=Stud.English__c;
        ac.Event_Name__pc=Stud.Event_Name__c;
        ac.Exclude_Registration__pc=Stud.Exclude_Registration__c;
        ac.Father_Email__pc=Stud.Father_Email__c;
        ac.Father_Name__pc=Stud.Father_Name__c;
        ac.Father_NRIC__pc=Stud.Father_NRIC__c;
        ac.Father_Office_Phone__pc=Stud.Father_Office_Phone__c;
        ac.Father_Primary_Contact__pc=Stud.Father_Primary_Contact__c;
        ac.Father_Remark__pc=Stud.Father_Remark__c;
        ac.Father_Staff__pc=Stud.Father_Staff__c;
        ac.Gender__pc=Stud.Gender__c;
        ac.GEP__pc=Stud.GEP__c;
        ac.Guardian_Email__pc=Stud.Guardian_Email__c;
        ac.Guardian_Mobile__pc=Stud.Guardian_Mobile__c;
        ac.Guardian_Name__pc=Stud.Guardian_Name__c;
        ac.Guardian_NRIC__pc=Stud.Guardian_NRIC__c;
        ac.Guardian_Office_Phone__pc=Stud.Guardian_Office_Phone__c;
        ac.Guardian_Primary_Contact__pc=Stud.Guardian_Primary_Contact__c;
        ac.Guardian_Relationship__pc=Stud.Guardian_Relationship__c;
        ac.Guardian_Remarks__pc=Stud.Guardian_Remark__c;
        ac.Guardian_Staff__pc=Stud.Guardian_Staff__c;
        ac.How_did_you_hear_about_us__pc=Stud.How_did_you_hear_about_us__c;
        ac.Is_Mind_Stretcher_Staff_Mother__pc=Stud.Is_Mind_Stretcher_Staff_Mother__c;
        ac.LastName=Stud.name__c;
        ac.Level__pc=Stud.Level__c;
        ac.Level_Promoted_DateTime__pc=Stud.Level_Promoted_DateTime__c;
        ac.Maths__pc=Stud.Maths__c;
        ac.Mobile_No_Father__pc=Stud.Mobile_No_Father__c;
        ac.Mother_Email__pc=Stud.Mother_Email__c;
        ac.Mother_Name__pc=Stud.Mother_Name__c;
        ac.Mother_No_Mobile__pc=Stud.Mobile_No_Mother__c;
        ac.Mother_NRIC__pc=Stud.Mother_NRIC__c;
        ac.Mother_Office_Phone__pc=Stud.Mother_Office_Phone__c;
        ac.Mother_Primary_Contact__pc=Stud.Mother_Primary_Contact__c;
        ac.Mother_Remark__pc=Stud.Mother_Remark__c;
        ac.Mother_Staff__pc=Stud.Mother_Staff__c;
        ac.MS_Start_Kit_Bag__pc=Stud.MS_Start_Kit_Bag__c;
        ac.Nationality__pc=Stud.Nationality__c;
        ac.NRIC__pc=Stud.NRIC__c;
        ac.NTUC_10_Discount__pc=Stud.NTUC_10_Discount__c;
        ac.NTUC_Card_Expiry__pc=Stud.NTUC_Card_Expiry__c;
        ac.NTUC_Remark__pc=Stud.NTUC_Remark__c;
        ac.Others__pc=Stud.Others__c;
        ac.Others_How_did_you_hear_about_us__pc=Stud.Others_How_did_you_hear_about_us__c;
        ac.Person_Contact__pc=Stud.Person_Contact__c;
        ac.PersonBirthdate=Stud.DOB__c;
        ac.PersonHomePhone=Stud.Home_No__c;
        ac.PersonMailingPostalCode=Stud.Postal_Code__c;
        ac.PersonMailingStreet=Stud.Street_Temp__C;
        ac.Primary_Contact_Number__pc=Stud.Contact_number__c;
        ac.Primary_Email__pc=Stud.Primary_Email__c;
        ac.Primary_Name__pc=Stud.Primary_Name__c;
        ac.Primary_Remark__pc=Stud.Primary_Remark__c;
        ac.Promotion__pc=Stud.Promotion__c;
        ac.Reading_Oceans_Email__pc=Stud.Reading_Oceans_Email__c;
        ac.Reading_Oceans_End_Date__pc=Stud.Reading_Oceans_End_Date__c;
        ac.Reading_Oceans_Start_Date__pc=Stud.Reading_Oceans_Start_Date__c;
        ac.Reading_Oceans_Subscrip__pc=Stud.Reading_Oceans_Subscrip__c;
        ac.recordtypeid=rt.id;
        ac.Registration_Centre__pc=Stud.Registration_Centre__c;
        ac.Related_Centres__pc=Stud.Related_Centres__c;
        ac.Remarks__pc=Stud.Remarks__c;
        ac.RO_Date_Send_User__pc=Stud.RO_Date_Send_User__c;
        ac.RO_Remark__pc=Stud.RO_Remark__c;
        ac.RO_Type__pc=Stud.RO_Type__c;
        ac.Safra_Card_Expiry__pc=Stud.Safra_Card_Expiry__c;
        ac.Safra_Card_Holding__pc=Stud.Safra_Card_Holding__c;
        ac.Safra_Remark__pc=Stud.Safra_Remark__c;
        ac.Salesforce_Id_from_Student_Object__pc=Stud.ID;
        ac.School__pc=Stud.School__c;
        ac.Science__pc=Stud.Science__c;
        ac.Secondary_Stream__pc=Stud.Secondary_Stream__c;
        ac.Start_Date__pc=Stud.Start_Date__c;
        ac.Street__pc=Stud.Street__c;
        ac.Student_Id_from_Student_Prospects__pc=Stud.name;
        ac.Student_Old_Reference_ID__pc=Stud.Student_Old_Reference_ID__c;
        ac.Student_Status__pc=Stud.Student_Status__c;
        ac.Student_Type__pc='Student';
        ac.Tick_to_activate_inter_branch_discount__pc=Stud.Tick_to_activate_inter_branch_discount__c;
        ac.Type_Course_Fee__pc=Stud.Type_Course_Fee__c;
        ac.Unit_Number__pc=Stud.Unit_Number__c;
        acc.add(ac);
     }
   if(acc.size>0){
   upsert acc;
}
}


 
mw6mw6
User-added image
AvaneeshAvaneesh
Hi
//m.put(Salesforce_Id_from_Student_Object__c, acc.id);   this is your line no 12
and your are getting this error because using iteration of for loop you do not put the variable name replace your code

m.put(acc.Salesforce_Id_from_Student_Object__c, acc.id);
if still, any problem let me know else mark solved 

Thank you
Avaneesh Singh

 
mw6mw6
Hi Avaneesh,

Sorry, your instructions are not clear to me, I am relatively new for this, try to learn, I tried to follow your step by step instruction, but still I encounter the error "Error: Compile Error: Variable does not exist: Salesforce_Id_from_Student_Object__c at line 12 column 11""
AvaneeshAvaneesh
Hii

I change your code put this code in your org and now it will work and i comment in that line where i make you change for you a small change acc.Salesforce_Id_from_Student_Object__c​
trigger CreatePersonAccountfromStudent on Student__c (after insert,after update)
{
    List<account> acc=new List<account>();
    Map<Id, Id> m = new Map<Id, Id>();
    RecordType rt = [select id from RecordType where SobjectType='Account' and Name='Student' Limit 1];
Set<id> allnewRecordIdset = new Set<id>();
for(Student__c st :trigger.new){
  allnewRecordIdset.add(st.id);
}
  if(trigger.isUpdate){
   for(account acc:[select id,nric__pc,student_Id_from_student_Prospects__pc,Salesforce_Id_from_Student_Object__pc from account where Salesforce_Id_from_Student_Object__pc IN : allnewRecordIdset]){
    m.put(acc.Salesforce_Id_from_Student_Object__c, acc.id);  // this line i told you to change  
}
}
    for(student__c stud:Trigger.New){  
      account ac = new account();
        if(trigger.isupdate){ // add this line here
      ac.id = m.get(st.id).id;
      }
        ac.Autocreated__pc=True;
        ac.Block__pc=Stud.Block__c;
        ac.Bulk_Load__pc=Stud.Bulk_Load__c;
        ac.Current_Acedamic_Year__pc=Stud.Current_Acedamic_Year__c;
        ac.Date_of_Collection__pc=Stud.Date_of_Collection__c;
        ac.Discount_Percentage__pc=Stud.Discount_Percentage__c;
        ac.Discount_Policy__pc=Stud.Discount_Policy__c;
        ac.Discount_Remarks__pc=Stud.Discount_Remarks__c;
        ac.Discount_Type__pc=Stud.Discount_Type__c;
        ac.English__pc=Stud.English__c;
        ac.Event_Name__pc=Stud.Event_Name__c;
        ac.Exclude_Registration__pc=Stud.Exclude_Registration__c;
        ac.Father_Email__pc=Stud.Father_Email__c;
        ac.Father_Name__pc=Stud.Father_Name__c;
        ac.Father_NRIC__pc=Stud.Father_NRIC__c;
        ac.Father_Office_Phone__pc=Stud.Father_Office_Phone__c;
        ac.Father_Primary_Contact__pc=Stud.Father_Primary_Contact__c;
        ac.Father_Remark__pc=Stud.Father_Remark__c;
        ac.Father_Staff__pc=Stud.Father_Staff__c;
        ac.Gender__pc=Stud.Gender__c;
        ac.GEP__pc=Stud.GEP__c;
        ac.Guardian_Email__pc=Stud.Guardian_Email__c;
        ac.Guardian_Mobile__pc=Stud.Guardian_Mobile__c;
        ac.Guardian_Name__pc=Stud.Guardian_Name__c;
        ac.Guardian_NRIC__pc=Stud.Guardian_NRIC__c;
        ac.Guardian_Office_Phone__pc=Stud.Guardian_Office_Phone__c;
        ac.Guardian_Primary_Contact__pc=Stud.Guardian_Primary_Contact__c;
        ac.Guardian_Relationship__pc=Stud.Guardian_Relationship__c;
        ac.Guardian_Remarks__pc=Stud.Guardian_Remark__c;
        ac.Guardian_Staff__pc=Stud.Guardian_Staff__c;
        ac.How_did_you_hear_about_us__pc=Stud.How_did_you_hear_about_us__c;
        ac.Is_Mind_Stretcher_Staff_Mother__pc=Stud.Is_Mind_Stretcher_Staff_Mother__c;
        ac.LastName=Stud.name__c;
        ac.Level__pc=Stud.Level__c;
        ac.Level_Promoted_DateTime__pc=Stud.Level_Promoted_DateTime__c;
        ac.Maths__pc=Stud.Maths__c;
        ac.Mobile_No_Father__pc=Stud.Mobile_No_Father__c;
        ac.Mother_Email__pc=Stud.Mother_Email__c;
        ac.Mother_Name__pc=Stud.Mother_Name__c;
        ac.Mother_No_Mobile__pc=Stud.Mobile_No_Mother__c;
        ac.Mother_NRIC__pc=Stud.Mother_NRIC__c;
        ac.Mother_Office_Phone__pc=Stud.Mother_Office_Phone__c;
        ac.Mother_Primary_Contact__pc=Stud.Mother_Primary_Contact__c;
        ac.Mother_Remark__pc=Stud.Mother_Remark__c;
        ac.Mother_Staff__pc=Stud.Mother_Staff__c;
        ac.MS_Start_Kit_Bag__pc=Stud.MS_Start_Kit_Bag__c;
        ac.Nationality__pc=Stud.Nationality__c;
        ac.NRIC__pc=Stud.NRIC__c;
        ac.NTUC_10_Discount__pc=Stud.NTUC_10_Discount__c;
        ac.NTUC_Card_Expiry__pc=Stud.NTUC_Card_Expiry__c;
        ac.NTUC_Remark__pc=Stud.NTUC_Remark__c;
        ac.Others__pc=Stud.Others__c;
        ac.Others_How_did_you_hear_about_us__pc=Stud.Others_How_did_you_hear_about_us__c;
        ac.Person_Contact__pc=Stud.Person_Contact__c;
        ac.PersonBirthdate=Stud.DOB__c;
        ac.PersonHomePhone=Stud.Home_No__c;
        ac.PersonMailingPostalCode=Stud.Postal_Code__c;
        ac.PersonMailingStreet=Stud.Street_Temp__C;
        ac.Primary_Contact_Number__pc=Stud.Contact_number__c;
        ac.Primary_Email__pc=Stud.Primary_Email__c;
        ac.Primary_Name__pc=Stud.Primary_Name__c;
        ac.Primary_Remark__pc=Stud.Primary_Remark__c;
        ac.Promotion__pc=Stud.Promotion__c;
        ac.Reading_Oceans_Email__pc=Stud.Reading_Oceans_Email__c;
        ac.Reading_Oceans_End_Date__pc=Stud.Reading_Oceans_End_Date__c;
        ac.Reading_Oceans_Start_Date__pc=Stud.Reading_Oceans_Start_Date__c;
        ac.Reading_Oceans_Subscrip__pc=Stud.Reading_Oceans_Subscrip__c;
        ac.recordtypeid=rt.id;
        ac.Registration_Centre__pc=Stud.Registration_Centre__c;
        ac.Related_Centres__pc=Stud.Related_Centres__c;
        ac.Remarks__pc=Stud.Remarks__c;
        ac.RO_Date_Send_User__pc=Stud.RO_Date_Send_User__c;
        ac.RO_Remark__pc=Stud.RO_Remark__c;
        ac.RO_Type__pc=Stud.RO_Type__c;
        ac.Safra_Card_Expiry__pc=Stud.Safra_Card_Expiry__c;
        ac.Safra_Card_Holding__pc=Stud.Safra_Card_Holding__c;
        ac.Safra_Remark__pc=Stud.Safra_Remark__c;
        ac.Salesforce_Id_from_Student_Object__pc=Stud.ID;
        ac.School__pc=Stud.School__c;
        ac.Science__pc=Stud.Science__c;
        ac.Secondary_Stream__pc=Stud.Secondary_Stream__c;
        ac.Start_Date__pc=Stud.Start_Date__c;
        ac.Street__pc=Stud.Street__c;
        ac.Student_Id_from_Student_Prospects__pc=Stud.name;
        ac.Student_Old_Reference_ID__pc=Stud.Student_Old_Reference_ID__c;
        ac.Student_Status__pc=Stud.Student_Status__c;
        ac.Student_Type__pc='Student';
        ac.Tick_to_activate_inter_branch_discount__pc=Stud.Tick_to_activate_inter_branch_discount__c;
        ac.Type_Course_Fee__pc=Stud.Type_Course_Fee__c;
        ac.Unit_Number__pc=Stud.Unit_Number__c;
        acc.add(ac);
     }
   if(acc.size>0){
   upsert acc;
}
}


 
darron clarisondarron clarison
here's the modified trigger to include 'after update' functionality:
apexCopy code
trigger CreatePersonAccountfromStudent on Student__c (after insert, after update) { List<Account> acc = new List<Account>(); Map<Id, String> m = new Map<Id, String>(); RecordType rt = [SELECT Id FROM RecordType WHERE SobjectType='Account' AND Name='Student' LIMIT 1]; for(Student__c stud : Trigger.new) { // Rest of your code to populate the account fields goes here... } insert acc; // Add this if needed for 'after update' }
Make sure to complete the logic inside the loop based on your requirements for both 'after insert' and 'after update' triggers https://allaboutrecliners.com/