function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ranadheer chRanadheer ch 

i have written this trigger to create opportunity once we create the account on account object i have written this trigger to create opportunity once we create the account on account object

i have written this trigger to create opportunity once we create the account on account object
i have written this trigger to create  opportunity once we create the account  on account object if    account.create opportunity=true ..what i mean is there is a check box field in account ...if that is equls true then only opptunity should get created ..here is the code


trigger Opportunitycreate on Account (after insert) {
    List<opportunity>Listopp = New List<opportunity>();
    for(account acc:trigger.new){
       if(acc.Create_Contact__c==True){
        Opportunity opp = New opportunity();
        opp.account.name = acc.name;
        opp.name=acc.name;
        opp.CloseDate=date.Today();
        opp.StageName='Prospecting';
        Listopp.add(opp);
       }
       }
    if(Listopp.size()>0)
    insert Listopp;
}

am getting error while adding record in accounts objcet ...the error is in line 6...
help me please..thanks in advance.

User-added image

Best Answer chosen by Ranadheer ch
Arunkumar RArunkumar R
Hi rana,

Use the below modified code, it will work.

trigger Opportunitycreate on Account (after insert) {
    List<opportunity>Listopp = New List<opportunity>();
    for(account acc:trigger.new){
       if(acc.Create_Contact__c==True){
        Opportunity opp = New opportunity();
        opp.accountid = acc.id;
        opp.name=acc.name;
        opp.CloseDate=date.Today();
        opp.StageName='Prospecting';
        Listopp.add(opp);
       }
       }
    if(Listopp.size()>0)
    insert Listopp;
}

All Answers

Arunkumar RArunkumar R
Hi rana,

Use the below modified code, it will work.

trigger Opportunitycreate on Account (after insert) {
    List<opportunity>Listopp = New List<opportunity>();
    for(account acc:trigger.new){
       if(acc.Create_Contact__c==True){
        Opportunity opp = New opportunity();
        opp.accountid = acc.id;
        opp.name=acc.name;
        opp.CloseDate=date.Today();
        opp.StageName='Prospecting';
        Listopp.add(opp);
       }
       }
    if(Listopp.size()>0)
    insert Listopp;
}
This was selected as the best answer
Ranadheer chRanadheer ch
Thanks Arun.