SOQL Advance Features
1. Group by feature:
Salesforce Developers are the aware of with “GROUP BY” word in SQL (Stucture Query Language)/SOQL (Salesforce Object Query Language). This key word returns count of records which are selected.
We can put the count to filter the records. My requirement is that if email is duplicates in contact’s record then I need to exclude those records. Following is the code for that, may it will help you too.
SOQL Code:
AggregateResult[] groupedRs = [Select c.Email from Contact c where email =emailed@gmail.com’ group by Email having count(Email)<2];
Description:
As above SOQL checks the email address and group by Email field count is less than 2. So using the above SOQL we could checks how much records are fetching in result set.
2. Salesforce Object initialization feature:
Up till now process to update any sobject was to first query that object using its Ids, and than we used to update that list.
Now if we are having Id of sobject than we need not do any SOQL. For ex-
Code:
Opportunity tempObj = new Opportunity (id = '0063000000Xh92q');
tempObj.name='Test 123';
Update tempObj;
Description:
This above code will update opportunity with Id=’ '0063000000Xh92q'’. Using this approach we can save many SOQL queries.
Issue – Client want the all new records of an object to submitted to an Approval Process, however you do not want to rely on users to manually click ‘Submit for Approval’ button after creating all records on Object.
Proposed Solution – Got to on Object and Create and test the Approval Process for that go through steps:
Setup > App Setup > Create > Workflow & Approvals > Approval Processes >
Implement the simple Apex Trigger (the following example is for the Account object).
For this we need to write Code on Object and you will also need to create the test method to ensure code coverage of our application. You can also add try / catch statements to ensure that the exceptions are caught and handled properly.
Code of Trigger:
trigger accountApprovalSubmitAction on Account (after insert) {
for (Account a : trigger.new)
{
Approval.ProcessSubmitRequest appAction = new Approval.ProcessSubmitRequest();
appAction.setObjectId(a.id);
Approval.ProcessResult resultAction = Approval.process(appAction);
}
}// END OF TRIGGER
Other Example: We can add some other conditions on after update or after insert.
if(Trigger.isAfter)
{
if (Criteria condition)
{
system.debug('condition is right');
Approval.ProcessSubmitRequest request = new Approval.ProcessSubmitRequest();
req1.setComments('Submitting request for approvalAction.');
req1.setObjectId(Trigger.old[0].Id);
List userId = new List {userInfo.getUserId()};
request.setNextApproverIds(userId);
approval.ProcessResult resultAction = Approval.process(request);
}
Conclusion: Using above code user can insert the records without any operation of Submit for Approval Button and use the Approval process methods according process/logic.
Formula Field Type Conversion
Issue on Formula field type conversion into Text or other field types:
Sales force developers have faced the issue related with Formula field type conversion into other Field Type. Reason behind the issue is there are no configurations available to convert the Formula field type conversion. We can say this is the one the limitation of Salesforce.com and we can not change the type of formulae field.
To avoid the Formula Type conversion:
We can solve the issue like there are 4 to 5 formulae fields are used in page layout which populated the values but earlier requirement was populate the value according to the record created by Partner users. Now the new requirement has changed and these fields populate the value according to the either record created by Standard Users or Partner user.
We could change the formula according to the created by user however there is more than two level hierarchy field. Means we cannot use the field of grandparent standard object fields. For example we need to populate Opportunity’s>Account’s>Contact data. This is not feasible to do populate so we could do this by using of Code.
OLD Formula is Link ID = CreatedBy.Contact.Account.BP_Link_Id__c
Used the following steps to overcome the above issue.
1. We need to create new text fields name like formulae fields. Like Link_Id__c (Formulae) Link_Ids__c (Text), etc.
2. Than write the code in to Trigger and initiate the value in to new fields. For this we do SOQL on Opportunity and Oppty Partners to fetched the partner’s name and other values.
3. Trigger Code:
Map mapOpptyPartner = new Map();
Opportunity opp= new Opportunity();
Map oppMap = new Map([Select Id, Name, OwnerId, Owner.Name from Opportunity WHERE Id in: Trigger. newMap.Id]);
mapOpptyPartner.put(opp.Opportunity__c, opp); //Add Oppty into Map
for(Sale__c saleObj: trigger.new){
if(saleObj.Opportunity_Name__c!=null){
salesObj. field11__c = mapOppPartner.get(salesObj.Opportunity_Name__c).Role__c;
salesObj.field22__c = mapOppPartner.get(salesObj.Opportunity_Name__c). r_name;
salesObj. field33__c = mapOpptyPartner.get(salesObj.Opportunity_Name__c).name;
salesObj. field44__c = mapOppPartner.get(salesObj.Opportunity_Name__c).City;
}}
Description:
Above code fetched all records of the Oppty on Sales record and populated value in these fields.
- After instantiate the new Fields value use the these fields into Formula field and populate the current correct value in to the Formula fields. LikeOld formulae field the value Link ID = CreatedBy.Contact.Account.BP_Link_Id__cNew Value on formulae Link ID = Field11__c; etcConclusion:Hence above steps will solve the issue of Formula conversion in to other field type.FIELD_FILTER_VALIDATION_EXCEPTION errorIssue:DML Exception update failed FIELD_FILTER_VALIDATION_EXCEPTION (You cannot violate field integrity rules) exception in Sales force.Description:I have faced the issue related with FIELD_FILTER_VALIDATION_EXCEPTION on field. It will occur when I update the records through specific Role/Profile.Solution:I have analyzed the issue of field filter validation exception and get the solution. I have logged in with Global Role and update the record in that I have change the value of Look up. This Look up field created and configuration with filter criteria.Before giving more information I want to share some knowledge about Look up field configuration.Look up Options:Related To: User and Child Relationship: SObject means it is related with user object and sObject. For example we have created Look up field on standard/visual force page. When any user clicks on this user it will show the list of User. However we need to show specific users like System Admin only. So we can use look up Filter and write the condition like Profile equal System Admin and save it.Now any user click on Look up them will get list of System Admin users only. Filter Type written some message like Value does not exist or does not match filter criteria and enable this filter.So issue was when any profile user update the record gets FIELD_FILTER_VALIDATION_EXCEPTION error. Because of we have written filter for System Admin only. Means System Admin user only can change the value of the lookup field. Update record we are changing the look up fields value. Hence we get the error on filter validation.Conclusion:Look up filter criteria display the specific users lists like System Admin and these users only change the value of this lookup field other wise you will get FIELD_FILTER_VALIDATION_EXCEPTION error.
0 Comments