+ Start a Discussion
Tala PinedaTala Pineda 

Can anyone help me create a trigger on the attachment for Orders?

We want to be able to validate Orders (Purchase Orders) created by our users by attaching the PO form in the Order record. However, we can configure a rule or build process to require attachment on Orders. We really need your help. I've read online and most said we can do this using APEX Triggers. I am unsure how to write triggers in Salesforce. Please help.
Waqar Hussain SFWaqar Hussain SF
Hi Tala, 

Salesforce deos not support attachment as a lookup field. So as per your requirement, we can not do this via trigger.

Your solution could be by creating a custom VF page for creating Purchase order, from where user will first enter purchase order information and then user will add attachments as many as want and then will save purchase order by clicking save button. On that page you can validate that user must add at least one attachment in order to save an order. 


Anyway If you have any custom object as an order attachment then you can write trigger as below.

I assume purchase order object api name is Purchase_Order__c and there is a field on PO for attachment which api name is PO_attachment__c.
 
Setup > Create > Object


then click on purchase order object name, on the trigger related list, click on create trigger button and write trigger as below.
 
trigger POTrigger on Purchase_Order__c(before insert){
    for(Purchase_Order__c po : trigger.new){
if(po.PO_attachment__c == null){
PO_attachment__c.addError('You must select PO attachment. ');
}
}
}

Let me know If you have any confusion.

Best,
Waqar Hussain
2011waqar@gmail.com
Skype: waqar.hussain991
seff seffseff seff
hi Waqar
can you pls help me in doing for writing a trigger on a given scenario, I need help with the following points which are in bold font. 

Apex Trigger
Use Case
ABC Firm needs to see total expenditure on running campaigns against each Account in their Organization. This would be one of the key parameters to measure investment vs returns.
Requirement Details
Create a custom number field on Account and name it
Total Campaign Cost
 Create a lookup relationship on Campaign as Account.
 Use Campaign Actual Cost field for operations.
Each time a campaign status is marked as Completed add the amount into Total Campaign Cost field of account.
Update Account Status:
 If Total Campaign Cost on Account is greater than $
10,000 : Platinum
If Total Campaign Cost on Account is > $8,000 &<$10,000 : Gold
If Total Campaign Cost on Account is <$8,000: Silver
Code should be able to handle bulk data; meaning multiple campaigns can be added/updated with status as Completed using Import Wizard or Dataloader
Create test class to ensure developed code can be deployed; adhere to
best practices of
testing framework
Waqar Hussain SFWaqar Hussain SF
Hi @self,

Sorry for late respones. Just looked your statement now.

Yes I can help you. You will have to write trigger. You can contact me on Skype waqar.hussain991

 
Waqar Hussain SFWaqar Hussain SF
Hi @Tala, 

Did my answer helpd you. Please dont forget to mark best answer If answer helped you.
seff seffseff seff
Hi@waqar
Thanks for your help
akshay salunke 3akshay salunke 3
seff seff 
sir send me the trigger code