+ Start a Discussion
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com 

Please help: Trigger to make an entry in Cust.Obj B when there is a change in feild of cust obj A

Hi,

    I wanna write a trigger .This trigger gets fired when I change a field (a1__C) in custom object A.This trigger should write thje following message

" a1__c has been changed in A".This message is stored in notes feild (B1__c)  of custom object B. Every time there is a change in A , a new entry is made in B. B has only one field i.e B1 which is of text area type.

        Please help me on this it's urgent!!!!

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
IspitaIspita

Hi shrey,

 Hope this example helps you :-

 trigger CaseTgr on CustObj (before update) {

 

 if(trigger.new[0].a1__C!=trigger.old[0].a1__C)

{

ObjB B= new  ObjB();

B.B1__c=" a1__c has been changed in A";

insert B;

}

 

}

 

 

All Answers

IspitaIspita

Hi shrey,

 Hope this example helps you :-

 trigger CaseTgr on CustObj (before update) {

 

 if(trigger.new[0].a1__C!=trigger.old[0].a1__C)

{

ObjB B= new  ObjB();

B.B1__c=" a1__c has been changed in A";

insert B;

}

 

}

 

 

This was selected as the best answer
Ispita_NavatarIspita_Navatar

 

Hi,

 

First of all you have to check old and new value of field of custom object A for before update event, if it is not equal to previous values, you can set a message if condition meet and assign the message to custom object field value for B.

 

You can use

 trigger.new[0].CUSTOMOBJFIELD.adderror('custom object A has been changed in A');

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

UVUV

You can write a workflow if your both objects are releated since trigger is not always a good solution.

https://help.salesforce.com/apex/HTViewHelpDoc?id=workflow_field_update_considerations.htm&language=en