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
anu_karthianu_karthi 

System.QueryException: List has no rows for assignment to SObject:

Hi Every one,

 

I am new to write triggers.In my application 3 objects are there Obj A,Obj B,Obj C.

Obj A-SFN Number(Input field(text),Total Amount(Number))

Obj B-SFN Number(lookup(ObjA),Amount(Number))

Obj C-SFN Number(lookup(ObjA),Amount(Number))

 

Here i need to calculate sum of amount from Obj B and Obj C and assign the total value to Total Amount in

Obj A by using triggers.

 

Exception is raising as follows:

 

contact your administrator: chiranjeevi.summation: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.chiranjeevi.summation: line 4, column 96

 

My code is as follows:

 

trigger summation on chiranjeevi__Obj_A__c (before insert,before update) {
decimal ObjATotal,ObjBAmount,ObjCAmount;
for(chiranjeevi__Obj_A__c a : Trigger.new)
ObjBAmount=[Select Amount__c from chiranjeevi__Obj_B__c where SFN_Number__c =:a.SFN_Number__c].Amount__c;
for(chiranjeevi__Obj_A__c b : Trigger.new)
ObjBAmount=[Select Amount__c from chiranjeevi__Obj_C__c where SFN_Number__c =:b.SFN_Number__c].Amount__c;

for(chiranjeevi__Obj_A__c c : Trigger.new)
c.Total_Amount__c=ObjBAmount+ObjCAmount;
}

 

If any one knows plz help me.

 

Thanks & Regards,

Anu..

 

Nick34536345Nick34536345


ObjBAmount=[Select Amount__c from chiranjeevi__Obj_B__c where SFN_Number__c =:a.SFN_Number__c].Amount__c;

 


Hi,

 

Here you are assigning the result from a query to a single object - which is fine if you know there is always one record returned, but otherwise will throw the error you are seeing, You'll need to assign it to a list and then check that you have some records.

 

e.g.

 

List<chiranjeevi__Obj_B__c> objBlist = [Select Amount__c from chiranjeevi__Obj_B__c where SFN_Number__c =:a.SFN_Number__c];

...

 

 

anu_karthianu_karthi

Thanks for ur reply,

 

 I am new to handle lists.Again i am facing the problem how to add the list values and assign it to one variable.error is coming like this.

 

 Arithmetic expressions must use numeric arguments.plz help me how to achieve this functionality.

 

my code is as follows:

 

trigger summation on chiranjeevi__Obj_A__c (before insert,before update) {
List<chiranjeevi__Obj_A__c> objAlist=new List<chiranjeevi__Obj_A__c>();
List<chiranjeevi__Obj_B__c> objBlist=new List<chiranjeevi__Obj_B__c>();
List<chiranjeevi__Obj_C__c> ObjClist=new List<chiranjeevi__Obj_C__c>();
for(chiranjeevi__Obj_A__c a : Trigger.new)
objBlist =[Select Amount__c from chiranjeevi__Obj_B__c where SFN_Number__c =:a.SFN_Number__c];
for(chiranjeevi__Obj_A__c b : Trigger.new)
ObjClist=[Select Amount__c from chiranjeevi__Obj_C__c where SFN_Number__c =:b.SFN_Number__c];

for(chiranjeevi__Obj_A__c c : Trigger.new)
c.Total_Amount__c=objBlist+ObjClist;

 

Thanks in advance,

Anu..

 

 

 

Nick34536345Nick34536345

Hi again,

 

You can loop through the lists, in the same way you are looping through Trigger.new.

 

for example, assuming that you want to all the amounts of related chiranjeevi__Obj_B__c records, you can do this:

 

 

Double objBtotal = 0;

for (chiranjeevi__Obj_B__c objB : objBlist) objBtotal += objB.Amount__c;

 

By the way I just remembered there are aggregate SOQL functions coming in Spring 10. This probably means it will be better to rewrite this using "select SUM(Amount__c) ... "

 

 

anu_karthianu_karthi

Thank you very much ur support,

 

I had done as u told.but In object A total amount field  is not updated .this is the sample code.In my project many places i need to do this functionality.plz check my code and tell me where is the mistake.

 

trigger summation on chiranjeevi__Obj_A__c (before insert,before update,after update) {
Double objBtotal = 0;
List<chiranjeevi__Obj_A__c> objAlist=new List<chiranjeevi__Obj_A__c>();
List<chiranjeevi__Obj_B__c> objBlist=new List<chiranjeevi__Obj_B__c>();
List<chiranjeevi__Obj_C__c> ObjClist=new List<chiranjeevi__Obj_C__c>();
for(chiranjeevi__Obj_A__c a : Trigger.new)
objBlist =[Select Amount__c from chiranjeevi__Obj_B__c where SFN_Number__c =:a.SFN_Number__c];
for(chiranjeevi__Obj_A__c b : Trigger.new)
ObjClist=[Select Amount__c from chiranjeevi__Obj_C__c where SFN_Number__c =:b.SFN_Number__c];

for (chiranjeevi__Obj_A__c objA : Trigger.new)
{
 for (chiranjeevi__Obj_B__c objB : objBlist)
 objBtotal += objB.Amount__c;
 {
  objA.Total_Amount__c=objBtotal;
 }
}

 

Thanks in advance,

Anu..

Nick34536345Nick34536345

I see at lot of problems, where to start =)

 

First, going back to this SOQL query, it doens't look right:

 

Select Amount__c from chiranjeevi__Obj_B__c where SFN_Number__c =:a.SFN_Number__c

 

Are you sure SFN_Number__c on Obj B is a lookup, and SFN_Number__c on Obj A is an input field?

anu_karthianu_karthi

Thanks for ur reply,

 

Yes in objA SFN Number is input field of type Text and in objB SFN Number is a lookup field which is look over to objA SFN Number.

 

anu_karthianu_karthi
can u plz send me the example query
Nick34536345Nick34536345

Ok you should understand that a lookup field is an Id value. The query should look like this:

 

 

Select Amount__c from chiranjeevi__Obj_B__c where SFN_Number__c =:a.Id

 

 

 

 

 

anu_karthianu_karthi

Thank you so much,

 

Now i got the solution.

 

 

 

anu_karthianu_karthi

Hi again,

 

This functionality works fine.While integreating this functionality in my project in some objects the following error is raising.i was unable to rectify this error.i want to know why in some objects error is raising.

 

Review all error messages below to correct your data.
Apex trigger chiranjeevi.mbltotal caused an unexpected exception, contact your administrator: chiranjeevi.mbltotal: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.chiranjeevi.mbltotal: line 10, column 15

 

my code is as follows:

 

trigger mbltotal on chiranjeevi__Sea_Foot_Note__c (before insert,before update) {
Double objBtotal = 0;
List<chiranjeevi__Master_Bill_Of_Ladding__c> objAlist=new List<chiranjeevi__Master_Bill_Of_Ladding__c>();
for(chiranjeevi__Sea_Foot_Note__c b : Trigger.new)
objAlist=[SELECT chiranjeevi__Console_charges__c,chiranjeevi__SFN_no__c FROM chiranjeevi__Master_Bill_Of_Ladding__c Where chiranjeevi__SFN_no__c =: b.Id];
for (chiranjeevi__Sea_Foot_Note__c objA : Trigger.new)
{
for (chiranjeevi__Master_Bill_Of_Ladding__c objB : objAlist)

 objBtotal += objB.chiranjeevi__Console_charges__c;
   objA.chiranjeevi__Total_service_charges__c=objBtotal;//showing the error in thisline

}    
}

 

plz tell me the mistake.

 

Thanks in advance,

Anu..

Nick34536345Nick34536345

Could you put curly brackets { } around all your for loops? it may or may not fix the problem but the code would be clearer

 

 

anu_karthianu_karthi

Hi again,

 

At the time of integrating this functionality into my project i am getting the following errors.

 

I am try to create the new record in the Obj A with total amount null value.After Saving the record in record is saving with some amount(previous record values.)

 

while adding the amount from one more child to parent following error is raising.

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger chiranjeevi.sfntotal1 caused an unexpected exception, contact your administrator: chiranjeevi.sfntotal1: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.chiranjeevi.sfntotal1: line 15, column 22

 

my code is as follows:

 

trigger sfntotal1 on chiranjeevi__Sea_Foot_Note__c (before insert,before update) {
    Double total = 0;
    List<chiranjeevi__Proforma_Invoice_Header__c> pheaderlist = new List<chiranjeevi__Proforma_Invoice_Header__c>();
    List<chiranjeevi__Master_Bill_Of_Ladding__c> mbllist=new List<chiranjeevi__Master_Bill_Of_Ladding__c>();
    for(chiranjeevi__Sea_Foot_Note__c b : Trigger.new)     
        pheaderlist=[SELECT chiranjeevi__Total_amt__c,chiranjeevi__SFN_no__c FROM chiranjeevi__Proforma_Invoice_Header__c Where chiranjeevi__SFN_no__c =: b.Id];
    for(chiranjeevi__Sea_Foot_Note__c e : Trigger.new)    
        mbllist=[SELECT chiranjeevi__Console_charges__c FROM chiranjeevi__Master_Bill_Of_Ladding__c WHERE  chiranjeevi__SFN_no__c =:  e.Id];
           
    for (chiranjeevi__Sea_Foot_Note__c sfn : Trigger.new)
    {
        for (chiranjeevi__Proforma_Invoice_Header__c phead : pheaderlist)       
            total += phead.chiranjeevi__Total_amt__c;           
        for (chiranjeevi__Master_Bill_Of_Ladding__c mbl : mbllist)
            total += mbl.chiranjeevi__Console_charges__c;
                
            sfn.chiranjeevi__Total_service_charges__c = total;
         
    }
}

 

plz help me where i did mistake.

 

Thanks in advance,

Anu...

 

anu_karthianu_karthi

Hi,

 

still i am facing the problem is at the time of creating the new record with new sfn number in parent object some garbage value is stored .if i edit the record then it adds the amount from child object.But i need to remove the garbage value and store the initial amout value as 0.

 

my code is as follows.

 

trigger sfntotal1 on chiranjeevi__Sea_Foot_Note__c (before insert,before update,after insert)
{
    for(chiranjeevi__Sea_Foot_Note__c objA : Trigger.new)
    {
        Double total = 0;
        for(chiranjeevi__Master_Bill_Of_Ladding__c mbllist:[SELECT chiranjeevi__Console_charges__c,chiranjeevi__Freight_charges__c,chiranjeevi__Other_charges__c,chiranjeevi__SFN_no__c FROM chiranjeevi__Master_Bill_Of_Ladding__c Where chiranjeevi__SFN_no__c =: objA.Id])
        {
            if(mbllist.chiranjeevi__Console_charges__c != null)
                total += mbllist.chiranjeevi__Console_charges__c;
            if(mbllist.chiranjeevi__Freight_charges__c != null)
                total += mbllist.chiranjeevi__Freight_charges__c;
            if(mbllist.chiranjeevi__Other_charges__c != null)
                total += mbllist.chiranjeevi__Other_charges__c;
        }
        for(chiranjeevi__House_Bill_Of_Ladding__c hbllist:[SELECT chiranjeevi__Console_charges__c,chiranjeevi__Freight_charges__c,chiranjeevi__Other_charges__c,chiranjeevi__SFN_no__c FROM chiranjeevi__House_Bill_Of_Ladding__c Where chiranjeevi__SFN_no__c =: objA.Id])
        {
            if(hbllist.chiranjeevi__Console_charges__c != null)
                total += hbllist.chiranjeevi__Console_charges__c;
            if(hbllist.chiranjeevi__Freight_charges__c != null)
                total += hbllist.chiranjeevi__Freight_charges__c;
            if(hbllist.chiranjeevi__Other_charges__c != null)
                total += hbllist.chiranjeevi__Other_charges__c;
        }
        for(chiranjeevi__Proforma_Invoice_Header__c proformalist:[SELECT chiranjeevi__Total_amt__c,chiranjeevi__SFN_no__c FROM chiranjeevi__Proforma_Invoice_Header__c Where chiranjeevi__SFN_no__c =: objA.Id])
        {
            if(proformalist.chiranjeevi__Total_amt__c != null)
                total += proformalist.chiranjeevi__Total_amt__c;
        }
        for(chiranjeevi__CFS_CCTL_Details__c cfslist:[SELECT chiranjeevi__Amount_paid__c,chiranjeevi__SFN_no__c  FROM chiranjeevi__CFS_CCTL_Details__c Where chiranjeevi__SFN_no__c =: objA.Id])
        {
            if(cfslist.chiranjeevi__Amount_paid__c != null)
                total += cfslist.chiranjeevi__Amount_paid__c;
        } 
        for(chiranjeevi__Delivery_Order_Details__c dolist:[SELECT chiranjeevi__Halting_Charges__c,chiranjeevi__Transport_Charges__c,chiranjeevi__SFN_Number__c FROM chiranjeevi__Delivery_Order_Details__c  Where chiranjeevi__SFN_Number__c=: objA.Id])
        {
            if(dolist.chiranjeevi__Halting_Charges__c != null)
                total += dolist.chiranjeevi__Halting_Charges__c;
            if(dolist.chiranjeevi__Transport_Charges__c != null)
                total += dolist.chiranjeevi__Transport_Charges__c;
        }
        for(chiranjeevi__Holding_Instruments__c holdlist:[SELECT chiranjeevi__Deposit_amt__c,chiranjeevi__SFN_no__c  FROM chiranjeevi__Holding_Instruments__c Where chiranjeevi__SFN_no__c =: objA.Id])
        {
            if(holdlist.chiranjeevi__Deposit_amt__c != null)
                total -= holdlist.chiranjeevi__Deposit_amt__c;
        }   
        if(total != 0)
            objA.chiranjeevi__Total_service_charges__c=total;
    }

}


Thanks in advance,

Anu..