• baseballkyle
  • NEWBIE
  • 15 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 7
    Replies
I have a batch apex class that runs on my custom object successfully - but my trigger doesn't seem to fire. If i edit/save the record manually, trigger fires just fine. What am i missing here? Why doesn't the trigger fire when batch apex runs? 

has anyone come across (or know how to build) a trigger for counting the # of attachments on an opportunity?

 

I did some research and found one post (http://goo.gl/jZlGC) - but that trigger does not work unfortunately....

 

Here is the trigger. Any ideas on what needs to be done to resolve?

 

It casuses this error:

Error: Apex trigger OpportunityAttachments caused an unexpected exception, contact your administrator: OpportunityAttachments: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.OpportunityAttachments: line 52, column 1


 

trigger OpportunityAttachments on Attachment(after insert)

{

 List<Opportunity> oppListToBeUpdated = new List<Opportunity>();

 Map<ID,Integer> attachmentOppList = new Map<ID,Integer>();



 for(Attachment a: Trigger.new)

 {

 String parentIdval = a.parentid;

 if(parentIdval.startswith('006'))

 {

 if(attachmentOppList.get(a.parentid) == null)

 attachmentOppLIst.put(a.parentid, 0);

 else

 {

 Integer i = attachmentOppLIst.get(a.parentid);

 attachmentOppLIst.put(a.parentid, (i+1)); 

 }

 }

 }

 

 if(attachmentOppLIst.size() > 0)

 {

oppListToBeUpdated = [select id, Attachment_Count__c from Opportunity where id in :attachmentOppLIst.keyset()];

 for(Opportunity o: oppListToBeUpdated)
 {
   if(o.Attachment_Count__c != null)
    o.Attachment_Count__c = (Integer)attachmentOppLIst.get(o.id);
  else
  o.Attachment_Count__c = o.Attachment_Count__c + (Integer)attachmentOppLIst.get(o.id);//assuming that Attachment_Count__c is a numeric field.if not convert it to integer and add the value
 }
 }


 

 if(oppListToBeUpdated.size() > 0) 

 update oppListToBeUpdated;



}
I have a batch apex class that runs on my custom object successfully - but my trigger doesn't seem to fire. If i edit/save the record manually, trigger fires just fine. What am i missing here? Why doesn't the trigger fire when batch apex runs? 

has anyone come across (or know how to build) a trigger for counting the # of attachments on an opportunity?

 

I did some research and found one post (http://goo.gl/jZlGC) - but that trigger does not work unfortunately....

 

Here is the trigger. Any ideas on what needs to be done to resolve?

 

It casuses this error:

Error: Apex trigger OpportunityAttachments caused an unexpected exception, contact your administrator: OpportunityAttachments: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.OpportunityAttachments: line 52, column 1


 

trigger OpportunityAttachments on Attachment(after insert)

{

 List<Opportunity> oppListToBeUpdated = new List<Opportunity>();

 Map<ID,Integer> attachmentOppList = new Map<ID,Integer>();



 for(Attachment a: Trigger.new)

 {

 String parentIdval = a.parentid;

 if(parentIdval.startswith('006'))

 {

 if(attachmentOppList.get(a.parentid) == null)

 attachmentOppLIst.put(a.parentid, 0);

 else

 {

 Integer i = attachmentOppLIst.get(a.parentid);

 attachmentOppLIst.put(a.parentid, (i+1)); 

 }

 }

 }

 

 if(attachmentOppLIst.size() > 0)

 {

oppListToBeUpdated = [select id, Attachment_Count__c from Opportunity where id in :attachmentOppLIst.keyset()];

 for(Opportunity o: oppListToBeUpdated)
 {
   if(o.Attachment_Count__c != null)
    o.Attachment_Count__c = (Integer)attachmentOppLIst.get(o.id);
  else
  o.Attachment_Count__c = o.Attachment_Count__c + (Integer)attachmentOppLIst.get(o.id);//assuming that Attachment_Count__c is a numeric field.if not convert it to integer and add the value
 }
 }


 

 if(oppListToBeUpdated.size() > 0) 

 update oppListToBeUpdated;



}