+ Start a Discussion
RayAllenRayAllen 

Attachment to an Account changes a field in the Account upon attachment - How to?

Hi,

 

Our users will be attaching documents to accounts periodically.  Upon attachment, I need a field ('review_needed' -  a checkbox) to be changed to either checked or unchecked (doesn't matter, just needs to be modified). 

 

How can I achieve this using a trigger?  Is it possible to change the field using a workflow upon attachment (I'm guessing no).

 

Thanks in advance for any help you can provide!!!

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Did you copied it correctly, It should not show any such compiler error.

 

trigger UpdateAccReviewNeeded on Attachment (after insert) 
{

    String accKey  = '001';
    Set<ID> setAccountId = new Set<ID>();
    
    for(Attachment  a : trigger.new)
        {
            String strParentId = String.valueOf(a.parentid).substring(0 , 3);
            if(strParentId == accKey)
                {
                    setAccountId.add(a.parentid);
                }
        }
    //if this condition is true that means an attachement is associated with account so you have to update the chcek box field

    if(setAccountId.size() > 0)
      {
          //review_needed__c  should be the API Name of your check box field
          List<Account> listAcc = [Select review_needed__c from Account where id in: setAccountId];
          for(Account a : listAcc)
              {
                 a.review_needed__c = (!a.review_needed__c);
              }
          update listAcc;
      }
}


 Copy it, please tell me where are you creating this trigger, it should be in attachement.

All Answers

bob_buzzardbob_buzzard

You can create a trigger for the Attachment object (I believe you'll have to do this via the Force.com IDE rather than the UI - though might have changed in the last release).  This will have the id of the account that the attachment has been uploaded to in the ParentId field.  Using this id, you can retrieve the account and apply the update.

 

Note that this would be invoked for all attachments, regardless of what sobject type they were attached to, so you may need to check the three character prefix of the id to check that it is indeed an account before retrieving and processing the parent.

RayAllenRayAllen

I was hoping someone could give me an example of the code I would need to write.  I'm very new to this and have really no idea where to start.

 

Thanks again.

Shashikant SharmaShashikant Sharma

You can use this code 

 

trigger UpdateAccReviewNeeded on Attachment (after insert) {

    String accKey  = '001';
    Set<ID> setAccountId = new Set<ID>();
    
    for(Attachment  a : trigger.new)
        {
            String strParentId = String.valueOf(a.parentid).substring(0 , 3);
            if(strParentId == accKey)
                {
                    setAccountId.add(a.parentid);
                }
        }
    //if this condition is true that means an attachement is associated with account so you have to update the chcek box field

    if(setAccountId.size() > 0)
      {
          //review_needed__c  should be the API Name of your check box field
          List<Account> listAcc = [Select review_needed__c from Account where id in: setAccountId];
          for(Account a : listAcc)
              {
                 a.review_needed__c = (!a.review_needed__c);
              }
          update listAcc;
      }


}

 I hope this will help you.

RayAllenRayAllen

Hi,

 

this is the compile error I am receiveing: "Error: Compile Error: expecting right curly bracket, found 'EOF' at line 0 column -1"

 

Any ideas as to why?

 

Thank you so much for your help!

Shashikant SharmaShashikant Sharma

Did you copied it correctly, It should not show any such compiler error.

 

trigger UpdateAccReviewNeeded on Attachment (after insert) 
{

    String accKey  = '001';
    Set<ID> setAccountId = new Set<ID>();
    
    for(Attachment  a : trigger.new)
        {
            String strParentId = String.valueOf(a.parentid).substring(0 , 3);
            if(strParentId == accKey)
                {
                    setAccountId.add(a.parentid);
                }
        }
    //if this condition is true that means an attachement is associated with account so you have to update the chcek box field

    if(setAccountId.size() > 0)
      {
          //review_needed__c  should be the API Name of your check box field
          List<Account> listAcc = [Select review_needed__c from Account where id in: setAccountId];
          for(Account a : listAcc)
              {
                 a.review_needed__c = (!a.review_needed__c);
              }
          update listAcc;
      }
}


 Copy it, please tell me where are you creating this trigger, it should be in attachement.

This was selected as the best answer
RayAllenRayAllen

Hi Shashikant,

 

I was copying it into 'Customize>Accounts > Triggers'.

 

I'm guessing this is the wrong area?  Where is 'Attachments'?

 

Please excuse my ignorance.

 

Thanks

Shashikant SharmaShashikant Sharma

You will need the Force.com IDE (Eclipse).http://wiki.developerforce.com/index.php/Force.com_IDE

 



There is one hack also for it, Go to any object's say Account's trigger list and click create new. Do not write any thing , In the URL, see the adderess bar you will see a  entity query string parameter (e.g. entity=Account) and change it to Attachment (entity=Attachment) and press Enter. Newl loaded screen will accept Attachment trigger. Now copy my code in it.

Shashikant SharmaShashikant Sharma

If you face problem in implementing hack , i can help you in that. Please let me know if any issue in creating trigger.

RayAllenRayAllen

That worked GREAT!

 

I can't thank you enough!!!

RayAllenRayAllen

How do you deploy these changes to Live?  There is no 'New' button for Triggers in the Live environment.

 

I would only like to deploy this change and no ther work I have done in my test environment.

RayAllenRayAllen

Awesome, great info.

 

I created a Change Set and included the Apex Trigger you created.  Upon a test deployment in live, the validation failed:

UpdateAccReviewNeeded:Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required

Deploy ErrorAverage test coverage across all Apex Classes and Triggers is 0%, at least 75% test coverage is required.

Shashikant SharmaShashikant Sharma

You need to create a Test Method also

 

here is your test class

 

@isTest
private class testMyTrigger 
{

    public static testmethod void testCopyAttachments()
    {
    Account a = new Account(Name ='Test Account' , review_needed__c = false);
    insert a;

   test.startTest();
  Attachment att = new Attachment(Name = 'TestAttachement', ParentId = a.id , Body = Blob.valueOf('Test Attachment'));
   insert att ;
   Account a1 = [ Select review_needed__c from Account id =: a.id];
   system.assertEquals((!a.review_needed__c) , a1.review_needed__c);  
   test.stopTest();
      
   }

}

 Please fill any required fields if there are any in account object. I hope above test class should work for you , let me know if any issues.

RayAllenRayAllen

Hi,

 

where do I put this code and does it fit with the other code you wrote for me in this thread?

 

Thanks again...

Shashikant SharmaShashikant Sharma

Go to Develop -> Apex Classes  ->  Click New -> Copy Paste the code that I provided-> Click Save

 

You will see a class then , click on that class you will see a Run Button click on this , you will see your code coverage of trigger.

RayAllenRayAllen

I received the following compiling error:

 

Error: Compile Error: expecting right square bracket, found '=' at line 13 column 58

 

Thanks again for all your help, I really appreciate it.

bob_buzzardbob_buzzard

This query is slightly malformed:

 

Account a1 = [ Select review_needed__c from Account id =: a.id];

 it should be:

 

Account a1 = [ Select review_needed__c from Account where id =: a.id];

 Note the additional 'where' keyword.

RayAllenRayAllen

Thank you for taking the time to review this Bob!  Worked great!!!