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
Vivek ChaudhariVivek Chaudhari 

How To write Trigger for after insert event? please suggest?

I have one custom SObject Book__c having fields Book_name__c, Author_name__c and Price__c.
Now i want to write a trigger where one author can write only 5 books i.e.
we should be able to insert 5 book record having same author,
when we try to insert 6th book record and having same Author same as above five book then it should not allow...?
Best Answer chosen by Vivek Chaudhari
Akhil AnilAkhil Anil
You can use the below trigger
 
trigger CheckAuthor on Trainingforce__Book__c (after insert) {
   
   List<String> auth = new List<String>();
   Set<String> restricted = new Set<String>();
 
   for(Trainingforce__Book__c bk:Trigger.New) {
      
      auth.add(bk.Trainingforce__Author__c); 
   
   }
      
   AggregateResult[] groupedResults = [SELECT COUNT(Id), Trainingforce__Author__c FROM Trainingforce__Book__c
    where Trainingforce__Author__c IN :auth GROUP BY Trainingforce__Author__c];
    
   for(AggregateResult ar:groupedResults) {
      
       if((INTEGER)ar.get('expr0') > 5) {
         restricted.add((STRING)ar.get('Trainingforce__Author__c'));
       }
   
   }
   
   for(Trainingforce__Book__c bk1:Trigger.New) {
      
      if(restricted.contains(bk1.Trainingforce__Author__c))
      bk1.addError('Author cannot have more than 5 books');
   
   }
      
}

 

All Answers

salesforce mesalesforce me
hi check this once...
trigger CreateEventAfterHandoff on Handoff__c (after insert, after update) {

List<Event> lstNewEvents = new List<Event>();

for (Handoff__c eve : Trigger.new) {

    if (Trigger.isUpdate) {

        if (eve.Status__c != Trigger.oldMap.get(eve.Id).Status__c) { 

        // Field has been changed! 

            if (eve.Status__c == 'Delivered') {

                Event e         = new Event();

                e.Event_Type__c     = eve.Event_Type__c;
                e.Meeting_Type__c   = eve.Meeting_Type__c;
                e.StartDateTime     = eve.Meeting_Time_Start__c;
                e.EndDateTime       = eve.Meeting_Time_Stop__c;
                e.Subject           = eve.Name;
                e.WhoId             = eve.Lead__c;
                e.OwnerId           = eve.User__c;
                e.Handoff_ID__c     = eve.Id;

                lstNewEvents.add(e);

            }           

        }

    } else if (Trigger.isInsert) {

        if (eve.Status__c == 'Delivered') {

            Event e         = new Event();

            e.Event_Type__c     = eve.Event_Type__c;
            e.Meeting_Type__c   = eve.Meeting_Type__c;
            e.StartDateTime     = eve.Meeting_Time_Start__c;
            e.EndDateTime       = eve.Meeting_Time_Stop__c;
            e.Subject           = eve.Name;
            e.WhoId             = eve.Lead__c;
            e.OwnerId           = eve.User__c;
            e.Handoff_ID__c     = eve.Id;

            lstNewEvents.add(e);

        }

    }           

}

insert lstNewEvents;     

}

 
Akhil AnilAkhil Anil
You can use the below trigger
 
trigger CheckAuthor on Trainingforce__Book__c (after insert) {
   
   List<String> auth = new List<String>();
   Set<String> restricted = new Set<String>();
 
   for(Trainingforce__Book__c bk:Trigger.New) {
      
      auth.add(bk.Trainingforce__Author__c); 
   
   }
      
   AggregateResult[] groupedResults = [SELECT COUNT(Id), Trainingforce__Author__c FROM Trainingforce__Book__c
    where Trainingforce__Author__c IN :auth GROUP BY Trainingforce__Author__c];
    
   for(AggregateResult ar:groupedResults) {
      
       if((INTEGER)ar.get('expr0') > 5) {
         restricted.add((STRING)ar.get('Trainingforce__Author__c'));
       }
   
   }
   
   for(Trainingforce__Book__c bk1:Trigger.New) {
      
      if(restricted.contains(bk1.Trainingforce__Author__c))
      bk1.addError('Author cannot have more than 5 books');
   
   }
      
}

 
This was selected as the best answer
Vivek ChaudhariVivek Chaudhari
Thanks a lot...