Posts

Showing posts with the label apex

Mark custom object record as duplicate via Apex for large (100,000+) data sets

 Problem statement Let's say I have a data load that inserts 100,000 unique records of a custom object - call it MyObject__c. Each record has a lookup to a Contact record that is populated. I want to iterate over the entire list of 100,000 inserted records and find all of the records of MyObject that look up to the same Contact, and then set a checkbox on all but one record that indicates they are 'duplicates'. Example: there are 5 record of MyObject (out of 100,000) that all look up to the Contact MyContact (SFID 123456789). After processing the entire list of 100,000 records, I'd have four of the five records have a 'duplicate reference' checkbox marked, with the fifth one having it unchecked. Basically a uniqueness check on the lookup. What would be the best way to accomplish something like this? My current thought process is to build an Apex batch job; The batch runs the query to get all MyObject records that need to be processed (100,000) and splits them in...

Governor limit SOQL 101 being suppressed

System.LimitException: Too many SOQL queries: 101 Did anyone faced scenario where SOQL 101 governor limit is not being reported to user interface (UI), instead it is being logged into debug logs ? Please provide your thoughts in the comments if there exists any possible scenario. As far as I know, Governor limits can not be suppressed and will be shown to user on the UI. However I am facing below scenario where SOQL 101 limit is being logged into debug log and not being shown in the UI. 1.User converts the lead. 2.Account, Opportunity, Contact created. 3.User is successfully redirected to newly created account page. 4.All the references are correct. Like ConvertedAccountId, convertedOpptyId etc. When I go to the Debug logs, There SOQL 101 (System.LimitException: Too many SOQL queries: 101) is being shown. If dig more in the debug logs then I found that this exception came while Opportunity Before Update trigger. The question is, Since all this is happening in sing...

apex object userRecordAccess not returning any row

userRecordAccess not returning any row Salesforce has launched new object ' userRecordAccess ' which provides the information that whether an user has access to a particular record. SELECT RecordId, HasEditAccess FROM UserRecordAccess WHERE userId = '0053232192...' AND RecordId = '001*****' However when I tried to use the same in my code, there were some issues. If you run the above query in the anonymous or developer console you get proper result. However the same query when embeded into the code with user id with some variable and record id from some variable. then the same query doesn't return any row. Although I found that the above problem resolves if same query is fired through dynamic query call. i.e. Database.execute( query ). Weird but it solve the purpose.

Too many batch retries in the presence of Apex triggers and partial failures.

Too many batch retries in the presence of Apex triggers and partial failures If you write and execute batch scripts then some time you might end up with this error, which may be difficult to understand. However proper explanation is given in the Salesforce documentation as below. Bulk DML Exception Handling Exceptions that arise from a bulk DML call (including any recursive DML operations in triggers that are fired as a direct result of the call) are handled differently depending on where the original call came from: When errors occur because of a bulk DML call that originates directly from the  Apex  DML statements, or if the  all_or_none  parameter of a database DML method was specified as true, the runtime engine follows the “all or nothing” rule: during a single operation, all records must be updated successfully or the entire operation rolls back to the point immediately preceding the DML statement. When errors occur because of a bulk DML call that ...

Salesforce trigger hell-1

Problem Many a time we see that the errors added by trigger's, are being shown to user in the below format, which are not user friendly and user's complain about that. Error: Invalid Data.  Review all error messages below to correct your data. Apex trigger validateLastname caused an unexpected exception, contact your administrator: validateLastname: execution of BeforeInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, no child allowed: Trigger.validateLastname: line 6, column 1 Root cause Whenever error with the line number is shown to user, you can be sure that this is un-handled exception. And it comes basically when you have fired another DML inside any trigger, and that DML is failed.  Let's understand this by scenario. Scenario  Let's assume that User is trying to save a record of OBJ1 from User interface. and you have written a trigger on OBJ1, which on certain cond...

apex HTML pass through not working

Pass through attribute doesn't generate proper html HTML Pass through provided by salesforce in VF pages has been very useful when working with javascript libraries, However while working on the same I came across a situation where Pass through attribute doesn't get passed to generated html if the binding variable is 'Currency' type. Below is the example. Controller 1: public Opportunity Oppty {get;set;} VF page 1: <apex:outputField value = '{!Oppty.Amount}' html-passedValue='XYZ'></apex:outputField> Generated html 1: <span id="j_id0:j_id1:j_id14">$20</span> Hence you can see that the pass through attribute is not generated. However if you change the binding variable to any type other then Currency, pass through will start working. as shown below. VF page 1: <apex:outputField value = '{!Oppty.name}' html-passedValue='XYZ'></apex:outputField> ...

IsVisibleInSelfService on Task salesforce

I was working on assignment which required me to access Tasks and show on customer portal. I written the apex code and fired SOQL, but to my surprise none of Tasks were fetched. I thought its a permission issue from profile or sharing access issue. However at the end I found that there is a field on task object called  IsVisibleInSelfService  which derives the visibility of the tasks for customer portals. Even Apex code can't find the task if this boolean field is not set to true. Probably its trivial information but usefull when you are not aware of this :)