Create and update records using Javascript button in Salesforce- Ajax Toolkit

On this blog we have already seen “how to use Ajax Toolkit to mass update records” without writing any Apex code. In this article as well, we will use Ajax Toolkit to add custom button in Account page layout and update record.

There are endless possibilities and use-cases where this approach can be very useful in Salesforce projects and can be accomplished by Salesforce admins. Lets say we want to create child record on click of button, Update current record on basis of some logic, Update hidden fields, execute existing Apex code using Javascript and so on. All of these can be implemented wthout writing single line of Apex code, Trigger or Visualforce pages.

In this post, we will add custom button on Account detail page and execute Javascript to update Account name using Ajax toolkit. This is just proof of concept and any other field can be updated.

Creating Custom button :

Navigate to “Customize | Accounts | Button, Links and Actions”.

Create new button of type “Detail Page Button” , behavior “Execute Javascript” and content source “Onclick JavaScript”.

Update Record using Javascript

Update Record using Javascript

Continue reading “Create and update records using Javascript button in Salesforce- Ajax Toolkit”

Apex Interview Question – Salesforce – Part 16

151. Give Sample Code Snippet of Apex that that will show that how Parent and Child record can be inserted in Single Statement ?
Ans : It can be done with help of External Id.

Date dt = Date.today().addDays(7);
Opportunity newOpportunity = new Opportunity(Name = 'shivasoft', StageName = 'Prospecting', CloseDate = dt);

/*
Create the parent reference. Used only for foreign key reference  and doesn't contain any other fields. If we provide any other value it will give following error

System.DmlException: Insert failed. First exception on row 1; first error: INVALID_FIELD, More than 1 field provided in an external foreign key reference in entity: Account: []
*/

Account accountReference = new Account(MyExtID__c = 'SHIVA1234567');
newOpportunity.Account = accountReference;

//  Create the Account object to insert.  Same as above but has Name field.  Used for the insert.
Account parentAccount = new Account(Name = 'Shiva', MyExtID__c = 'SHIVA1234567');

Database.SaveResult[]
    results = Database.insert(new SObject[] {  parentAccount, newOpportunity });


152 . Which SOQL statement can be used to get all records even from recycle bin or Achieved Activities?
Ans : We will need “ALL Rows” clause of SOQL.
Sample :

SELECT COUNT() FROM Contact WHERE AccountId = a.Id ALL ROWS

153. How can you lock record using SOQL so that it cannot be modified by other user.
Ans : we will need “FOR UPDATE” clause of SOQL.
Sample :

 Account [] accts = [SELECT Id FROM Account LIMIT 2 FOR UPDATE];

Continue reading “Apex Interview Question – Salesforce – Part 16”

Create Simple Mass Delete Button for ListView using Salesforce Ajax Toolkit

In Many situation, I needed a Mass Delete Kind of button. Where, i can select records in ListView and simply click “Delete” button.

In this example, we will create a simple List View button and add on “Search Layout” of that object.

Salesforce Mass Delete Button - List View
Salesforce Mass Delete Button – List View

So, create a new Custom Button with following property:

  1. Display Type :  List Button
  2. Behavior : Execute JavaScript
  3. Content Source :  OnClick JavaScript

and use below Source code: Continue reading “Create Simple Mass Delete Button for ListView using Salesforce Ajax Toolkit”