Salesforce Q&A Part 4:
Customisation Q&A Continued:
Q) How can we perform a 'callout' using javascript button?
Ans.
Old Way:
button logic
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
if({!AAA__c.Name}!=Null)
{
sforce.apex.execute("MyClass","myMethod",{}"});
alert("This is {!AAA__c.Name}");
}
Apex class logic:
global class MyClass
{
webservice static void myMethod() // you can pass parameters
{
// Do something
}
}
New Way:
Create visualforce button as javascript button is incompatible in SF1 and Lightning.
<apex:page standardController="Case" extensions="EscalCase" action="{!caseEscalation}">
<apex:form>
<apex:inputHidden value="{!case.OwnerId}"/>
</apex:form>
</apex:page>
Apex:
public class EscalCase {
//Apex properties or variables
public Id owner {get; set;}
public Id Id { get; set; }
public Case cas { get; set; }
//constructor to get the Case record
public EscalCase(ApexPages.StandardController controller) {
cas = (Case) controller.getRecord();
Id = cas.Id;
System.debug('The case record: ' + cas);
owner = cas.OwnerId;
}
//Method that can is called from the Visual Force page action attribute
public PageReference caseEscalation() {
System.debug('Case Owner: ' + owner);
System.debug('Case Id: ' + Id);
//build your code logic here
PageReference pageRef = new PageReference('/'+Id);
pageRef.setRedirect(true);
return pageRef; //Returns to the case page
}
}
Q) What is Recursive trigger?
Ans. An event that fires a trigger multiple time is called a recursive trigger. This can be avoided by a static boolean variable in the code.
Q) What is Merge trigger?
Ans. Merge events do not fire their own trigger events. Instead, they fire delete and update events based on deletion of losing records and updation of winning records.
Deletion of losing records
A single merge operation fires a single delete event for all records that are deleted in the merge.
To determine which records were deleted as a result of a merge operation use the MasterRecordId field in Trigger.old.
When a record is deleted after losing a merge operation, its MasterRecordId field is set to the ID of the winning record.
The MasterRecordId field is only set in after delete trigger events.
If your application requires special handling for deleted records that occur as a result of a merge, you need to use the after delete trigger event.
Update of the winning record
A single merge operation fires a single update event for the winning record only.
Any child records that are reparented as a result of the merge operation do not fire triggers.
Q) Scenarios in Circular Trigger.
Ans. Circular triggers are recursive triggers and can be handeled as same way was recursive trigger.
Apex Batch Class:
Q) Why we use batch class in Apex?
Ans. Batch class is used to divide a large volume of records into small chunks in order to avoid governor limit.
Q) What is meant by Transactions?
Ans. Batch class divide large volume of records into small chuncks, each chunk that is processed are called Transactions.
Q) What is the name of the interface to which batch class implements?
Ans. Database.Batchable Interface.
Q) What are methods in batch class?
Ans. There are three methods in batch class.
Start();
Execute();
Finish();
Q) What method is responsible to divide the objects/records into chunks?
Ans. Start() Method.
Q) What is difference between query locator and Iterator difference?
Ans. QueryLocator is used to generate the scope of the object, using QueryLocator helps in bypassing governor limit. In Iterable the governor limit is enforced, mostly used when external objects are queried.
Q) How can we determine batch failed and pass from batachableContext class?
Ans. Using Database.BatchableContext, this object to track the progress of the batch job.
used mostly in finish method of apex batch.
global void finish(Database.BatchableContext BC)
{
AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,TotalJobItems, CreatedBy.Email FROM AsyncApexJob WHERE Id =: BC.getJobId()];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {a.CreatedBy.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Sharing Recalculation ' + a.Status);
mail.setPlainTextBody
('The batch Apex job processed ' + a.TotalJobItems + ' batches with '+ a.NumberOfErrors + ' failures.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
Q) What Key word is used to maintain the Sate of object so that they do not lose value after each transaction?
Ans. Stateful.
Ans.
Old Way:
button logic
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
if({!AAA__c.Name}!=Null)
{
sforce.apex.execute("MyClass","myMethod",{}"});
alert("This is {!AAA__c.Name}");
}
Apex class logic:
global class MyClass
{
webservice static void myMethod() // you can pass parameters
{
// Do something
}
}
New Way:
Create visualforce button as javascript button is incompatible in SF1 and Lightning.
<apex:page standardController="Case" extensions="EscalCase" action="{!caseEscalation}">
<apex:form>
<apex:inputHidden value="{!case.OwnerId}"/>
</apex:form>
</apex:page>
Apex:
public class EscalCase {
//Apex properties or variables
public Id owner {get; set;}
public Id Id { get; set; }
public Case cas { get; set; }
//constructor to get the Case record
public EscalCase(ApexPages.StandardController controller) {
cas = (Case) controller.getRecord();
Id = cas.Id;
System.debug('The case record: ' + cas);
owner = cas.OwnerId;
}
//Method that can is called from the Visual Force page action attribute
public PageReference caseEscalation() {
System.debug('Case Owner: ' + owner);
System.debug('Case Id: ' + Id);
//build your code logic here
PageReference pageRef = new PageReference('/'+Id);
pageRef.setRedirect(true);
return pageRef; //Returns to the case page
}
}
Q) What is Recursive trigger?
Ans. An event that fires a trigger multiple time is called a recursive trigger. This can be avoided by a static boolean variable in the code.
Q) What is Merge trigger?
Ans. Merge events do not fire their own trigger events. Instead, they fire delete and update events based on deletion of losing records and updation of winning records.
Deletion of losing records
A single merge operation fires a single delete event for all records that are deleted in the merge.
To determine which records were deleted as a result of a merge operation use the MasterRecordId field in Trigger.old.
When a record is deleted after losing a merge operation, its MasterRecordId field is set to the ID of the winning record.
The MasterRecordId field is only set in after delete trigger events.
If your application requires special handling for deleted records that occur as a result of a merge, you need to use the after delete trigger event.
Update of the winning record
A single merge operation fires a single update event for the winning record only.
Any child records that are reparented as a result of the merge operation do not fire triggers.
Q) Scenarios in Circular Trigger.
Ans. Circular triggers are recursive triggers and can be handeled as same way was recursive trigger.
Apex Batch Class:
Q) Why we use batch class in Apex?
Ans. Batch class is used to divide a large volume of records into small chunks in order to avoid governor limit.
Q) What is meant by Transactions?
Ans. Batch class divide large volume of records into small chuncks, each chunk that is processed are called Transactions.
Q) What is the name of the interface to which batch class implements?
Ans. Database.Batchable Interface.
Q) What are methods in batch class?
Ans. There are three methods in batch class.
Start();
Execute();
Finish();
Q) What method is responsible to divide the objects/records into chunks?
Ans. Start() Method.
Q) What is difference between query locator and Iterator difference?
Ans. QueryLocator is used to generate the scope of the object, using QueryLocator helps in bypassing governor limit. In Iterable the governor limit is enforced, mostly used when external objects are queried.
Q) How can we determine batch failed and pass from batachableContext class?
Ans. Using Database.BatchableContext, this object to track the progress of the batch job.
used mostly in finish method of apex batch.
global void finish(Database.BatchableContext BC)
{
AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,TotalJobItems, CreatedBy.Email FROM AsyncApexJob WHERE Id =: BC.getJobId()];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {a.CreatedBy.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Sharing Recalculation ' + a.Status);
mail.setPlainTextBody
('The batch Apex job processed ' + a.TotalJobItems + ' batches with '+ a.NumberOfErrors + ' failures.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
Q) What Key word is used to maintain the Sate of object so that they do not lose value after each transaction?
Ans. Stateful.
Comments
Post a Comment