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
Nitin SharmaNitin Sharma 

I create a trigger to add related opportunity record on account object if there is no opportunity.but when i create a account its gives me a error .what does it mean

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger Addreletedrecord caused an unexpected exception, contact your administrator: Addreletedrecord: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.Addreletedrecord: line 8, column 1
Best Answer chosen by Nitin Sharma
Abhishek BansalAbhishek Bansal
Hi Nitin,

As you are using update on records which are not existing in the database so the Id for update is not identified and as a result you are getting error.
Please replace the update statement with "UPSERT".
Upsert will automatically detects wheteher a record is inserted or updated
So change your code with below code :
trigger Addreletedrecord on Account (after update, after insert) {
List<Opportunity> oppList = new List<Opportunity>();
for(Account a:[SELECT Id,Name from Account where Id IN:Trigger.new AND Id NOT IN(SELECT AccountId from Opportunity)]){

    oppList.add(New Opportunity(Name= a.Name+'opportunity',StageName='Prospecting',AccountId=a.Id));
    }
    If(oppList.Size()>0){
        upsert oppList;
       
        }

}

Let me know if you have any other issue.

Thanks,
Abhishek

All Answers

Abhishek BansalAbhishek Bansal
Hi Nitin,

There is an error in your trigger code where you are trying to update some object.
Maybe you are no passing the correct Id of object which you are trying to update.
It would be easy to help you if you post your trigger code here.
Without seeing your code it is not possible to help you out.
So please share your code.

Thanks,
Abhishek
Nitin SharmaNitin Sharma
trigger Addreletedrecord on Account (after update, after insert) {
List<Opportunity> oppList = new List<Opportunity>();
for(Account a:[SELECT Id,Name from Account where Id IN:Trigger.new AND Id NOT IN(SELECT AccountId from Opportunity)]){

    oppList.add(New Opportunity(Name= a.Name+'opportunity',StageName='Prospecting',AccountId=a.Id));
    }
    If(oppList.Size()>0){
        Update oppList;
       
        }

}
Abhishek BansalAbhishek Bansal
Hi Nitin,

As you are using update on records which are not existing in the database so the Id for update is not identified and as a result you are getting error.
Please replace the update statement with "UPSERT".
Upsert will automatically detects wheteher a record is inserted or updated
So change your code with below code :
trigger Addreletedrecord on Account (after update, after insert) {
List<Opportunity> oppList = new List<Opportunity>();
for(Account a:[SELECT Id,Name from Account where Id IN:Trigger.new AND Id NOT IN(SELECT AccountId from Opportunity)]){

    oppList.add(New Opportunity(Name= a.Name+'opportunity',StageName='Prospecting',AccountId=a.Id));
    }
    If(oppList.Size()>0){
        upsert oppList;
       
        }

}

Let me know if you have any other issue.

Thanks,
Abhishek
This was selected as the best answer
Nitin SharmaNitin Sharma
Not working showing same eror .

Thanks,
Nitin
BarakBarak
@ Abhishek Bansal

Please elaborate your below code. I am not able to understand.


for(Account a:[SELECT Id,Name from Accountwhere Id IN:Trigger.new AND Id NOT IN(SELECTAccountId from Opportunity)]){


oppList.add(New Opportunity(Name= a.Name+'opportunity',StageName='Prospecting',AccountId=a.Id));

Thanks
Naveen
sanjul bhatiasanjul bhatia
can you guys help me with a similar problem.
i need to create a trigger with handler class to automatically create an opportunity when a new account is created where the account has min 5 mandatory fields and all the fields are copied to the new opportunity created.
Stancioiu Stefan 1Stancioiu Stefan 1
Hi guys. Anybody help me to write this trigger?.

1. Create a custom field on the Contact object: Is Primary Contact(checkbox);
2. A validation should be added so that if the Account already has a Primary Contact set a new one cannot be added;​
Abhishek BansalAbhishek Bansal
Hi Stancioiu,

Can you please open a new question regarding your request and share the link here?

Thanks,
Abhishek Bansal.
Stancioiu Stefan 1Stancioiu Stefan 1

Hi Abhishek ! 

 Thanks

https://developer.salesforce.com/forums/ForumsMain?id=9060G000000MTbFQAW