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
ledap13ledap13 

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());
                        }
                    }
                }
        }
    }
}
}

Best Answer chosen by Admin (Salesforce Developers) 
Deepa.B.AnkaliDeepa.B.Ankali
@ledap13,

Try this

new contract(AccountID = [select AccountID from Opportunity where Id = :tagoppid ].AccountID);

All Answers

Deepa.B.AnkaliDeepa.B.Ankali
@ledap13,

Try this

new contract(AccountID = [select AccountID from Opportunity where Id = :tagoppid ].AccountID);
This was selected as the best answer
GlynAGlynA

@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.

 

trigger QuoteTrigger on Quote ( after update )
{
    List<Contract> list_newContracts = new List<Contract>();

    for ( Quote quo : trigger.new )
    {
        if ( quo.Status != 'Accepted' || quo.Status == trigger.oldMap.get( quo.Id ).Status ) continue;

        //  create a contract
        list_newContracts.add
        (   new Contract
            (   AccountId = [SELECT AccountID FROM Opportunity WHERE Id = :quo.OpportunityId][0].AccountId,
                CustomerSigned      = quo.Contact,
                Name                = 'Test 1',
                CustomerSignedTitle = quo.Name,
                StartDate           = System.today(),
                ContractTerm        = 6
            )
        );
    }

    Database.SaveResult[] srList = Database.insert( list_newContracts, 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() );
            }
        }
    }
}

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

 

ledap13ledap13

@GlynA

 

I miss the syntax if(trigger.isUpdate){

 

Is it not necessary?

GlynAGlynA

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

ledap13ledap13

All right.