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
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
Fenil Mehta 13Fenil Mehta 13
Hi All,
I have one scenario in which i need your help.

Ther is two standard field on case : Case origin and Subject
When one of the field or both are updated, both field value(concating 2 filed values) should be auto populate in one custom field using trigger for the same object Case