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
Kieran HarrisonKieran Harrison 

How to write Apex Trigger for Opportunity?

Hi Guys,

I need help writing an Apex Trigger, please. I currently have a validation rule in place which does not allow the user to move forward in Opportunity stage unless they have ticked a checkbox, however, I want to add to this rule so that they cannot move forward in stage unless they upload a customer declaration PDF under 'Files' as well as ticking the checkbox. 

I have been made aware that this cannot be done using Validation Rules, however, must be done using an Apex Trigger on Opportunity. 

I have taken a stab in the dark at writing the trigger from looking at Youtube videos but admittedly have absolutely no clue what I'm doing;

User-added image

Any assistance is greatly appreciated.
v varaprasadv varaprasad
Hi Kieran,

Please check once below snippet :
 
trigger SetTitleToAttachment on Attachment (after insert) {

set<id> oppIds = new set<id>();

for(Attachment att: Trigger.new){
    if(att.ParentId.startsWithIgnoreCase('006') && Att.name =='customer declaration')
        oppIds.att.ParentId;
}

List<opportunity> oppList=[select Id , fileuploaded__c from opportunity where Id in : oppIds];
 
 for(opportunity opp : oppList){
      opp.fileuploaded__c = True;
  }

  update oppList;

}

Note: I have not tested the above code may you will get some syntactical errors.



Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For  Support: varaprasad4sfdc@gmail.com



 
Kieran HarrisonKieran Harrison
Hi There, 

Thanks so much for your help - Looks like there was an issue though. 

Please see below; 

User-added image

Is there any chance you could please very briefly explain what this trigger is doing? Does it just look at whether something is uploaded to files and then allow the user to move to the next stage?

Perhaps if it is easier the trigger can look at whether a PDF was uploaded using 'Files' and then select the tick box for the field Customer_Declaration_Signed_Uploaded__c which in turn allow the user to move forward?
Raj VakatiRaj Vakati
trigger SetTitleToAttachement on Attachment (after insert) {
    Set<Id>  oppIds= new Set<Id>();
    for(Attachment att : trigger.New){
         if(att.ParentId.getSobjectType() == Opportunity.SobjectType && att.Name=='Customer declaration '){
              oppIds.add(att.ParentId);
         }
    }
    List<Opportunity> oppList= [select id, fileUpload__c from Opportunity where id in : oppIds];
    if(accountList!=null && accountList.size()>0){
        for(Opportunity o: oppList){
            o.fileUpload__c = true;
        }
        update oppList;
    }
}

Use this code
Raj VakatiRaj Vakati
Please change API name as per your API name
Kieran HarrisonKieran Harrison
Hi Raj, 

Do I just need to put in the API name for the Customer Declaration field? I do not have a field called fileUpload__c ?

Thanks,
Ajvad AjuAjvad Aju
Hi Harrison,

 If a pdf is uploaded into the record and then showing a checkbox equals true or checked (checkbox name is fileUploaded), try like this code
trigger CustomerDeclaration on Opportunity (before insert, before update) 
{
    for(Opportunity opp:trigger.new)
    {
     if(opp.fileUploaded==true)
     {
         //write here the logic to allow the user
     }
    }
}

Regards
Ajvad Aju
Kieran HarrisonKieran Harrison
Thanks very much, Ajvad. 

The trigger has saved successfully but I am not sure it will do exactly what I want. 

All I want the trigger to do is when the user uploads the file under 'Files' (shown below in the screenshot) then tick the checkbox for the field Customer_Declaration_Signed_Uploaded__c (shown in screenshot)

User-added image
 
Ajvad AjuAjvad Aju
I think it will work with the above simple trigger, try 'opp.Customer_Declaration_Signed_Uploaded__c == true' instead of 'opp.fileUploaded==true'
Kieran HarrisonKieran Harrison
Hi Ajvad, 

It is not working - I have been testing and once I get to the correct stage then the Customer_Declaration_Signed_Uploaded__c checkbox has already been checked?