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
Praveen JhaPraveen Jha 

Change case status when attachment is added

When I add attachment on case, case status should change from new to working. How can i do this??
Sagar PareekSagar Pareek
Open developer console and write a small trigger on attachment , below is sample code which can help you (assuming that your status field is custom field on case)
 
trigger SetTitleToAttachment on Attachment (after insert) {

String Title;
Id pId;

for(Attachment att: Trigger.new){
Title=att.Name;
pId=att.ParentId;
}

List<Case> c=[select Id , Title__c from Case where Id=:pId];

//assuming one record is fetched.
c[0].Status__c=Title;

update c[0];
 
}

 
Sagar PareekSagar Pareek
at line 7 replace with Title='working';
Bhanu MaheshBhanu Mahesh
Hi Praveen,

Refer the following link how to write trigger on attachment through Developer Console
https://help.salesforce.com/HTViewSolution?id=000181538&language=en_US (https://help.salesforce.com/HTViewSolution?id=000181538&language=en_US)

And the trigger code will be 
trigger UpdateCaseStatus on Attachment (after insert) {
    Set<Id> stCaseIds =  new Set<Id>();
    List<Case> lstCasesToUpdate = new List<Case>();
    for(Attachment att:trigger.new){
        If(att.ParentId != null && String.valueOf(att.ParentId).startsWith('500')){
             stCaseIds.add(att.ParentId);   
        }
    }
    if(!stCaseIds.isEmpty()){
        for(Case cas:[SELECT Id,Status FROM Case WHERE Id IN :stCaseIds AND Status = 'New']){
            cas.status = 'Working'; 
            lstCasesToUpdate.add(cas);
        }        
    }
    if(!lstCasesToUpdate.isEmpty()){
        update lstCasesToUpdate;      
    }
}

Regards,
Bhanu Mahesh