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
Debbie61Debbie61 

Problem: Invalid type: QueryResult

I am trying to write a trigger that will check to see if an xls spreadsheet attachment has been uploaded for a custom object. In the API,  Attachments are of type="tns:QueryResult". But when I deploy my trigger to Salesforce, I get an error back that says:  Problem: Invalid type: QueryResult

 

Here is the code:

trigger dmbSurveyContainsSpreadsheet on dmb_survey__c (after update) {	
	 
    for(dmb_survey__c obj : Trigger.new) 
    { 
    	Boolean contains = false;
    	if (obj.Status_Setting__c == 'Complete')
    	{
    		QueryResult qr = obj.Attachments;
    		if (qr != null)
    		{
    			for (int i = 0; i < qr.records.Length; i++) 
    			{
    			   Attachment attach = (Attachment)attachQR.records[i]; 
    			   string contentType = attach.ContentType;
                              contains = contentType.Contains('ms-excel');
                              if (contains)
                              {
                                  break;
                               }    				    	
    			}
    		}
    		if (!contains)
    		{    		
    			obj.Contains_Spreadsheet__c = 1;
    		}    		    		
    	}    	
    }
}

 

 

 

Could somebody please tell me what I am doing wrong? Thank you!

Best Answer chosen by Admin (Salesforce Developers) 
Debbie61Debbie61

I do not know why Apex code is so painful. I can code Java and C# til the cows come home. Here is the final solution that works.

 

trigger dmbSurveyContainsSpreadsheet on dmb_survey__c (before update) { 

    Set<Id> attachIds = new Set<Id>();
    
    for (dmb_survey__c dmb : Trigger.new) {  
         //Load the Ids Set from this object ref. to the Master              
         attachIds.add(dmb.Id);                
    } 
    
    Attachment[] aArray = new List<Attachment>();
    aArray = [select ParentId, ContentType from Attachment where ParentId in :attachIds];    
            
     for(dmb_survey__c obj : Trigger.new) 
    { 
        Boolean contains = false;
        if (obj.Status_Setting__c == 'Complete' )
        {  
            
            for (Attachment a : aArray)
            {
                if(a.ParentId == obj.ID)
                {
                    String contentType = a.ContentType;
                    contains = contentType.contains('ms-excel');
                    System.Debug('ContentType is ' + contentType);
                    if (contains)
                    {                        
                         break;
                    }                 
                }
                                                            }                        
            if (!contains)
            {           
                obj.No_Spreadsheet__c = true;
            }                              
        }       
    }
}

 

All Answers

Anand@SAASAnand@SAAS

Try :

 

List<Attachment> qr = obj.Attachments;

 

 

You are confusing the Java code and Apex. They are differences when you write something in Java v/s Apex.

 

Also, you will have to query for the Attachment related to the records in your trigger first. Trigger does'nt automatically bring the related objects when it executes your code.. 

Debbie61Debbie61

I have re-written my query but am getting a new error that claims that the following statement must be a list type, which I am not understanding why it would be a List type at this point.

 

String contentType = attachMap.get(obj.Id)[i].ContentType;

 

The error thrown is: Compile Error: Expression must be a list type: SOBJECT:Attachment at line 25 column 42

The entire code block follows:

trigger dmbSurveyContainsSpreadsheet on dmb_survey__c (before update) { 

    Set<Id> attachIds = new Set<Id>();
    
    for (dmb_survey__c dmb : Trigger.new) {  
         //Load the Ids Set from this object ref. to the Master              
         attachIds.add(dmb.Id);                
    } 
    
    Map<Id, Attachment> attachMap = new Map<Id, Attachment>
       ([select Id, ParentId, ContentType from Attachment where ParentId in : attachIds  ]);
        
     for(dmb_survey__c obj : Trigger.new) 
    { 
        Boolean contains = false;
        if (obj.Status_Setting__c == 'Complete' )
        {  
            if ( attachMap.get(obj.Id).size() > 0)
            {
                for(Integer i = 0; i < attachMap.get(obj.Id).size(); i++) 
                {
                    String contentType = attachMap.get(obj.Id)[i].ContentType;
                    contains = contentType.contains('ms-excel');
                    System.Debug('ContentType is ' + contentType);
                    if (contains)
                    {                        
                        break;
                    }                
                }            
            } 
            if (!contains)
            {           
                obj.No_Spreadsheet__c = true;
            }                              
        }       
    }
}

 

 

imuino2imuino2

String contentType = attachMap.get(obj.Id)[i].ContentType;

contentType is an Id, Attachment map

When you do attachMap.get(obj.Id) you dont need to use the i index cause you are not in a list or array it is an attachment

all you need to do is

attachMap.get(obj.Id).ContentType;

That will gives you the content type of the attachment related to the Id  on the map.

 

Ignacio.

Debbie61Debbie61

When I revise it, I get an error when I try to save the record that the reference is null for that line of code. The record does have an attachment but my query for getting the attachments must not be right?

 

Error: System.NullPointerException: Attempt to de-reference a null object: On this line:

String contentType = attachMap.get(obj.Id).ContentType;

 

Here is the modified code:

 

trigger dmbSurveyContainsSpreadsheet on dmb_survey__c (before update) { 

    Set<Id> attachIds = new Set<Id>();
    
    for (dmb_survey__c dmb : Trigger.new) {  
         //Load the Ids Set from this object ref. to the Master              
         attachIds.add(dmb.Id);                
    } 
    
    Map<Id, Attachment> attachMap = new Map<Id, Attachment>
       ([select Id, ParentId, ContentType from Attachment where ParentId in : attachIds  ]);
        
     for(dmb_survey__c obj : Trigger.new) 
    { 
        Boolean contains = false;
        if (obj.Status_Setting__c == 'Complete' )
        {              
                    //String contentType = attachMap.get(obj.Id)[i].ContentType;
                    String contentType = attachMap.get(obj.Id).ContentType;


                    contains = contentType.contains('ms-excel');
                    System.Debug('ContentType is ' + contentType);
                    if (contains)
                    {                        
                        break;
                    }                
                        if (!contains)
            {           
                obj.No_Spreadsheet__c = true;
            }                              
        }       
    }
}

 

Debbie61Debbie61

I do not know why Apex code is so painful. I can code Java and C# til the cows come home. Here is the final solution that works.

 

trigger dmbSurveyContainsSpreadsheet on dmb_survey__c (before update) { 

    Set<Id> attachIds = new Set<Id>();
    
    for (dmb_survey__c dmb : Trigger.new) {  
         //Load the Ids Set from this object ref. to the Master              
         attachIds.add(dmb.Id);                
    } 
    
    Attachment[] aArray = new List<Attachment>();
    aArray = [select ParentId, ContentType from Attachment where ParentId in :attachIds];    
            
     for(dmb_survey__c obj : Trigger.new) 
    { 
        Boolean contains = false;
        if (obj.Status_Setting__c == 'Complete' )
        {  
            
            for (Attachment a : aArray)
            {
                if(a.ParentId == obj.ID)
                {
                    String contentType = a.ContentType;
                    contains = contentType.contains('ms-excel');
                    System.Debug('ContentType is ' + contentType);
                    if (contains)
                    {                        
                         break;
                    }                 
                }
                                                            }                        
            if (!contains)
            {           
                obj.No_Spreadsheet__c = true;
            }                              
        }       
    }
}

 

This was selected as the best answer