Thursday 28 December 2017

How to enable debug for site user in Salesforce

1. Set a browser cookie:

  • Open your Force.com site URL in your browser tab. (This is important - You won't see debug log until Setting cookie without opening your site)
  • Open the Chrome DevTools Console by pressing Ctrl+Shift+J (Cmd+Opt+J on macOS).
  • Execute a command to set the cookie. (Copy and paste the below command and click Enter)

          If you use a .force.com domain, use this command.
         document.cookie="debug_logs=debug_logs;domain=.force.com";

         If you use a custom domain (for example, yourCustomDomain.com), use this command.
         document.cookie="debug_logs=debug_logs;domain=yourCustomDomain.com";


2. Find the name of your site’s guest user:
  • From Setup, enter Sites in the Quick Find box, then select Sites.
  • Select your site from the Site Label column.
  • Select Public Access Settings | View Users.
3. Set a user-based trace flag on the guest user.
  • From Setup, enter Debug Logs in the Quick Find box, then click Debug Logs.
  • Click New.
  • Set the traced entity type to User.
  • Open the lookup for the Traced Entity Name field, and then find and select your guest user.
  • Assign a debug level to your trace flag.
  • Click Save.

Reference:

Tuesday 6 June 2017

How to use Limits Apex Methods to avoid Hitting Governor Limits

Many of us facing governor limit error in trigger/classes/test classes. Few of the governor limit errors as follow,

1. Too many SOQL queries: 101
2. Too many dml rows 10001
3. too many query rows 50001.

This article is helpful to verify the Governor Limit by checking limit methods to avoid hitting the governor limit.

Here is the snippet,
  1. System.assert((Limits.getDMLRows() * 100 / Limits.getLimitDMLRows()) < 50);
  2. System.assert((Limits.getDMLStatements() * 100 / Limits.getLimitDMLStatements()) < 50);
  3. System.assert((Limits.getAggregateQueries() * 100 / Limits.getLimitAggregateQueries()) < 50);
  4. System.assert((Limits.getQueries() * 100 / Limits.getLimitQueries()) < 50);
  5. System.assert((Limits.getQueryRows() * 100 / Limits.getLimitQueryRows()) < 50);

Lets take this example,
  1. System.assert((Limits.getQueries() * 100 / Limits.getLimitQueries()) < 50);

In the synchronous transaction, the number of query limit is 100. If it exceeds, then your will get an error like Too many SOQL queries: 101.

  1. Account acc = [SELECT Id FROM Account Limit 1];
  2. System.debug('## Queries Used'+Limits.getQueries());
  3. System.debug('## Total Allowed Queries'+Limits.getLimitQueries());
  4. System.assert((Limits.getQueries() * 100 / Limits.getLimitQueries()) < 50);

Just execute the above snippet in the developer console. We have used one query

## Queries Used --> 1
## Total Allowed Queries --> 100
So as per the assert statement,

(1 * 100 / 100) < 50 --> So the transaction is safe without hitting SOQL limit. Because of query limit is less than 50 out of 100.

References:

Wednesday 19 April 2017

How to check Permission Set in Custom Button Salesforce

Sometimes you may get requirement as below,

1. Create a Custom Button (JavaScript) and assign it to the page layout.
2. Create a Custom Permission Set.
3. If the logged in user not assigned in the specified permission set, then just throw an alert like "You don't have sufficient Permission". Else navigate to another page or whatever based on your requirement.

Custom Button JavaScript Code:

  1. {!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}
  2.  
  3. var result = sforce.connection.query("SELECT Id FROM PermissionSetAssignment WHERE PermissionSet.Name = 'Payment_Type_Permission_Set' AND AssigneeId = '{!$User.Id}'");
  4.  
  5. var psAssignment = result.getArray("records");
  6. if(psAssignment.length === 1){
  7. window.location.href = '/apex/PaymentChange?id={!Log__c.Id}';
  8. }
  9. else
  10. {
  11. alert('You do not have sufficient permission. Please contact your administrator.');
  12. }


1. Initialized the REQUIRESCRIPT resource for SOQL query.
2. Queried PermissionSet using sforce.connection.query. 
Where,
PermissionSet.Name is API name of the Permission Set.
AssigneeId use
3. In apex we will check the List size, here get records using getArray method and assign it to var.
4. Check variable length which is equal to list size.
5. Finally, do logic as need. 


References:

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...