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
Shubham SengarShubham Sengar 

trigger problem 3


trigger Oppsame on Contact(After insert) {

    List <Opportunity> Opp = new List <Opportunity>();

    For (Contact C: Trigger.new){

    Opportunity O = new Opportunity();

   O.Name = C.Name;

    O.CloseDate = System.TODAY();

    O.StageName = 'Closed Won';
     O.AccountId   = C.id;
    Opp.add(O);

    }
 Insert Opp;
}

when we insert contact there will a insert a Opportunity for this contact

its give insert time errror ..
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger Oppsame caused an unexpected exception, contact your administrator: Oppsame: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Account ID: id value of incorrect type: 00328000005GxcuAAC: [AccountId]: Trigger.Oppsame: line 18, column 1
 
Best Answer chosen by Shubham Sengar
Kiran kumar 193Kiran kumar 193
Hi Sengar,

Modify trigger like this..

trigger Oppsame on Contact(After insert) {

    List <Opportunity> Opp = new List <Opportunity>();

    For (Contact C: Trigger.new){

    Opportunity O = new Opportunity();

   O.Name = C.Name;

    O.CloseDate = System.TODAY();

    O.StageName = 'Closed Won';
     O.AccountId   = C.Accountid;
    Opp.add(O);

    }
 Insert Opp;
}

All Answers

William TranWilliam Tran
Change this 
  O.AccountId   = C.id;

to

  O.AccountId   = C.Accountid;
Kiran kumar 193Kiran kumar 193
Hi Sengar,

Modify trigger like this..

trigger Oppsame on Contact(After insert) {

    List <Opportunity> Opp = new List <Opportunity>();

    For (Contact C: Trigger.new){

    Opportunity O = new Opportunity();

   O.Name = C.Name;

    O.CloseDate = System.TODAY();

    O.StageName = 'Closed Won';
     O.AccountId   = C.Accountid;
    Opp.add(O);

    }
 Insert Opp;
}
This was selected as the best answer
Shubham SengarShubham Sengar
hey William Tran and Kiran
still its giving error 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger Oppsame caused an unexpected exception, contact your administrator: Oppsame: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Opportunity Name]: [Opportunity Name]: Trigger.Oppsame: line 18, column 1


trigger Oppsame on Contact(After insert) {

    List <Opportunity> Opp = new List <Opportunity>();

    For (Contact C: Trigger.new){

    Opportunity O = new Opportunity();

   O.Name = C.Name;

    O.CloseDate = System.TODAY();

    O.StageName = 'Closed Won';
     O.AccountId   = C.Accountid;
    Opp.add(O);

    }
 Insert Opp;
}
William TranWilliam Tran
Change this:

O.Name = C.Name;

to this:

O.Name = C.LastName;

Thx
Shubham SengarShubham Sengar
thanks william now its not giving any error but Opportunity is blank no any record there..
trigger Oppsame on Contact(After insert) {

    List <Opportunity> Opp = new List <Opportunity>();

    For (Contact C: Trigger.new){

    Opportunity O = new Opportunity();
O.Name = C.LastName;

    O.CloseDate = System.TODAY();

    O.StageName = 'Closed Won';
     O.AccountId   = C.Accountid;
    Opp.add(O);

    }
 Insert Opp;
}
William TranWilliam Tran
How are you checking that the Opportunity is blank no records?

Do this:

1)Check to make sure you don't have an validation rules on the opportunity object that stop the trigger from being successful.

Disable all validation rules on the opportunity if you have too.

2) When checking the opportunity (after creating a contact), make sure to use "New this week" filter list to get the newly created opportunities.

Thx
Shubham SengarShubham Sengar
hi William,ya  m follow with ur code n disable all validation ...but still no opportunity is coming ....
William TranWilliam Tran
Shubham, it works for me.  

Do you have custom required fields for opportunities or work flow/process builder causing issues?'

Thx
Shubham SengarShubham Sengar
hi william
ya here no any custom required fields for opportunities or work flow/process
i think in this line 

 O.AccountId   = C.Accountid;
 some thing is worng may be ??

 
Shubham SengarShubham Sengar
Actully m just thinking why we use C.Accountid 
bcoz u r using contact object then y we use accountid