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
Kiran kumar 193Kiran kumar 193 

update attachment name as opportunity name?

I want to update attachment name as opportunity name... Please share the code
SalesFORCE_enFORCErSalesFORCE_enFORCEr
trigger BeforeInsertOfAttachment on Attachment(before insert) {
    for(Attachment att: Trigger.New){
        att.Name = [Select Id,Name from Opportunity where Id=:att.ParentId].Name;
    }
}
Kiran kumar 193Kiran kumar 193
My Requirement is I have opportunity and Childs are Opportunity Template and Attachment.

Opportunity template has already some attachments and with the same name I want to update the name in attachment as well..

How do I do this?

 
Kiran kumar 193Kiran kumar 193
This is my code and it is not working as expected..

public static void beforeInsert(list<attachment> newrecords)
    {
          Set<ID> parentIDs = new Set<ID>();
          List<attachment> updateAttachmentList = new list<attachment>();
          for(attachment oattach : newrecords){        
            if(oattach.ParentId.getSobjectType() == SBQQ__Quote__c.SobjectType){                             
                parentIDs.add(oattach.ParentID);
            }        
        } 
                 
     List<SBQQ__QuoteDocument__c> quoteDocuments = [SELECT Id, Name, SBQQ__QuoteTemplate__c,  SBQQ__QuoteTemplate__r.Name, SBQQ__Quote__r.Name FROM SBQQ__QuoteDocument__c where SBQQ__Quote__c =:parentIDs];
          for(attachment oattach : newrecords){ 
              oattach.Name= quoteDocuments[0].SBQQ__QuoteTemplate__r.Name + ' - ' + quoteDocuments[0].SBQQ__Quote__r.Name;
              
              updateAttachmentList.add(oattach);
          }
          if(!updateAttachmentList.isEmpty()){
          //update updateAttachmentList;
        }
    }
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Change you query to this
 List<SBQQ__QuoteDocument__c> quoteDocuments = [SELECT Id, Name, SBQQ__QuoteTemplate__c,  SBQQ__QuoteTemplate__r.Name, SBQQ__Quote__r.Name FROM SBQQ__QuoteDocument__c where SBQQ__Quote__c IN:parentIDs];
Kiran kumar 193Kiran kumar 193
Getting below error on after insert
Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AttachmentTrigger: execution of AfterInsert caused by: System.ListException: List index out of bounds: 0 () : []
Kiran kumar 193Kiran kumar 193
I want to represent Attachment same name Quote document name
I want to represent Attachment same name Quote document name
SalesFORCE_enFORCErSalesFORCE_enFORCEr
You are attaching on Opportunity so Parent Id will be the opportunity Id but you are passing opportunity id as Quote__c id in your query. It will never return anything. So, your query should be
List<SBQQ__QuoteDocument__c> quoteDocuments = [SELECT Id, Name, SBQQ__QuoteTemplate__c,  SBQQ__QuoteTemplate__r.Name, SBQQ__Quote__r.Name FROM SBQQ__QuoteDocument__c where Opportunity__c IN:parentIDs];

I am guessing Opportunty__C is a field on Quote__c object.
Kiran kumar 193Kiran kumar 193
Still I am getting below error..

nsert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AttachmentTrigger: execution of BeforeInsert caused by: System.ListException: List index out of bounds: 0 () : []
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Can you try adding debugs to check if parentIDs is getting populated with OpportunityId correctly?
Kiran kumar 193Kiran kumar 193
Can you check the screeshot once...that is requirement..

I want to populate Same quote documents as the Attachments
SalesFORCE_enFORCErSalesFORCE_enFORCEr
I think I got the problem. The issue is in building the parentIds. The attachment is on opportunity but you are checking if the object typs is Quote__c which will be false always. Hence, your query will not return anything.
Just use this if clause instead
if(oattach.ParentId.startsWith('006')){                             
      parentIDs.add(oattach.ParentID);
 }
Kiran kumar 193Kiran kumar 193
Still not working ...this is my code... can you please check once

Class 

public class AttachmentTrigger1
{

    public static void beforeInsert(list<attachment> newrecords)
    {
          Set<ID> parentIDs = new Set<ID>();
          List<attachment> updateAttachmentList = new list<attachment>();
          for(attachment oattach : newrecords){        
            if(String.valueOf(oattach.ParentId).startsWith('006')){                            
                parentIDs.add(oattach.ParentID);
            }        
        } 
                 
     List<SBQQ__QuoteDocument__c> quoteDocuments = [SELECT Id, Name, SBQQ__QuoteTemplate__c,  SBQQ__QuoteTemplate__r.Name, SBQQ__Quote__r.Name FROM SBQQ__QuoteDocument__c where SBQQ__Opportunity__c IN:parentIDs];
     for(SBQQ__QuoteDocument__c doc : quoteDocuments){
            if(doc.SBQQ__QuoteTemplate__c == null || 
               doc.SBQQ__Opportunity__c == null || 
               doc.SBQQ__QuoteTemplate__r.Name == null){
              
                 Attachment a = new Attachment();
                 a.Name = doc.SBQQ__QuoteTemplate__r.Name + ' - ' + doc.SBQQ__Opportunity__r.Name;
                  updateAttachmentList.add(a);
            }
          if(!updateAttachmentList.isEmpty()){
          update updateAttachmentList;
        }
    }
    }
}

Trigger :

trigger AttachmentTrigger on Attachment (before insert,after insert) {       
     if(Trigger.isInsert && Trigger.isBefore){
    AttachmentTrigger1.beforeInsert(trigger.new);
    }
    
        
}
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Before I update your code, I have a question. Will there be only one Quote document per opportunity? What if there are more than one quote documents then how will you name the attachments?
Kiran kumar 193Kiran kumar 193
Multiple quote documents should be populated...

1) When Quote get generated
2) when order get generated

The name of the attachment should be if it is quote - Quote Attachment
The name of the attachment should be if it is Order - Order Attachment
Kiran kumar 193Kiran kumar 193
@SalesFORCE_enFORCEr,

do you have Any solution on this?
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Sorry but I did not understand it. I thought you just want to rename the attachment to Quote name. 
Kiran kumar 193Kiran kumar 193
whenever new Quote document inserted automatically same attachment should be inserted into the attachment object.

Quote document object is child of Quote object.
Attachment also child of Quote Object
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Oh I got confused becasue you are changing the name of the attachment. So, how are you attaching the document on Quote Document object? Using Attachment related list?
Kiran kumar 193Kiran kumar 193
Steelbrick Managed package code generating the Quote documents on Quote Document object. So we have a trigger on Quote document object to change the names of files based on seleted template while generating the the document.

so now I have a requirement to change the name of Attachment in the attachment object. 

so finally the documents on document object and Attachment object should be the same 
chikkuchikku
Hi all,
try this one too works perfectly for me.

you have to retrieve the Name of the opportunity :
Opportunity opp = [SELECT Name FROM Opportunity WHERE id =: a.id];
Attachment attach = new Attachment(+ opp.Name +System.today() +);
 
opp.Name  ----> it'll retrieve the  opportunity name