Thursday 13 October 2016

Apex - Trigger.old and trigger.new comparison

trigger contacttrigger on contact (before delete) {
    for (customobject__C coj : [SELECT Contactlookup__c
                                      FROM contactrelatedobject__c
                                      WHERE Contactlookup__c
                                      IN :Trigger.oldMap.keySet()]) {
        Trigger.oldMap.get(coj.contactlookup__c).addError(
                   'Cannot delete contact with a contactrelatedobject record');
    }
}

Apex - create master-detail records

master(parent) - account

detail(child) - contact


 Creating a Child Record When a Parent Record is Created

You want to automatically create a new child record when you create a parent record. The child record should be populated with default values from contact.


trigger AutoCreatecontact on account (after insert) {
    List<contact> cons = new List<contact>();

  
    for (account newacc: Trigger.New) {
        if (newacc.customfield__c != null) {
            cons.add(new contact(
                        Name = 'Iain',
                        account = newacc.Id,
                        contactcustomfield__c = newacc.customfield__c,
                        another_contactcustomfield__c = 'Yes'));
        }
    }
    insert cons;
}


Wednesday 12 October 2016

Apex - loops

Loops are used when a particular piece of code should be repeated with desired number of iteration.

FOREACH loops let you repeatedly execute code on every element of a list:

List<Contact> allContacts = [SELECT Id FROM Contact];

for (Contact currentContact : allContacts) {
    

    currentContact.customfield__c = 'customvalue';



FOR loops  lets you repeatedly execute code a specific number of times:


for (Integer i = 0; i < 5; i++) {

    
    Contact newcontact = new Contact();
    newcontact.FirstName = 'Iain';
    newcontact.LastName  = 'Banks #' + i;
    newcontact.customfield__c = 'customvalue';
    insert newcontact;
}

FOR LOOP:
syntax - 
for (variable : list_or_set) { code_block }

List<apex_invoice__c> PaidInvoiceNumberList = new List<apex_invoice__c>();
//initialising list of object records 


PaidInvoiceNumberList = [SELECT Id,Name, APEX_Status__c FROM APEX_Invoice__c  WHERE CreatedDate = today]; 
//SOQL query to find filter records


List<string> InvoiceNumberList = new List<string>();
//list to store paid invoice


for (APEX_Invoice__c objInvoice: PaidInvoiceNumberList) { 

//loop iterates (executes repeatedly) on the PaidInvoiceNumberList and will process the each record (objInvoice)

if (objInvoice.APEX_Status__c == 'Paid') { 
//this if condition will check each record for the boolean condition



InvoiceNumberList.add(objInvoice.Name);
//if Status value is paid then it will add the invoice number into List of String
}
}

SOQL FOR LOOP:
This type of for loop is used when we don't want to create the List and directly iterate over the returned set of records of SOQL query.


Syntax - 
for (variable : [soql_query]) { code_block }
OR
for (variable_list : [soql_query]) { code_block }

the variable_list or variable should always be of same type as the records returned by the Query

List<apex_invoice__c> PaidInvoiceNumberList = new List<apex_invoice__c>();
//initialising list of object records 

List<string> InvoiceNumberList = new List<string>();
//list to store paid invoice

for (APEX_Invoice__c objInvoice: [SELECT Id,Name, APEX_Status__c FROM APEX_Invoice__c  WHERE CreatedDate = today]) { 

//this loop will iterate and will process the each record returned by the Query

if (objInvoice.APEX_Status__c == 'Paid') { 
//this if condition will check each record for the boolean condition



InvoiceNumberList.add(objInvoice.Name);
//if Status value is paid then it will add the invoice number into List of String
}
}

JAVA FOR LOOP:
Syntax - 
for (init_stmt; exit_condition; increment_stmt) { code_block }

List<apex_invoice__c> PaidInvoiceNumberList = new List<apex_invoice__c>();
//initialising list of object records 

List<string> InvoiceNumberList = new List<string>();
//list to store paid invoice

for (Integer i = 0; i < paidinvoicenumberlist.size(); i++) { 

//this loop will iterate on the List PaidInvoiceNumberList and will process the each record. It will get the List Size and will iterate the loop for number of times that size. For example, list size is 10.

if (PaidInvoiceNumberList[i].APEX_Status__c == 'Paid') { 
//this if condition will check each record for the boolean condition



InvoiceNumberList.add(PaidInvoiceNumberList[i].Name);
//if Status value is paid then it will add the invoice number into List of String
}

}

WHILE LOOP:
Syntax - 
while (Boolean_condition) { execute_code_block }

//Fetch 20 records from database
List<apex_invoice_c> InvoiceList = [SELECT Id, APEX_Description_c, APEX_Status_c FROM APEX_Invoice_c LIMIT 20];
Integer i =1;
//Update ONLY 10 records
while (i< 10) {
InvoiceList[i].APEX_Description__c = 'This is the '+i+'Invoice';

i++;

}

DO WHILE LOOP:
syntax - 
do { code_to_execute } while (Boolean_condition);


//Code for do while loop
List<apex_invoice__c> InvoiceList = [SELECT Id, APEX_Description__c, APEX_Status__c FROM APEX_Invoice__c LIMIT 20];//it will fetch only 20 records
Integer i =0;
do {
InvoiceList[i].APEX_Description__c = 'This is the '+i+' Invoice';

i++;//Increment the counter
}while (i< 1); 

//iterate till 1st record only