You need to sign in to do that
Don't have an account?

Error in Insert Trigger (Invalid initial expression type for field)
Hi Friends,
what 's wrong?
Error:
Error: Compile Error: Invalid initial expression type for field Contract.AccountId, expecting: Id at line 9 column 42
Trigger
trigger QuoteTrigger on Quote(after update){
private string tagoppid {get;set;}
for(Quote quo : trigger.new){
if(trigger.isUpdate){
if(quo.Status== 'Accepted' && quo.Status!= trigger.oldMap.get(quo.Id).Status){
tagoppid = quo.OpportunityId;
//create a contract
contract[] newcontract = new List<contract>{
new contract(AccountID = [select AccountID from Opportunity where Id = :tagoppid ],
//AccountID = quo.OpportunityId,
CustomerSigned = quo.Contact,
Name = 'Test 1',
CustomerSignedTitle = quo.Name,
StartDate = System.today(),
ContractTerm = 6)};
//SaveResult[] results = binding.create(new sObject[] {newcontract });
Database.SaveResult[] srList = Database.insert(newcontract, false);
// Iterate through each returned result
for (Database.SaveResult sr : srList) {
if (sr.isSuccess()) {
// Operation was successful, so get the ID of the record that was processed
System.debug('Successfully inserted account. Account ID: ' + sr.getId());
}
else {
// Operation failed, so get all errors
for(Database.Error err : sr.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Account fields that affected this error: ' + err.getFields());
}
}
}
}
}
}
}
Try this
new contract(AccountID = [select AccountID from Opportunity where Id = :tagoppid ].AccountID);
All Answers
Try this
new contract(AccountID = [select AccountID from Opportunity where Id = :tagoppid ].AccountID);
@ledap13,
DeepaAnika.Ankali has the solution to your question.
I notice that your trigger is not batch safe, because you have the Database.insert() call inside of your for loop. If you were to update a large number of Quotes, you would hit the DML limit. It is better practice within the loop to create a list of records to be inserted, and then insert the list of records outside of the loop at the end.
If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!
-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator
@GlynA
I miss the syntax if(trigger.isUpdate){
Is it not necessary?
It's not necessary because the trigger is defined to fire only for the "after update" event. Unless you add "after insert" or some other event to the trigger, it will only execute on update.
-Glyn
All right.