Saturday 23 November 2013

Salesforce - Buttons to Mass delete records / Get ids / Show google maps / Create a new record - URL hack /Clone a record - URL Hack/ insert a complete record - onclick javascript

Mass Delete


This example creates a button that can be added to activity related lists and list views, and allows users to delete selected records at the same time.
1 Define a button for events with these atributes:
Display Type─List Button
behavioue - execute javascript
content source - onclick javascript
add this code:
{!REQUIRESCRIPT("/soap/ajax/9.0/connection.js")}
var records = {!GETRECORDIDS( $ObjectType.Event )};
var taskRecords = {!GETRECORDIDS( $ObjectType.Task)};
records = records.concat(taskRecords);

if (records[0] == null) {
alert("Please select at least one record.") }
else {
var errors = [];
var result = sforce.connection.deleteIds(records);
if (result && result.length){
var numFailed = 0;
var numSucceeded = 0;
for (var i = 0; i < result.length; i++){
var res = result[i];
if (res && res.success == 'true'){
numSucceeded++;
} else {
var es = res.getArray("errors");
if (es.length > 0) {
errors.push(es[0].message);
}
numFailed++;
}
}
if (numFailed > 0){
alert("Failed: " + numFailed + "\nSucceeded: " + numSucceeded + " \n Due to: " + errors.join("\n"));
} else {
alert("Number of records deleted: " + numSucceeded);
}
}
window.location.reload();
}

2 add button to activity list views
3 add button to  to any page layout that contains an activity related list. The button deletes any selected task or event in the list.

Getting Record IDs

This example creates a button that opens a popup window listing record IDs for user selected records. This is useful when testing to ensure you have the correct record IDs before processing them further.

1 Define a button for events with these atributes:
Display Type─List Button
behavioue - execute javascript
content source - onclick javascript
add this code:
idArray = {!GETRECORDIDS($ObjectType.Contact)};
alert("The Ids you have selected are: "+idArray);

2 add button to  to the appropriate related list on a page layout or list view layout.

Get Google Map

This example creates a button that opens a new window showing a google map.  This is useful for anyone wanting to make a site visit.

1 define a new button at customise - accounts - actions links buttons with these attributes:
Display Type─Detail Page Button
behavioue - display in new window
content source - URL
add this code:

http://maps.google.com/maps?q={!Account_BillingStreet}%20{!Account_BillingCity}%20{!Account_BillingState}%20{!Account_BillingPostalCode}
2 add button too account page layout

Create a new record - url hack

1 Define a button for events with these atributes:
Display Type─List Button
behavioue - display in exiting window without sidebar or header
content source - URL

2 Define the URL as below:

the code to insert will essentially open the edit pagelayout for the object you want to create a new record with some values prepopulated from another related record.  This is usefull to save the user some time linking records correctly.

the url doesn't need the “https://na15.salesforce.com/” so the url starts after the / with “/a0U/e” - a0U is an object code and e represents edit so to find this simply push edit on on a relevant record and check the url in the browser whilst on the edit page (https://na15.salesforce.com/a0U/e?) you dont need the "?"

/a0U/e

so this is now good enough to place on a related list via the page layout editor but you will want to add values

to add values you will need to add the ? its used as a seperator

now goto setup of the object you are creating a record for and click on the field you want to preopulate and grab the field id from the url, you will then write in your field id and objectname.fieldname like this

/a0U/e?00Ni000000EpsgY={!Opportunity.Description}

for a lookup field you add CF to the front of the id

CF00Ni000000EpsgO

as this is a lookup we add the relationship parameter  add “_lkid” to the end of the id

/a0U/e?CF00Ni000000EpsgO={!Opportunity.Name}&CF00Ni000000EpsgO_lkid={!Opportunity.Id}

notice & is used to seperate fields

/a0U/e?00Ni000000EpsgY={!Opportunity.Description}&CF00Ni000000EpsgO={!Opportunity.Name}&CF00Ni000000EpsgO_lkid={!Opportunity.Id}

finally add a return url incase user cancels midway or on the save of the record

&retURL={!Opportunity.Id}  this will take yu back to the original record where you have placed your button
/a0U/e?00Ni000000EpsgY={!Opportunity.Description}&CF00Ni000000EpsgO={!Opportunity.Name}&CF00Ni000000EpsgO_lkid={!Opportunity.Id}&retURL={!Opportunity.Id} 

Clone a record - URL Hack

Here's an example for cloning a user record:
/{!User.Id}/e?clone=1&retURL=%2F{!User.Id}&name_firstName=&name_lastName=&Alias=&Email=&Username=&CommunityNickname=

you can see that by leaving some fields blank (eg. name_firstName=)  that you will not copy that fields value into the cloned record

insert a complete record onclick javascript


a - button to create a record
b - button to create a related child record
c - button to create parent and related child record

button to create a record
1 Define a button for events with these atributes:
Display Type─List Button
behavioue - Execute JavaScript

content source - onclick

2 define the javascript code:

you must include the libraries so add the following on the first line

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}

you'll need to create an object variable to hold our data. for example “acct”, as an Account record.

var acct = new sforce.SObject("Account");

now just populate the fields on the object like so

acct.name = 'New Account';
acct.phone = '515-123-4567';

Now that I have all my fields defined, I’m ready to save the new record.

var result = sforce.connection.create([acct]);

last is 

if(result[0].getBoolean("success")){
window.location = "/" + result[0].id + "/e";
}else{
alert('Could not create record '+result);

}


button to create a related child record if you are creating a child record you will have to identify the lookupfield to the parent

var parent = new sforce.SObject(“parent__c”);
parent.id = “{!lookuptoparentfromchildfield__c.Id}”;

create the child

var child = new sforce.SObject(“child__c”);

set field values for child

child.lookuptoparentfromchildfield__c = parent.id;
child.childfield2__c = “Test”;

save record

var result = sforce.connection.create([child]);

verify result

// verify the results
if(result[0].getBoolean(“success”)){
window.location = “/” + result[0].id + “/e”;
}else{
alert(‘Could not create record ‘+result);
}

button to create parent and related child record
you could combine both into one button like this:

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
var parent = new sforce.SObject("parent__c");
var start = new Date;

parent.name = 'parentname';
parent.account_name__c = "{!Account.Id}";
parent.contact_name__c = "{!Contact.Id}";
parent.Due_Date__c = new Date(start.setDate(start.getDate() + 1));
parent.stage__c = "New";
parent.currencyisocode = 'GBP';

var resultparent = sforce.connection.create([parent]);

if(resultparent[0].getBoolean("success")){
 var newparentId = resultparent[0].id;
 var child = new sforce.SObject("child__c"); 

child.easy_opportunity__c = newparentId; 
 child.Sale_Price__c = "0.00"; 
 child.Status__c = "New"; 
 child.Name = "childname"; 
 child.CurrencyIsoCode = 'GBP'; 

 var resultchild = sforce.connection.create([child]); 

 if(resultchild[0].getBoolean("success")){ 
  window.location = "/" + newparentId; 
 }else{ 
  alert('Could not create record '+resultchild); 
 }
}else{
 alert('Could not create record '+resultparent);

}

No comments:

Post a Comment