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
kingsixkingsix 

Error Trigger with Quote Id

I got a error message with my Trigger. I just want to do, if Quote's status is ''Accepted', so Opportunity's stage change to 'Closed Won' automaticlly.

 

trigger test on Quote(after insert, after update) {
List<Opportunity> stageToUpdate = new List<Opportunity>();

for(Quote quo : trigger.new)
{if (quo.status == 'Accepted')
stageToUpdate.add(new Opportunity(Id=quo.Id, StageName = 'Closed Won'));
}
if (stageToUpdate != null && !stageToUpdate.isEmpty())
Database.update(stageToUpdate);
}

 

Error message,

execution of AfterUpdate caused by: System.TypeException: Invalid id value for this SObject type: 0Q09000000009qYCAQ: Trigger.test: line 6, column 38

Best Answer chosen by Admin (Salesforce Developers) 
Rahul S.ax961Rahul S.ax961

Hi,

 

I tried the same code in my org, its working....

I dont get the exact problem here.  i'm not sure it would help, but give it a try...

 

Code:

 

trigger test on Quote (after insert, after update)
{
    Set<Id> setId = new set<Id>();
    List<Opportunity> stageToUpdate = new List<Opportunity>();
    for(Quote objQuote : [Select OpportunityId, Name, status from Quote where status = 'Accepted' and Id In: Trigger.new])
    {
        if(!setId.contains(objQuote.OpportunityId))
        {
            setId.add(objQuote.OpportunityId);
            string str = objQuote.OpportunityId;
            stageToUpdate.add(new Opportunity(Id=str.substring(0,15), StageName = 'Closed Won'));
        }
    }
    if (stageToUpdate != null && !stageToUpdate.isEmpty())
        Database.update(stageToUpdate);
}

 

 

Thanks

All Answers

VPrakashVPrakash

Try this 

 

this would work only for one quote.

 

trigger test on Quote(after insert, after update) {
List<Opportunity> stageToUpdate = new List<Opportunity>();

Opportunity opp = [select id, name,StageName from opportuntiy where id:=tirgger.new[0].opportunity];

for(Quote quo : trigger.new)
{if (quo.status == 'Accepted')

opp.StageName = 'Closed Won';
stageToUpdate.add(opp));
}
if (stageToUpdate != null && !stageToUpdate.isEmpty())
Database.update(stageToUpdate);
}

kingsixkingsix

Thanks, it show a error info.

 

Severity and Description Path Resource Location Creation Time Id
Save error: Invalid bind expression type of SOBJECT:Opportunity for column of type Id test/src/triggers test.trigger line 4 1302196655625 147

Opportunity opp = [select id, name, StageName from opportunity where id=:trigger.new[0].opportunity];

 

VPrakashVPrakash

Did you try this 

 

Opportunity opp = [select id, name, StageName from opportunity where id=:trigger.new[0].opportunity.id];

kingsixkingsix

Sorry, when I save Quote, it said  test: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.test: line 4, column 19

 

kingsixkingsix

Anyone can help me?

Rahul S.ax961Rahul S.ax961

Hi,

try this.. hope it help.

 

trigger test on Quote(after insert, after update)
{
    Set<Id> setId = new setId();
    List<Opportunity> stageToUpdate = new List<Opportunity>();
    for(Quote objQuote : [Select OpportunityId, Name, status from Quote where status = 'Accepted' and Id In: Trigger.new])
    {
        if(!setId.contains(objQuote.OpportunityId))
        {
            setId.add(objQuote.OpportunityId);
            stageToUpdate.add(new Opportunity(Id=quo.Id, StageName = 'Closed Won'));
        }
    }
    if (stageToUpdate != null && !stageToUpdate.isEmpty())
        Database.update(stageToUpdate);
}

 

 

 

Thanks

kingsixkingsix
Sorry, when I change the status of Quote, and click save, it shows error info. test: execution of AfterUpdate caused by: System.TypeException: Invalid id value for this SObject type: 0Q0900000000AFaCAM: Trigger.test: line 10, column 50
Rahul S.ax961Rahul S.ax961

Hi,

 

I tried the same code in my org, its working....

I dont get the exact problem here.  i'm not sure it would help, but give it a try...

 

Code:

 

trigger test on Quote (after insert, after update)
{
    Set<Id> setId = new set<Id>();
    List<Opportunity> stageToUpdate = new List<Opportunity>();
    for(Quote objQuote : [Select OpportunityId, Name, status from Quote where status = 'Accepted' and Id In: Trigger.new])
    {
        if(!setId.contains(objQuote.OpportunityId))
        {
            setId.add(objQuote.OpportunityId);
            string str = objQuote.OpportunityId;
            stageToUpdate.add(new Opportunity(Id=str.substring(0,15), StageName = 'Closed Won'));
        }
    }
    if (stageToUpdate != null && !stageToUpdate.isEmpty())
        Database.update(stageToUpdate);
}

 

 

Thanks

This was selected as the best answer
kingsixkingsix

Finally! Finally!  It works!!

 

Thanks soso much:-)

KitagawaSan1337KitagawaSan1337

Could you also use a similar method to update the SyncedQuoteID field on the opportunity with the Quote ID when a quote is created? 

 

-Justin