+ Start a Discussion
stollmeyerastollmeyera 

Error throwing on simple update trigger

Let me first start by saying that I am completely new to the Appex scene.  I created the below trigger with some help from another cummunity member yesterday.  The logic is very simple: Whenever the field opp.StageName is updated to '4 - Closed Won' and the roll-up summary field opp.Hosting__c is greater than zero, change the account.type to 'Customer'.  

 

 

trigger OpportunityUpdateAccount on Opportunity(after insert, after update) {
List<Account> accsToUpdate = new List<Account>();

for(Opportunity opp : trigger.new)
{if (opp.StageName == '4 - Closed Won' && opp.Hosting__c > 0)
accsToUpdate.add(new Account(Type = 'Customer'));
}
if (accsToUpdate != null && !accsToUpdate.isEmpty())
Database.update(accsToUpdate);
}

 However, when I go into an Opportunity and (in either order) add a product to increase the Hosting__c above $0 and update the StageName to '4 - Closed Won', it throws the below error:

 

Apex trigger OpportunityUpdateAccount caused an unexpected exception, contact your administrator: OpportunityUpdateAccount: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.OpportunityUpdateAccount: line 9, column 1  

 

 

My guess is that I have not done a well enough job identifying what records actually need to be changed.  

 

Any suggestions?

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

The problem is that you are creating a new account:

 

 

accsToUpdate.add(new Account(Type = 'Customer'));

 

 

but the system has no way to associate this with an existing account.  You'll need to specify the id, which you should be able to pull from the opportunity object, e.g.

 

 

accsToUpdate.add(new Account(Id=opp.AccountId, Type = 'Customer'));

 

 

 

 

 

All Answers

bob_buzzardbob_buzzard

The problem is that you are creating a new account:

 

 

accsToUpdate.add(new Account(Type = 'Customer'));

 

 

but the system has no way to associate this with an existing account.  You'll need to specify the id, which you should be able to pull from the opportunity object, e.g.

 

 

accsToUpdate.add(new Account(Id=opp.AccountId, Type = 'Customer'));

 

 

 

 

 

This was selected as the best answer
stollmeyerastollmeyera

That fixed it.  Thanks a ton!!

vinodshivharevinodshivhare

I am also facing the same issue. Please see my code snippet below:

    private void updateRecordLockedBy() {
    	List<Workflow_Status__c> wsList = new List<Workflow_Status__c>();
    	for (statusWrapper sw : wstatuses) {
    		wsList.add(sw.workflowStatus);
    	}
    	for (Workflow_Status__c ws : wsList) {
    		Workflow_Status__c myWs = new Workflow_Status__c(Id=ws.Id);
    		myWs.Record_Locked_By__c = system.UserInfo.getUserId();
    		update myWs;
    	}
    }

 

I am receiving following error:

System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: [] 

 

 

 Appreciate your help to resolve this issue.