+ Start a Discussion
EdCodeEdCode 

Accessing custom field on Case from Attachment (through relationship)

Hello,

On line 65, how can I set the value of the custom field Has_Attachment__c on the standard sObject Case to FALSE?

I would have thought that I could reach this custom field on Case from Attachment through the relationship name Attachments (I have verified the name of the this relationship in Workbench), but it is not working.

See my code below and line 65?
User-added image
 
EdCodeEdCode
I am adding the code below as the above screen-shot does not look very clear. I highlight the line of code 65:

    public static void hasAttachmentFalse(List<Attachment> inboundAttachments){
        Map<Id,List<Attachment>> mapParentCaseIdWithAttachments = new Map<Id,List<Attachment>>();
        List<Attachment> lstAttachments = new List<Attachment>();
        for(Attachment a:inboundAttachments){
            if(mapParentCaseIdWithAttachments.containsKey(a.ParentId)){
                List<Attachment> lstExistingAttachments = mapParentCaseIdWithAttachments.get(a.ParentId);
                lstExistingAttachments.add(a); //add iterated child to existing list of children attachments (existing in the map)
                lstAttachments = lstExistingAttachments;//assign increased list of children to new list of children
                
            }//end-if
            else
            {
                lstAttachments.add(a);
            }//end-else
            mapParentCaseIdWithAttachments.put(a.ParentId, lstAttachments); //add new list of children to Map
            if(lstAttachments.size() == 0){
                a.Attachments.Has_Attachment__c = FALSE;
            }
        }
    }//end-loop - the Map contains all information
} //end-method
Raj VakatiRaj Vakati
Use this code
 
public static void hasAttachmentFalse(List<Attachment> inboundAttachments){
        Map<Id,List<Attachment>> mapParentCaseIdWithAttachments = new Map<Id,List<Attachment>>();
        List<Attachment> lstAttachments = new List<Attachment>();
        for(Attachment a:inboundAttachments){
            if(mapParentCaseIdWithAttachments.containsKey(a.ParentId)){
                List<Attachment> lstExistingAttachments = mapParentCaseIdWithAttachments.get(a.ParentId);
                lstExistingAttachments.add(a); //add iterated child to existing list of children attachments (existing in the map)
                lstAttachments = lstExistingAttachments;//assign increased list of children to new list of children
                
            }//end-if
            else
            {
                lstAttachments.add(a);
            }//end-else
            mapParentCaseIdWithAttachments.put(a.ParentId, lstAttachments); //add new list of children to Map
            if(lstAttachments.size() == 0){
                a.Has_Attachment__c = FALSE;
            }
        }
    }//end-loop - the Map contains all information
}

 
EdCodeEdCode
Hello Raj,

The object is an instance of the sObject Attachment (inboundAttachments is a List of Attachment).

But the field Has_Attachment__c is a field created at the standard sObject Case level (not Attachment)

In the line 17 of your above code/reply, you have kindly suggested  a.Has_Attachment__c = FALSE;    which suggests you missunderstood that the field Has_Attachment__c exists at the Attachment standard sObject level. But it does not exist there. The field Has_Attachment__c only exists at the parent sObject Case.

Ultimately, I will want to set and update the Case.Has_Attachment__c field to the value TRUE.

Do you know how to to this?

Thank you very much.