• About
  • Contact me!

Salesforce Everywhere

~ Blog on Salesforce Dev and admin stuff

Salesforce Everywhere

Category Archives: Apex

Tip#19 – Using workbench to execute APEX

10 Wednesday Feb 2016

Posted by Mani in Apex, Uncategorized

≈ 2 Comments

Tags

Salesforce Tips

Today, I was working on a requirement which queries public group and sends an email to the users(group members) with an attachment from the custom object record and it was implemented using APEX code. I wanted to test using Execute Anonymous option in Developer Console before I call the code using process builder. When I tried executing it in Developer Console, it was throwing Sending Request – Execution Failed error. I could not figure out the reason for it, but I realised that using workbench to execute Anonymous APEX code is better than Dev Console since your code, results are displayed in the same page and you can set the log level too.

Steps to execute APEX in Workbench:

a. Login to https://workbench.developerforce.com
b. Select Sandbox or Production(obviously, we will test the code in Sandbox first)
c. Then system will ask you to login to Salesforce
d. Once logged in, select “Apex Execute” in Jump to option
e. Paste your code in the text box and click on Execute button.
f. System will show the results on the same page. You can select the appropriate Log category and Log level too.

In the future post, I will write about creating a flow to query the public group based on the name and send an email to the group members(role based members).

Advertisement

Tip#18 Get Salesforce instance in Apex

09 Tuesday Feb 2016

Posted by Mani in Apex, Uncategorized

≈ Leave a comment

Tags

Salesforce Tips

Recently, I worked on a requirement where in I had to send an email by using APEX and I had to prepare the HTML body within APEX itself. As part of it, there has to be a clickable link in the email which should take the user to the Salesforce record after clicking on it and we should not hardcode the URL in the code.

So, here is the code below to get the logged in user’s URL and concatenate it with the Salesforce record ID:

String u=System.Url.getSalesforceBaseURL().toExternalForm();//this returns the base URL of the Salesforce instance as String.
String ufinal=u+’/’+POId;//POId is the variable that holds Id of the custom object record.

or you can write the above code in one line:

String ufinal = URL.getSalesforceBaseUrl().toExternalForm() + ‘/’ + POId;

Tip# 12: Resolving “Schedulable class has jobs pending or in progress” error

30 Wednesday Sep 2015

Posted by Mani in Apex

≈ 2 Comments

Tags

Apex class save error, Scheduable class error

This error “Schedulable class has jobs pending or in progress” might occur when saving the class after editing it.

This could be due to the reason that there are scheduled jobs related to the class that we are trying to edit or its dependent classes. To resolve the issue, we can abort the jobs manually from Setup->Jobs->Scheduled Jobs.

There is a detailed explanation about this error in the link https://help.salesforce.com/apex/HTViewSolution?id=000004423&language=en_US

P.S. I had got this error recently in Sandbox for one of the class after I modify it, though its dependent class is scheduled and after deleting the corresponding jobs in Setup->Jobs->Scheduled Jobs, I could save the class successfully.

Tip#11 : Getting Role name and Profile Name from User Object

28 Monday Sep 2015

Posted by Mani in Apex

≈ Leave a comment

Tags

Profile name, role name

To fetch Role and Profile name for the logged in user in APEX, use the following SOQL:

SELECT UserRole.Name, Profile.Name FROM User

Please note that UserRole and Profile are lookup fields in User object.

Tip#6: Standard Pricebook record visibility in Test Class

08 Tuesday Sep 2015

Posted by Mani in Apex, APEX Test class

≈ Leave a comment

Tags

APEX Test Class, Standard Pricebook

Suppose, we want to access Standard Pricebook Id in the test class and this can be achieved in 2 ways:

a. Annotate the test class with @isTest(SeeAllData=true).
b. Use getStandardPricebookId() method of Test Class as given below.

Id pricebookId = Test.getStandardPricebookId();// This is available irrespective of the state of SeeAllData.

Imagine we have a test class which creates a record in Custom object and once the record is saved, flow is called which creates Pricebook/Product/Pricebook entry records. In this case, we would have to go with annotating the test class with SeeAllData=true as we do not need to pass Standard Pricebook Id instead it is queried inside the flow, but it will be visible only with SeeAllData=true.

Please let me know for any comments/clarification.

Tip#5: APEX Best Practices

06 Sunday Sep 2015

Posted by Mani in Apex

≈ Leave a comment

Tags

APEX Best Practices, APEX Code

There are instances where we need to write APEX code when the desired functionality cannot be achieved through point and click. Following are the links which talks about APEX best practices:

https://developer.salesforce.com/blogs/developer-relations/2015/01/apex-best-practices-15-apex-commandments.html

https://developer.salesforce.com/page/Apex_Code_Best_Practices

Enjoy writing good apex code!

 

SOQL : Using NOT LIKE

01 Tuesday Sep 2015

Posted by Mani in Apex

≈ Leave a comment

Tags

NOT LIKE IN SOQL, SOQL

There may be instances where we need to use NOT LIKE in SOQL(I got to use it recently in my project) and using it in SOQL is little more counter intuitive unlike LIKE.

For instance, we want to fetch Id from Job where Status=Complete,Type contains ‘Physiotherapy’ and it should not contain ‘Pain Management’, for instance Type=’Physiotherapy Pain Management’ should not be fetched and the correct query is as follows:

select id from Job__c where Job_Status__c=’Complete’ and Type__c LIKE ‘Physiotherapy%’ and (NOT Type__c LIKE ‘%Pain Management%’)

You might get MALFORMED_QUERY … unexpected token: ‘not’ or MALFORMED_QUERY … unexpected token: ‘like’, if you try any similar variants as below which would not work:

select id from Job__c where Job_Status__c=’Complete’ and Type__c LIKE ‘Physiotherapy%’ and NOT Type__c LIKE ‘%Pain Management%’

select id from Job__c where Job_Status__c=’Complete’ and Type__c LIKE ‘Physiotherapy%’ and NOT (Type__c LIKE ‘%Pain Management%’)

select id from Job__c where Job_Status__c=’Complete’ and Type__c LIKE ‘Physiotherapy%’ and Type__c NOT LIKE ‘%Pain Management%’

Happy sharing!

Getting Recordtype Id in APEX

29 Saturday Aug 2015

Posted by Mani in Apex

≈ Leave a comment

Tags

APEX, RecordtypeId

Suppose, we have 2 record types(Lets say Sales and Support) in Opportunity and when we create an Opportunity through APEX code, we need to populate the record type as Sales. We cannot hard code the record type Id in the code when we create the Opportunity as it varies based on Organization(Sandbox/Prod).

Following code would help to get the desired RecordTypeId:

Map sObjectMap = Schema.getGlobalDescribe();//This returns a map of all sObject names (keys) to sObject tokens (values) for the standard and custom objects defined in your organization.
Map recordType sObjectMap.get(‘Opportunity’).getDescribe().getRecordTypeInfosByName();//From the map sObjectMap, getDescribe which returns the describe result for Opportunity (contains all the describe properties for the sObject Opportunity) and getRecordTypeInfosByName method return maps that associate RecordTypeInfo with record IDs and record labels, respectively for Opportunity.
Id rtId = recordType.get(‘Sales’).getRecordTypeId();//getRecordTypeId returns the ID of the record type ‘Sales’.

There is another way to get the RecordType using SOQL(subjective to SOQL limit) which is as follows:

RecordType RecType = [Select Id From RecordType Where SobjectType = ‘Opportunity’ and DeveloperName = ‘Sales’];

There is a wonderful post in Salesforce Stackexchange about the best approach to get the recordtype id and the link for the same is RecordType Id best approach

Happy coding! Please let me know your comments as always.

Tip#3 – Aggregate functions in SOQL

28 Friday Aug 2015

Posted by Mani in Aggregate functions, Apex, SOQL

≈ Leave a comment

Tags

Aggregate functions in SOQL

Aggregate functions in SOQL:

Aggregate functions in SOQL, such as SUM(),MAX(),COUNT() and AVG() are used to summarise the data in a query.

Suppose,we want to sum the Duration of all the Billable Jobs on a custom object Job, the query can be as follows:
AggregateResult[] groupedResultsBillable;
groupedResultsBillable=[select sum(Duration__c) from Job__c where Job_Billable__c=true];

Note that any query that includes an aggregate function returns its results in an array of AggregateResult objects.

Get the sum of the duration using the following code:
Decimal dur;
for (AggregateResult ar : groupedResultsBillable) //loop through the results
{
dur=(Decimal)ar.get(‘expr0’);
}
Note: Any aggregated field in a SELECT list that does not have an alias automatically gets an implied alias with a format expri, where i denotes the order of the aggregated fields with no explicit aliases. The value of i starts at 0 and increments for every aggregated field with no explicit alias.

Blog Stats

  • 114,771 hits

Archives

Recent Posts: Salesforce Everywhere

How to get field values on onLoad in lightning-record-edit-form in LWC?

Using Dynamic Actions

How to check if the logged in user is a Guest user or not in APEX class?

How to Close Quick action in LWC

Lightning component to filter Products and display results in lightning data table

Connect with me

  • View @manibalan_s’s profile on Twitter
  • LinkedIn

Developer links

  • Salesforce Developer website
  • Salesforce Success Community

Enter your email address and click on Follow us (below) to follow this blog and receive notifications of new posts by email.

Website Powered by WordPress.com.

  • Follow Following
    • Salesforce Everywhere
    • Already have a WordPress.com account? Log in now.
    • Salesforce Everywhere
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar