+ Start a Discussion
SapanaSapana 

trigger on update

Hi

 

I have two objects A and B 

B has MasterDetail relationship with A

how can I  write a trigger to insert a record In object B on update of field in a record of  object A

Best Answer chosen by Admin (Salesforce Developers) 
asish1989asish1989

Hi Sapan

    Try this....

 

    

trigger updateDocDetails on Leave_Application__c (after update)

{    

       if(trigger.new.size() > 1) return;

 

           for (Leave_Application__c leaveapp:Trigger.new){      

                    if(trigger.new[0].Status__c =='Approved')    

                      {        

                                   Leave__c lv =  new Leave__c();     

                                   lv.Name = 'xyz';          

                                   lv.Leave_Application_No__c = trigger.new[0].Id;          

                                   insert lv;                          

                     }  

 

 

             }

 }

 

DId this post solve your problem If so please mark It solved so that others get benified.....

 

Thanks

asish

All Answers

asish1989asish1989

Hi Sapana

    It would be more usiful If you could have told more about your requirement...

   I am just giving trigger format... try this

   

  

trigger updateA on A(after update)
{
Map<Id, A> mapA = new Map<Id, A>();
for (A objA: Trigger.new)
{

mapA .put(objA.AId__c,objA);
}

if(mapA .size() > 0)
{

  B newobj = new B ();

--------------

------

}

}

 

Did this solve your problem.. If so please mark it solved

 

 

Thanks 

asish

SapanaSapana

hi asish,

 

I suppose explaining my requirement would clear the picture

 

I have two objects Leave_Application__c and Leave__c 

Leave__c has MasterDetail relationship with Leave_Application__c

there is a picklist  field in Leave_Application__c object named Status__c

Now whenever a Status__c field is updated to the value "Approved"  a record should be created in Leave__c object  

asish1989asish1989

Hi Sapan

    Try this....

 

    

trigger updateDocDetails on Leave_Application__c (after update)

{    

       if(trigger.new.size() > 1) return;

 

           for (Leave_Application__c leaveapp:Trigger.new){      

                    if(trigger.new[0].Status__c =='Approved')    

                      {        

                                   Leave__c lv =  new Leave__c();     

                                   lv.Name = 'xyz';          

                                   lv.Leave_Application_No__c = trigger.new[0].Id;          

                                   insert lv;                          

                     }  

 

 

             }

 }

 

DId this post solve your problem If so please mark It solved so that others get benified.....

 

Thanks

asish

This was selected as the best answer
SapanaSapana

Thanks asish