+ Start a Discussion
Laurie DrewLaurie Drew 

Method does not exist or incorrect signature: void startswith(String) from the type Id

Hello Friends,

I am getting an error trying to compile my class that I am hoping someone can assist with.  My requirement is to check a box on the Case object when an attachment is added to the case.  I  am getting the message 'Method does not exist or incorrect signature: void startswith(String) from type Id" . 

Any assistance would be greatly appreciated.  Here's my code:

trigger AttachmentPresent on Attachment (after insert) {
    Set<Id> caseIds = new Set<Id>();
    for(Attachment att : Trigger.New) {
        //Check if added attachment is related to Case or not
        if(att.ParentID.startswith('500')) {
            caseIds.add(att.ParentId);
        }
    }
    if(!caseIds.isEmpty()) {
        CaseHandler.attachmentAdded(caseIds);
    }
}

And Class:

public with sharing class CaseHandler {

    public static void attachmentAdded(Set<Id> caseIds) {
        List<Case> updateCases = new List<Case>();
        List<Case> caseList = [SELECT Id, has_Attachment__c FROM Case WHERE Id IN: caseIds];
        for(Case myCase : caseList) {
            if(!myCase.has_Attachment__c) {
                myCase.has_Attachment__c = true;
                updateCases.add(myCase);
            }
        }
        if(!updateCases.isEmpty()) {
            update caseList;
        }
    }
}
Best Answer chosen by Laurie Drew
Hemant_SoniHemant_Soni
Hi,
The issue is att.ParentID is the Id type of field and you are checking with wrong type so you need to Check list String.valueOf(att.ParentID).startsWith();
Then you will not get any error here.
Please let me know if still you are facing any issue.
Thanks
Hemant

All Answers

Hemant_SoniHemant_Soni
Hi,
The issue is att.ParentID is the Id type of field and you are checking with wrong type so you need to Check list String.valueOf(att.ParentID).startsWith();
Then you will not get any error here.
Please let me know if still you are facing any issue.
Thanks
Hemant
This was selected as the best answer
Laurie DrewLaurie Drew
That did it, thank you Hemant!
Steven NsubugaSteven Nsubuga
Change the for loop to this.
if(((String)att.ParentID).startswith('500')) {
            caseIds.add(att.ParentId);
}
The trigger will be
trigger AttachmentPresent on Attachment (after insert) {
    Set<Id> caseIds = new Set<Id>();
    for(Attachment att : Trigger.New) {
        //Check if added attachment is related to Case or not
        if(((String)att.ParentID).startswith('500')) {
            caseIds.add(att.ParentId);
        }
    }
    if(!caseIds.isEmpty()) {
        CaseHandler.attachmentAdded(caseIds);
    }
}

 
v varaprasadv varaprasad
Hi Laurie,

Please execute following code in the anonymous window  : 
 
case cs = new case();
cs.status = 'new';
cs.Origin = 'Phone';
insert cs;



Attachment attachment = new Attachment();
  attachment.Body = Blob.valueOf('Test Attachment');
  attachment.Name = String.valueOf('test.txt');
  attachment.ParentId = cs.id; 
  insert attachment;
  
  set<id> caseIds = new set();
  caseIds.add(cs.id);
  
  CaseHandler.attachmentAdded(caseIds);



Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com