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
Sfdc@SmithaSfdc@Smitha 

urgent requirement on trigger pls help me

Hi

 can anybody help me?

  I have the following requirement. Please write a trigger for it

I have two objects Leave_Application__c and Leave__c 

Leave__c has Master Detail 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 . Newly created record will have one field that is Leave_Application_No__c and that should be set to the trigger ID.

Best Answer chosen by Admin (Salesforce Developers) 
SFAdmin5SFAdmin5

this should work fine.  let me know if you need it tweaked for any fields

 

 

trigger insertNewLeave on Leave_Application__c (after insert,after update)
{
    List<Leave__c> leaveList = new List<Leave__c>();
        for(Leave_Application__c la : Trigger.new)
            if(la.Status__c == 'Approved')
        {
            Leave__c l= new Leave__c();
            l.Leave_Application__c = la.Id;
            leaveList.add(l);
        }
        insert leaveList;
}

All Answers

SFAdmin5SFAdmin5

this should work fine.  let me know if you need it tweaked for any fields

 

 

trigger insertNewLeave on Leave_Application__c (after insert,after update)
{
    List<Leave__c> leaveList = new List<Leave__c>();
        for(Leave_Application__c la : Trigger.new)
            if(la.Status__c == 'Approved')
        {
            Leave__c l= new Leave__c();
            l.Leave_Application__c = la.Id;
            leaveList.add(l);
        }
        insert leaveList;
}

This was selected as the best answer
SFFSFF

Minor change so that only one Leave__c record gets created:

 

trigger insertNewLeave on Leave_Application__c (after insert, after update)
{
   list<Leave__c> leaveList = new list<Leave__c>();
   for (Leave_Application__c la : trigger.new)
   {
      if (la.Status__c == 'Approved' &&
          trigger.oldMap.get(la.Id).Status__c != 'Approved')
      {
         Leave__c l = new Leave__c();
         l.Leave_Application__c = la.Id;
         leaveList.add(l);
      }
   }
   insert leaveList;
}

 

Sfdc@SmithaSfdc@Smitha

Thanku for reply but here  what iam having doubt is the two objects having master detailed relationship so in code we represent the two objets like __c .But for  child object we mention like__r here is  there  any  problm of  with out mention th__r pls breifly explain?

SFAdmin5SFAdmin5

shouldn't matter if it's lookup or md.  the triggers above work