+ Start a Discussion
ch ranjithch ranjith 

how to update same object field using trigger?

trigger realtime2 on Contact (before insert)
  {

     map<id,string> map1=new map<id,string>();
     list<contact> conlist=new list<contact>();
      list<contact> conlist1=new list<contact>();
if(trigger.isbefore)
    {
    for (Contact con:trigger.new)
     {
       map1.put(con.id,con.phone);
       conlist.add(con);
     }
      for(contact con:conlist)
      {        
       con.mobilephone=map1.get(con.id);
       conlist1.add(con);
     }
    
  update conlist1;
   }
  }
Best Answer chosen by ch ranjith
Shyam BhundiaShyam Bhundia
It seems like you are trying to put the phone number from the contact in to the mobile number field?

If so you can do the following:

trigger realtime2 on Contact (before insert){
     if(trigger.isbefore){
          for(Contact con : trigger.new) {
              con.mobilephone =  con.phone;
          }
  }

You dont need to call the update.

Hope this helps

All Answers

Shyam BhundiaShyam Bhundia
It seems like you are trying to put the phone number from the contact in to the mobile number field?

If so you can do the following:

trigger realtime2 on Contact (before insert){
     if(trigger.isbefore){
          for(Contact con : trigger.new) {
              con.mobilephone =  con.phone;
          }
  }

You dont need to call the update.

Hope this helps
This was selected as the best answer
Vikash TiwaryVikash Tiwary
Hi Ranjit,

You don't need to perform dml(i.e update conlist1) in before Insert. Simply You should assign the value. Looking at your code it seems you are trying to populate MobilePhone with Phone field. You can do as such.

trigger realtime2 on Contact (before insert)
{
if(trigger.isbefore)
{
    for(contact con:conlist)
    {       
       con.mobilephone=map1.get(con.id);
    }
}
}

 
Nishad BashaNishad Basha
Hi, Shyam Bhundia

How to update the custom field using trigger in same object?
can you plase give me the example of above scenario.
Shyam BhundiaShyam Bhundia
Hi Nishad,

In what context are you looking for an example?

Cheers
Shyam