+ Start a Discussion
KNKKNK 

Trigger to copy field value

Hi Team,

 

I Have a field called price in three objects A,B and C.

 

Now i want to get the price values of object A and B into C.

 

Could you please tell me how to create a trigger for it.

 

vishal@forcevishal@force

Are these three objects related in any way?

KNKKNK

Hi, 

 

Yes, in C object A and B are lookup fields.

 

 

KNKKNK

Hi Vishal,

 

 I have done this thorugh workflow field update. It is working fine with new ones, but once update the the price value for existing records in Object A and B facing few issues, so want to create a trigger for the same.

 

I am new to the triggers and apex concepts..

vishal@forcevishal@force

Why not use formula fields? You simply want to copy the values of A and B on C, correct?

 

If yes, then just create a cross object formula field on C which refers to A.Field and another one referring to B.Field.

 

KNKKNK

Hi Vishal,

 

I am using this price field from C Object  in VF page to calculate total Amount for A Object and B Object.

 

I have a created workflow to calculate this total amount on C object.

 

I am not getting calculated amount if we update the price value for exising records as this workflow in on C object. I need to remove the existing product and need to add again on VF page every time, then only it works.

 

Hope you got my point.

bujjibujji

Hi,

I think you need to write a After Insert,After Update trigger on C object.

trigger C_Field_Update on Cobject(after insert, after update)

{
          for(Cobject c:Trigger.new)

         {
//*********Here get the a_field,b_field values with respect to the Cobject
                if(Trigger.isInsert){                

                    if(C_update_field == null)
                   {                           
                               c.C_update_field = a_field+b_field;
                   }
                insert c;
                }
                if(Trigger.isUpdate){
                    if(C_update_field == null || C_update_field != null)
                   {       
                               c.C_update_field = a_field+b_field;
                   }
                 update c;
                }
         }
}
Thanks,
Bujji

sudha76sudha76

Hi Buji,

 

I need something similiar on the TASK object wherein I am trying to copy the first 255 characters from the "Description" (standard field) to the custom field called "Extract Heading".

 

Can you give me one example on how to do this?

 

thanks.

bujjibujji

Hi,

 

use the above code once you got the description value in the trigger.

//suppose description value is "desc" than

if(Extract Heading == null)

{

String substr = desc.substring(0,255);

Extract Heading = substr;   // here you will get the only string from 0 to 255.

 

}

 

Thanks,

Bujji