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
sreelekhasreelekha 

About Opportunity and Quote

i have written a trigger for updating the amount field value with quote total price when we selet the quote as primary.but it will work only when we adding single quotelinitem to the quote.When i am trying bulky quotelineitems at a time it will throw an exception " Apex trigger qlitem123 caused an unexpected exception, contact your administrator: qlitem123: execution of AfterInsert caused by: System.QueryException: List has more than 1 row for assignment to SObject: Trigger.qlitem123: line 3, column 28  "

 

my code is here:please help me to solve this problem

 

 

trigger qlitem123 on QuoteLineItem (after insert, after update) {
         public static double total=0;
         quotelineitem qli=[select id,quoteid,totalprice from quotelineitem where id in:Trigger.newMap.keyset()];
         List<QuoteLineItem> qlil=new List<Quotelineitem>();
          qlil.add(qli);
         quote q=[select id,opportunityid,totalprice,stage__c from quote where id=:qli.quoteid];
         
         opportunity opp=[select id,amount from opportunity where id=:q.opportunityid];
         List<opportunity> oppl=new List<opportunity>();
         
         for(integer i=0;i<qlil.size();i++)
          {
           
             total=total+qlil[i].totalprice;
          
          }
          if(q.stage__c=='primary')
          {
            opp.amount=q.totalprice+total;
           
          }
          oppl.add(opp);
          update oppl;
}

 Thanks 

 Sreelekha

SrikanthKuruvaSrikanthKuruva

you are doing a mistake in the code

quotelineitem qli=[select id,quoteid,totalprice from quotelineitem where id in:Trigger.newMap.keyset()];

it should actually be

List<quotelineitem> qli=[select id,quoteid,totalprice from quotelineitem where id in:Trigger.newMap.keyset()];

 

and then you process further accordingly

sreelekhasreelekha

Hi,

Thanks for reply

List<quotelineitem> qli=[select id,quoteid,totalprice from quotelineitem where id in:Trigger.newMap.keyset()];

i have written like that also.But i didn't get how to set qli.quoteid to quote for number of quote line items without loop.

 

vishal@forcevishal@force

quotelineitem qli=[select id,quoteid,totalprice from quotelineitem where id in:Trigger.newMap.keyset()];      

List<QuoteLineItem> qlil=new List<Quotelineitem>();         

qlil.add(qli);

 

Here when you are creating a quotelineitem object qli, your query is returning more than one resulting values, hence you are unable to assign it to the object variable. for more than one row, you need to assign it to a list.

 

 

what you can do is :

 

List<QuoteLineItem> qlil=new List<Quotelineitem>();       

qlil = [select id,quoteid,totalprice from quotelineitem where id in:Trigger.newMap.keyset()];      

 

No need to take a separate object and then add it to the list.

 

will that work for you?

vishal@forcevishal@force

incase of multiple records in your list, I don't think you can process it without a for loop.

How would you determing from which quotelineitem, your quote id is?

sreelekhasreelekha

but i have the requirement to do this without writing any query inside for loop.I think it is possible through the map,set.But i could confused with them.if you know please reply me how to do.

SrikanthKuruvaSrikanthKuruva

try the following code. correct any syntax mistakes

 

trigger qlitem123 on QuoteLineItem (after insert, after update) 
    {
     public static double total=0;
     //quotelineitem qli=[select id,quoteid,totalprice from quotelineitem where id in:Trigger.newMap.keyset()];
    map<Id,double> mapQuoteidPrice = new map<Id,double>();
    for (quotelineitem qli : [select id,quoteid,totalprice from quotelineitem where id in:Trigger.newMap.keyset()])
    {
        if (mapQuoteidPrice.isEmpty())
        {
            mapQuoteidPrice.put(qli.quoteid,qli.totalprice);
        }
        else
        {
            if (mapQuoteidPrice.contains(qli.quoteid))
            {
                double temp = mapQuoteidPrice.get(qli.quoteid);
                temp = temp+qli.totalprice;
                mapQuoteidPrice.remove(qli.quoteid);
                mapQuoteidPrice.put(qli.quoteid,temp);
            }
            else
            {
                mapQuoteidPrice.put(qli.quoteid,qli.totalprice);
            }
        }
    }
    map<Id,double> mapOppidAmount = new map<Id,double>();
    for (quote q : [select id,opportunityid,totalprice,stage__c from quote where id in:mapQuoteidPrice.keySet()];
    {
        if (q.stage__c == 'primary')
        {
            double temp = mapQuoteidPrice.get(q.id);
            temp = temp+q.totalprice;
            mapQuoteidPrice.remove(q.id);
            mapQuoteidPrice.put(q.id,temp);
        }
        if (mapOppidAmount.isEmpty())
        {
            mapOppidAmount.put(q.opportunityid,mapQuoteidPrice.get(q.id));
        }
        else
        {
            if (mapOppidAmount.contains(q.opportunityid))
            {
                double temp = mapOppidAmount.get(q.opportunityid);
                temp = temp+mapQuoteidPrice.get(q.id);
                mapOppidAmount.remove(q.opportunityid);
                mapOppidAmount.put(q.opportunityid,temp);
            }
            else
            {
                mapOppidAmount.put(q.opportunityid,mapQuoteidPrice.get(q.id));
            }
        }
    }
    List<opportunity> oppl=new List<opportunity>();
    for(Opportunity opp : [select id,amount from opportunity where id=:q.opportunityid])
    {
        opp.amount = mapOppidAmount.get(q.opportunityid);
        oppl.add(opp);
    }
    update oppl;
}

 

sreelekhasreelekha

Hi ,

Thanks for reply.

But it gives compile error: Variable does not exist: q.opportunityid at line 59 column 41

Rahul SharmaRahul Sharma

the code is not bulkified, is it the case that you won't bulk insert the records?

and for error, change your code to:

 

for(Opportunity opp : [select id,amount from opportunity where id=:q.opportunityid])
    {
        opp.amount = mapOppidAmount.get(opp.Id);
        oppl.add(opp);
    }

 

sreelekhasreelekha

Hi,Rahul

Thans for reply it also give compiler error:Compile Error: Variable does not exist: q.opportunityid at line 57 column 72

at   select id,amount from opportunity where id=:q.opportunityid

SrikanthKuruvaSrikanthKuruva

Change the line of code to

for(Opportunity opp : [select id,amount from opportunity where id in : mapOppidAmount.KeySet()]