Monday, August 24, 2015

Recursive triggers handling salesforce



Helper class

----------------------------------------------------------------------------------------------------------------


public class recursioncontrol
{

public static boolean flag=True;

}


-----------------------------------------------------------------------------------------------------------------


Trigger

-----------------------------------------------------------------------------------------------------------------


trigger firsttrigggercontact on Contact (before insert, before update,before delete,after insert,after update,after delete) {



List<Contact> c1=trigger.new;

List<Contact> c2=trigger.old;

if(trigger.isbefore)
{
if(trigger.isinsert)
{
system.debug('%%%%%%%%%%%%am from  before insert%%%%%%%%%%%%%'+c1);
}


if(trigger.isUpdate)
{

system.debug('%%%%%%%%%%%%am from  before update%%%%%%%%%%%%%'+c1);
system.debug('%%%%%%%%%%%%am from  before update%%%%%%Old Values%%%%%%%'+c2);
}


if(trigger.isDelete)
{
system.debug('%%%%%%%%%%%%am from before delete%%%%%%%%%%%%%'+c1);

system.debug('%%%%%%%%%%%%am from  before update%%%%%%%%%%%%%'+c1);
system.debug('%%%%%%%%%%%%am from  before update%%%%%%Old Values%%%%%%%'+c2);
}

}





if(trigger.isafter)
{
if(trigger.isinsert)
{

List<contact> c4=new List<contact>();
contact c5=new contact();
for(contact c3:trigger.new)
{
if(recursioncontrol.flag){
c5.LastName=c3.LastName;
c4.add(c5);
}
recursioncontrol.flag=false;
}
insert c4;

system.debug('%%%%%%%%%%%%am from  after insert%%%%%%%%%%%%%');
}


if(trigger.isUpdate)
{
system.debug('%%%%%%%%%%%%am from  after insert%%%%%%%%%%%%%');

}


if(trigger.isDelete)
{
system.debug('%%%%%%%%%%%%am from  after insert%%%%%%%%%%%%%');

}

}


}

Explaining Trigger events

trigger updatefields on Account (before insert,before update,before delete,after insert,after update,after delete) {



List<account> ac=trigger.new;


List<account> ac1=trigger.old;
if(trigger.isbefore)
{
if(trigger.isinsert )
{


system.debug('333$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$am from insert###################new Data'+ac);

//system.debug('333$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$am from insert###################old Data'+ac1);
For(Account ac3:ac)
{
ac3.Name='Mr.' +ac3.Name;


}



}

if(trigger.isupdate)

{

system.debug('333$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$am from update###################new Data'+ac);
system.debug('333$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$am from update###################old Data'+ac1);


For(Account ac3:ac)
{
if(!ac3.Name.contains('Mr'))
{
ac3.Name='Mr.' +ac3.Name;
}

}


}

if(trigger.isdelete)
{
system.debug('333$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$am from delete');


}
}

if(trigger.isafter)
{
List<contact> con1=new List<contact>();
if(trigger.isinsert)
{
for(account ac6:trigger.new)
{

contact c1=new contact();


c1.lastname=ac6.Name;

con1.add(c1);



}
insert con1;
}
}










}

salesforce trigger error message

Salesforce provides validation rules in configuration for standard as well as custom objects. Validation rule for standard object can be written by navigating to following:
set up --> customise --> standard object name --> validation rule

similarly for custom object you can write validation rule by navigating to following:
set up --> create --> objects --> click on the custom object --> scroll down and click on new(next to validation Rules)

Although, you can write validation using simple configuration, sometimes your requirements may not be fullfilled using validation rule especially when your validation criteria is bit complex or need querying in database to check previously created data. In such a case you can write your logic in trigger.

Lets write down a very basic trigger that will throw a validation errror message.

Scenario : Show error message on account if annual revenue is less than 2000.

The error message can be shown using adderror method as shown in the example below:

trigger validation_using_Trigger on Account (before insert, before update) {
 for(Account acc:trigger.new){
    if(acc.AnnualRevenue < 2000){
       acc.adderror('Annual revenue cannot be less than 2000');
    }
 }
}












message at the top of the page. You can also display your message at a particular field, you only have to mention the field name in the adderror method as in the below example:

trigger validation_using_Trigger on Account (before insert, before update) {
 for(Account acc:trigger.new){
    if(acc.AnnualRevenue < 2000){
       acc.AnnualRevenue.adderror('Annual revenue cannot be less than 2000');
    }
 }
}





Above triggers will also throw validation errors while inserting/updating records using data loader. 

While writing validation you have to make sure that your trigger is bulkified; that is you are properly iterating over for loop(trigger.new)

Also your trigger should run over before insert and before update contexts as you want the message be displayed before the record is created or updated. 

Also the adderror method should always be written in the trigger new context. Trigger will not show error message if you are iterating over some other collection which is not trigger.new 


Thursday, August 13, 2015

Salesforce Triggers

Trigger is piece of code that is executes before and after a record is Inserted/Updated/Deleted from the force.com database.
Syntax:
Trigger <trigger name> on <Object name> (trigger Events) {
// Implement the Logic here
}
Types of Triggers:
– Before Triggers
– After Triggers

Before Trigger: Before triggers are used to perform the logic on the same object and specifically we cannot use the DML operation (Insert, update, delete) on these triggers.
These triggers are fired before the data is saved into the database.
After Trigger: After triggers are used to perform the logic on the related objects and these triggers are used access the fields values that are created by system (Ex: CreatedBy, LasteModifiedBy , Record Id etc..).
Bulk Triggers:
By default every trigger is a bulk trigger which is used to process the multiple records at a time as a batch. For each batch of 200 records.
Trigger Context Variables:
All the trigger context variables are prefixed with “Trigger.” (Ex: Trigger.isInsert, etc..)
isInsert: Returns true if the trigger was fired due to insert operation.
isUpdate: Returns true if the trigger was fired due to update operation.
isDelete: Returns true if the trigger was fired due to delete operation.
isBefore: Returns true if the trigger was fired before record is saved.
isAfter: Returns true if the trigger was fired after record is saved.
New: Returns a list of new version of sObject records.
Old: Returns a list of old version of sObject records.
NewMap: Returns a map of new version of sObject records. (map is stored in the form of map<id,newRecords>)
OldMap: Returns a map of old version of sObject records. (map is stored in the form of map<id,oldRecords>)
Size: Returns a integer (total number of records invoked due to trigger invocation for the both old and new)
isExecuting: Returns true if the current apex code is a trigger.
The below table is tells what are the events we can use in the new trigger and old trigger
Trigger EventTrigger.NewTrigger.Old
Before InsertYesNo
Before UpdateYesYes
Before DeleteNoYes
Before UnDeleteNoYes
After InsertYesNo
After UpdateYesYes
After DeleteNoYes
Trigger Context Variable considerations:
– Trigger.Old is always readOnly
– We cannot delete trigger.new
– In before triggers, trigger.new can be used to update the fields on the same object.
– In After trigger, we get run time exception is thrown when user try to modify the fields in the same object.

Error codes for http requests

HTTP response codeDescription
200“OK” success code, for GET or HEAD request.
201“Created” success code, for POST request.
204“No Content” success code, for DELETE request.
300The value returned when an external ID exists in more than one record. The response body contains the list of matching records.
304The request content has not changed since a specified date and time. The date and time is provided in a If-Modified-Since header. See Get Object Metadata Changes for an example.
400The request couldn’t be understood, usually because the JSON or XML body contains an error.
401The session ID or OAuth token used has expired or is invalid. The response body contains the message anderrorCode.
403The request has been refused. Verify that the logged-in user has appropriate permissions.
404The requested resource couldn’t be found. Check the URI for errors, and verify that there are no sharing issues.
405The method specified in the Request-Line isn’t allowed for the resource specified in the URI.
415The entity in the request is in a format that’s not supported by the specified method.
500An error has occurred within Force.com, so the request couldn’t be completed. Contact Salesforce Customer Support.

Friday, July 31, 2015

Status=Length Required, StatusCode=411 error in Apex callout

HttpRequest request = new HttpRequest();request.setHeader('Content-Length', '512'); 

Generate Json Data



Here is Simple classs to form json data..

public class ParentRecord {
    public String recordType, entity, job;
    public ChildRecord[] item;
    public ParentRecord(String recordType, String entity, String job) {
        this.recordType = recordType;
        this.entity = entity;
        this.job = job;
        item = new ChildRecord[0];
    }
}

public class ChildRecord {
    public String item;
    public Decimal quantity, amount;
    public ChildRecord(String item, Decimal quantity, Decimal amount) {
        this.item = item;
        this.quantity = quantity;
        this.amount = amount;
    }
}


pass paramenters to the above class using below code.



// Query parent and children, you can do both at once.
// Replace object names, relationship names, and field names.
Parent record = [SELECT Id, recordType, entity, job, (SELECT Id, Item, quantity, amount FROM Children) FROM Parent WHERE Id = :someId];

// Create a parent record entry from the utility class
ParentRecord parent = new ParentRecord(record.recordType, record.Entity, record.job);
// Loop through and build the item list
for(Child lineItem: record.Children) {
    parent.item.add(new ChildRecord(lineItem.Item, lineItem.quantity, lineItem.Amount));
}



Now convert the data into json using JSON.serialize() method
String jsondatagen= JSON.serialize(parent);