0

I have an apex class where I need to check if a field value of a particular record is contained in a pre-defined list. Please find my code here.Can someone please help? Apex class:

global class Account_chk {
  WebService static void method1(Id i) {
  List<Call2_vod__c> a= new List<Call2_vod__c>();
  List<Attachment> p=new List<Attachment> ();
    List<Attachment> exist=new List<Attachment> ();
    exist=[select ParentId,id from Attachment];
    a=[select Name,Id,Signature_vod__c,Signature_Date_vod__c,Account_vod__c from Call2_vod__c where Signature_Date_vod__c !=Null limit 100];
    for (Call2_vod__c call:a)
    {          
        Attachment record = new Attachment(Name = Call.Id ,ParentId = call.Account_vod__c , Body = EncodingUtil.base64Decode(call.Signature_vod__c), ContentType = 'image/jpeg');
        if(exist.contains(record.parentId))
         p.add(record);          
    }
    try { insert(p);
    } 
    catch(System.DMLException e){
      System.debug('The following exception has occurred: ' + e.getMessage());
    }
  }
}

Issue is with below line :

if(exist.contains(record.parentId))

2 Answers 2

1

I would build up a map of Attachments using ParentId as the key, like so:

exist=[select ParentId,id from Attachment];

Map<Id, Attachment> existMap = new Map<Id, Attachment>();

for (Attachment att : exist) {

    existMap.put(att.ParentId, att);

}

and then check the map contents:

if (existMap.get(record.ParentId).Id != null) {

}
2
  • Hi DavinC! It is working! Thanks but I facing a Null pointer exception and Limit Exception here for evaluating records more than 200 or so. Aug 29, 2014 at 10:52
  • Glad to hear it. I would suggest posting a new question about the errors you're now seeing, as opposed to hyjacking this one. Aug 29, 2014 at 11:02
1

You are testing if a List of Attachment contains an Id. It's not correct !

Try :

if(exist.contains(record))
3
  • Thanks SF_User! just a question. ParentId field should be matched with exist and record , name or other fields may differ.Should it take care of such cases? Aug 29, 2014 at 9:36
  • Sorry, I don't understand what you want !
    – SF_user
    Aug 29, 2014 at 9:40
  • I mean I need to match ParentId of record(yet to be created) with ParentId of exist. Can this operation be done by this? Aug 29, 2014 at 10:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .