Skip to main content

I am practicing building triggers in Salesforce. I have a trigger that works in creatingcreates an opportunity record automatically when an account record's Type field is "Prospect". How How can I take this trigger further by limiting the number of opportunities created to just one? For instance, every time a person updates the account record to "Prospect", it will create a new opportunity which I do not want since one was created already.

trigger createOppty on Account (after update) {
    list <Opportunity> Opptylist = new list <Opportunity>();

    for (Account o : trigger.new){
 
        if (o.Type == 'Prospect') {
 
            Opportunity Oppty = new Opportunity(
                AccountId = o.Id,
                Name = 'Edit Opportunity',
                CloseDate = system.today(),
                StageName = 'Prospecting');
            Opptylist.add(Oppty);

        }
        insert Opptylist;
 
     }
}

I am practicing building triggers in Salesforce. I have a trigger that works in creating an opportunity record automatically when an account record's Type field is "Prospect". How can I take this trigger further by limiting the number of opportunities created to just one? For instance, every time a person updates the account record to "Prospect", it will create a new opportunity which I do not want since one was created already.

trigger createOppty on Account (after update) {
    list <Opportunity> Opptylist = new list <Opportunity>();

    for (Account o : trigger.new){
 
        if (o.Type == 'Prospect') {
 
            Opportunity Oppty = new Opportunity(
                AccountId = o.Id,
                Name = 'Edit Opportunity',
                CloseDate = system.today(),
                StageName = 'Prospecting');
            Opptylist.add(Oppty);

        }
        insert Opptylist;
 
    }
}

I am practicing building triggers in Salesforce. I have a trigger that creates an opportunity record automatically when an account record's Type field is "Prospect". How can I take this trigger further by limiting the number of opportunities created to just one? For instance, every time a person updates the account record to "Prospect", it will create a new opportunity which I do not want since one was created already.

trigger createOppty on Account (after update) {
    list <Opportunity> Opptylist = new list <Opportunity>();

    for (Account o : trigger.new){
        if (o.Type == 'Prospect') {
            Opportunity Oppty = new Opportunity(
               AccountId = o.Id,
               Name = 'Edit Opportunity',
               CloseDate = system.today(),
               StageName = 'Prospecting');
            Opptylist.add(Oppty);

        }
        insert Opptylist;
     }
}

I am practicing building triggers in Salesforce. I have a trigger that works in creating an opportunity record automatically when an account record's Type field is "Prospect". How can I take this trigger further by limiting the number of opportunities created to just one? For instance, every time a person updates the account record to "Prospect", it will create a new opportunity which I do not want since one was created already.

trigger createOppty on Account (after update) {
    list <Opportunity> Opptylist = new list <Opportunity>();

    for (Account o : trigger.new){

        if (o.Type == 'Prospect') {

            Opportunity Oppty = new Opportunity(
                AccountId = o.Id,
                Name = 'Edit Opportunity',
                CloseDate = system.today(),
                StageName = 'Prospecting');
            Opptylist.add(Oppty);

        }
        insert Opptylist;

    }
}

I am practicing building triggers in Salesforce. I have a trigger that works in creating an opportunity record automatically when an account record's Type field is "Prospect". How can I take this trigger further by limiting the number of opportunities created to just one? For instance, every time a person updates the account record to "Prospect", it will create a new opportunity which I do not want since one was created already.

trigger createOppty on Account (after update) {
list <Opportunity> Opptylist = new list <Opportunity>();

for (Account o : trigger.new){

if (o.Type == 'Prospect') {

Opportunity Oppty = new Opportunity(
AccountId = o.Id,
Name = 'Edit Opportunity',
CloseDate = system.today(),
StageName = 'Prospecting');
Opptylist.add(Oppty);

}
insert Opptylist;

}
}

I am practicing building triggers in Salesforce. I have a trigger that works in creating an opportunity record automatically when an account record's Type field is "Prospect". How can I take this trigger further by limiting the number of opportunities created to just one? For instance, every time a person updates the account record to "Prospect", it will create a new opportunity which I do not want since one was created already.

trigger createOppty on Account (after update) {
    list <Opportunity> Opptylist = new list <Opportunity>();

    for (Account o : trigger.new){

        if (o.Type == 'Prospect') {

            Opportunity Oppty = new Opportunity(
                AccountId = o.Id,
                Name = 'Edit Opportunity',
                CloseDate = system.today(),
                StageName = 'Prospecting');
            Opptylist.add(Oppty);

        }
        insert Opptylist;

    }
}
Source Link
Paul
  • 71
  • 1
  • 10

Limiting number of records created through Salesforce trigger

I am practicing building triggers in Salesforce. I have a trigger that works in creating an opportunity record automatically when an account record's Type field is "Prospect". How can I take this trigger further by limiting the number of opportunities created to just one? For instance, every time a person updates the account record to "Prospect", it will create a new opportunity which I do not want since one was created already.

trigger createOppty on Account (after update) {
list <Opportunity> Opptylist = new list <Opportunity>();

for (Account o : trigger.new){

if (o.Type == 'Prospect') {

Opportunity Oppty = new Opportunity(
AccountId = o.Id,
Name = 'Edit Opportunity',
CloseDate = system.today(),
StageName = 'Prospecting');
Opptylist.add(Oppty);

}
insert Opptylist;

}
}