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
JuulJuul 

Trigger on attachment contains "RFP"

Hi,

 

I'm trying to create a trigger on attachment. If the attachment contains the text "RFP" then the checkbox "RFP_check__c" has to be checked.

 

// Trigger to update the RFP_check__c custom checkbox field in Opportunity (Opportunity) if it has an attachment containing RFP.

            trigger TiggerName on attachment (after insert)

            {

                        List<Opportunity> co = [select id, Attachment__c from Opportunity where id =: Trigger.New[0].ParentId];

                        if(co.size()>0) && att.Name.toLowerCase().contains('RFP')) 

                        {

                                    co[0].RFP_check__c = true;

                                    update co;

                        }                                                                   

            }

 

 Eclipse is giving me the error : Unexpected token &&

 

And the following error:

Screenshot

 

bob_buzzardbob_buzzard

You have an unbalanced closing bracket.  Try changing to:

 

if ((co.size()>0) && att.Name.toLowerCase().contains('RFP'))

 

JuulJuul

Thanks, but now I'm getting the error:

 

Error: Compile Error: Method does not exist or incorrect signature: att.Name.toLowerCase() at line 9 column 46

bob_buzzardbob_buzzard

That's because att doesn't exist.

 

You probably need to pull back the attachment name in the query and check that:

 

List<Opportunity> co = [select id, Attachment__r.Name from Opportunity where id =: Trigger.New[0].ParentId];

 Then you can simply check attachment__r.Name.toLowerCase() ...

 

I'd move this to a before trigger where you can update the opportunity object in situ, otherwise you need to protect against your trigger being recursively called.

bob_buzzardbob_buzzard

That's because att doesn't exist.

 

You probably need to pull back the attachment name in the query and check that:

 

List<Opportunity> co = [select id, Attachment__r.Name from Opportunity where id =: Trigger.New[0].ParentId];

 Then you can simply check attachment__r.Name.toLowerCase() ...

 

I'd move this to a before trigger where you can update the opportunity object in situ, otherwise you need to protect against your trigger being recursively called.

JuulJuul

Thanks Bob, but that isn't working ('Compile Error: Didn't understand relationship 'Attachment__r' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name')

 

I did some new search on the forum. And I found a before insert trigger. This trigger is giving an error when the file name contains shivasoft. And I need to check the field RFPcheck. Can you help me a bit, I'm quite new to triggers.

 

thanks

 

 

trigger CheckAttachmentName on Attachment (before insert) {
	for (Attachment att:Trigger.new)
	{
		String parentObjId = att.ParentId;
		//006 is the starting sting in ID for all opportunities
		if(parentObjId.startsWith('006') && att.Name.toLowerCase().contains('shivasoft'))
		{
				att.addError('Cannot upload file with name containing - shivasoft');
		}
	}

	//This method will return true if the id belongs to Opportunity
	private Boolean isOpportunityParent(String id)
	{
		for(Opportunity opp : [SELECT Id FROM Opportunity WHERE Id = :id])
		{
			return true;
		}
		return false;
	}
}

 

bob_buzzardbob_buzzard

I don't have time to rewrite triggers to your specification I'm afraid.  If you want to have a go and hit problems I'll endeavour to help you out.

JuulJuul

Sorry it was a rude question. Thanks anyway for your help. I will give it a try and when I hit problems I will post it :)