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
sdfasdsdfasd 

how to update the records using trigger.

i create the trigger for convert the records from account obejct to contact object. condition is

 if(a1.Group_Admin_Name__c != null ){

}

 

1.account object  contain Group_Admin_Name__c field. once fils the records in account object that time fills the value in GroupAdminName field. then converted records from account to contact. but records displayed in contact object two times.this is the problem

2.i created update trigger also.once edit the account record then fills the GroupAdminName click on save buttn records will be converted to contact object.this one working properly.

 

Trigger:

======

 

trigger ConvertQualifiedCasetoContact on Account (after insert, after update) {
    
   if(Trigger.isInsert){
      List<Account> acc = new List<Account>();
      List<Contact> con = new List<Contact>();
      List<ID> AID = new List<ID>();
   
   for(Account ac:Trigger.New){
       acc.add(ac);
       AID.add(ac.id);
   }
   for(Account a:acc) {
       Contact c = new Contact();
       if(a.Group_Admin_Name__c != null){
          c.LastName = a.Group_Admin_Name__c;
          c.Email = a.Group_Admin_Email__c;
          c.Phone = a.Group_Admin_Phone__c;
          c.AccountId = a.ID;
          con.add(c);
         }
       }
       insert con;
   }
   
    if(Trigger.isUpdate){
       List<Account> acc = new List<Account>();
       List<Contact> con1 = new List<Contact>();
       set<ID> aid = new set<ID>();
       for(Account ac:Trigger.old){
           acc.add(ac);
           aid.add(ac.id);
       }
       
     for(Account a:acc){
         Account a1 = [Select id,Group_Admin_Name__c,Group_Admin_Email__c,Group_Admin_Phone__c   from Account where id in :aid];
         Contact c = new Contact();
         if(a1.Group_Admin_Name__c != null ){
            c.LastName = a1.Group_Admin_Name__c;
            c.Email = a1.Group_Admin_Email__c;
            c.Phone = a1.Group_Admin_Phone__c;
            c.AccountId = a.ID;
            con1.add(c);
           }
        }
        update con1;
    }
 }

 

pls help me.........

Best Answer chosen by Admin (Salesforce Developers) 
yvk431yvk431

i guess you should get an dml error here , because you are trying to update contacts that never existed.

 

The idea of updating contacts is right but you have to fetch the contacts using the AccountId, incase if htere is no contact then we have to insert. so we have to go for upsert here .

 

 

--yvk