0

I have a custom object Call2_vod__c which is detail to Account.

It has a Signature_vod__c field that is in encoded base64 format.

Now for one Account, I can create an attachment from any of the calls associated with it (Account:Call2_vod__c=1:many).

I am getting a limit exception with my below code. Can someone please help me?

Apex Class:` global class Account_chk { WebService static void method1(Id i) { List a= new List(); List p=new List (); List exist=new List (); exist=[select ParentId,id from Attachment];

Map existMap = new Map(); Map signMap = new Map();

for (Attachment att : exist) { existMap.put(att.ParentId, att); }

for (Call2_vod__c call: [select Name,Id,Signature_vod__c,Signature_Date_vod__c,Account_vod__c from Call2_vod__c where Signature_Date_vod__c !=Null and Account_vod__c not in :existMap.keySet()])
{

  Attachment record = new Attachment(Name = Call.Id ,ParentId = call.Account_vod__c , Body = EncodingUtil.base64Decode(call.Signature_vod__c), ContentType = 'image/jpeg');
   if (record.parentId !=Null && existMap.get(record.ParentId).Id == null) {
    p.add(record);

}

}
try { insert(p);
} catch(System.DMLException e){
  System.debug('The following exception has occurred: ' + e.getMessage());
  }

} }`

Please find the error currently I am facing with the code:

Failed to execute 'send' on 'XMLHttpRequest':Failed to load 'https://cs18.salesforce.com/services/Soap/package/Account_chk'

  • 1
    Hi @saikat. Providing the full error and stacktrace will help people understand your issue. – BarCotter Aug 29 '14 at 11:12
  • Error: Query of LOB fields caused exceeding heap storage limit. The class is being called from a Custom Button in Account page. – Testing_SFDC Aug 29 '14 at 11:16
4

You are querying too many unneeded records and creating Attachments for them which is why you are hitting the heap limit. Try make the 2 changes listed below

1: Restrict your query

Changing the SOQL to have extra where conditions will mean you are querying less records which means less fields that are base64 encoded. The SOQL below is looking at the Account_vod__c fields which will mean you query less records.

[select Name,Id,Signature_vod__c,Signature_Date_vod__c,Account_vod__c 
    from  Call2_vod__c 
    where Signature_Date_vod__c !=Null and Account_vod__c in :existMap.keySet()]

The extra check for Account_vod__c in :existMap.keySet() will stop you querying the records you are not interested in.

2: Change the for loop

Because you do your SOQL and store the results in a it means that ALL records will be on the heap. Try changing your for loop to the format shown below. This will avoid the heap issues are you are not storing all the results to a list:

for (Call2_vod__c call : [select ... from ... where ...]) {
    //
}

Doing number 2 will probably fix your issue but having the SOQL returning so many records is wrong and will cause problems as the number of Call2_vod__c records grow. For example: you will hit another limit if that query returns more than 10,000 records

  • Hi BarCotter! I have edited the class as you said,still facing the exception.Please suggest the way out here.:( – Testing_SFDC Aug 29 '14 at 20:06
  • Theres two SOQL selects in that code. One used for the for loop and the other one right before it. The first one is no longer needed. – BarCotter Aug 30 '14 at 8:40
  • Can you please put it for me?I am sorry I can't get it. – Testing_SFDC Sep 2 '14 at 12:23
  • Remove this line: a=[select Name,Id,Signature_vod__c,Signature_Date_vod__c,Account_vod__c from Call2_vod__c where Signature_Date_vod__c !=Null and Account_vod__c in :existMap.keySet()]; – BarCotter Sep 2 '14 at 12:44
  • hi BarCotter ! i've tried the changes u mentioned, but getting null pointer exception at line 20 col 1 – Testing_SFDC Sep 2 '14 at 14:13
2

You cannot catch or handle LIMIT exception.

Depending on actual LIMITs being violated, you could try to check if some value is close to limit and stop any functionality if it is approaching the limit. Check https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_limits.htm Limits class which has some helpful methods to get current limits usage.

  • Hi Patlatus, can this code be optimzed to do the necessary operation by passing limit exception? – Testing_SFDC Aug 29 '14 at 11:17
  • First, you can use getHeapSize() and getLimitHeapSize() to find current usage of heap and limit allowed to use. – Patlatus Aug 29 '14 at 11:20
  • Second, yes, any code can be optimized. If you have too many masters or too many details records, then wouldn't you like to pagination? – Patlatus Aug 29 '14 at 11:21
  • I mean, to query only for small amount of records instead of querying all of them? – Patlatus Aug 29 '14 at 11:22
  • I need attachments one each for all Accounts that are having detail records. – Testing_SFDC Aug 29 '14 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.