+ Start a Discussion
Pavan kumar 546Pavan kumar 546 

can any one give me the real time senarios on triggers ..(ie;how can i use it and where i have to use it) pls dont give me defintions..!!

can any one give me the real time senarios on triggers ..(ie;how can i use it and where i have to use it) pls dont give me defintions..!!
Amit Chaudhary 8Amit Chaudhary 8
Please check below post. I hope that will help you with some code and some sample which you can try:-

This trigger adds a related opportunity for each new or updated account if no opportunity is already associated with the account. The trigger first performs a SOQL query to get all child opportunities for the accounts that the trigger fired on. Next, the trigger iterates over the list of sObjects in Trigger.New to get each account sObject. If the account doesn’t have any related opportunity sObjects, the for loop creates one. If the trigger created any new opportunities, the final statement inserts them.
trigger AddRelatedRecord on Account(after insert, after update) {
    List<Opportunity> oppList = new List<Opportunity>();
    // Get the related opportunities for the accounts in this trigger
    Map<Id,Account> acctsWithOpps = new Map<Id,Account>(
        [SELECT Id,(SELECT Id FROM Opportunities) FROM Account WHERE Id IN :Trigger.New]);
    // Add an opportunity for each account if it doesn't already have one.
    // Iterate through each account.
    for(Account a : Trigger.New) {
        System.debug('acctsWithOpps.get(a.Id).Opportunities.size()=' + acctsWithOpps.get(a.Id).Opportunities.size());
        // Check if the account already has a related opportunity.
        if (acctsWithOpps.get(a.Id).Opportunities.size() == 0) {
            // If it doesn't, add a default opportunity
            oppList.add(new Opportunity(Name=a.Name + ' Opportunity',

    if (oppList.size() > 0) {
        insert oppList;
Please let us know if this will help u
Hi Pavan,

Check below link for trigger concept .

http://manojjena20.blogspot.in/2013/03/manojs-trigger-blog.html    (http://manojjena20.blogspot.in/2013/03/manojs-trigger-blog.html)

https://developer.salesforce.com/trailhead/apex_triggers/apex_triggers_intro  (https://developer.salesforce.com/trailhead/apex_triggers/apex_triggers_intro)

Hey basically we are writting triggger in Object it may be standard or custom  .Here you take one realtime example .
Assume we have a insurance claim obejct .Here we have one rule that when you have taken the insurance after 15 days you can claim the insurance .
Here we have two object one is Insurance and other is Insurance Claim .
So when you have created insurance after 15 days you can create claim.I think you are clear now about the requrment .

Try to create two object and add relationship between them and write trigger in child object .

2.Assume a simple example you need to send an email when you will create a account to any one of your company .
3.Write a trigger in Opportunity when your stage will close Own yoou need to add a chile object record related to Opportunity .
try with above requirment and let me know if you need any help !!

Hi Manojjena,

Could you please provide me the solution for the scenario you had mentioned,

Thanks in advance !