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
Somasundaram S 1Somasundaram S 1 

get parent record

In below code am getting , am trying so many different method sample lot is parent and sample transaction is child could you please suggets me what is the reason for below error
Compile Error: Didn't understand relationship 'Sample_Transaction__c' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 8 column 44

 
trigger quantityleft on Sample_Transaction__c (after insert,after update,after delete,after undelete) {
    List<Sample_Lot__c> list_Account= new List<Sample_Lot__c>();
     set<Id> set_Opportunity = new set<Id>();
         for(Sample_Transaction__c objOpp: trigger.new){
        set_Opportunity.add(objOpp.Sample_Lot_ID__c);
    }
    Decimal Sum;
            for(Sample_Lot__c objAccount : [SELECT Id,(SELECT Id,sample_lot FROM Sample_Transaction__c),Quantity_Left__c FROM Sample_Lot__c WHERE Id IN: set_Opportunity]){
        Sum=0;
                        
        for(Sample_Transaction__c objOpp01: objAccount.Quantity_Left__c){
            Sum+=objOpp01.Confirmed_Quantity__c ;
        }
        objAccount.Quantity_left__c =Sum;
        list_Account.add(objAccount);
    }
    update list_Account;
}

 
Best Answer chosen by Somasundaram S 1
Rafael.Martins.SantosRafael.Martins.Santos
OK.
So you want to sum all the register of the child object, and show in the field Quantity__c the total, is it?
if is it that you want, you must do something like that:


    Decimal Sum;
    for(Sample_Transaction__c sample_transaction : [SELECT Id,Sample_Lot_ID__c, Confirmed_Quantity__c FROM Sample_Transaction__c        WHERE Id = : set_Opportunity]){

            Sum+= sample_transaction.Confirmed_Quantity__c;

        for(Sample_Lot__c objAccount : [SELECT Id, Quantity_Left__c FROM Sample_Lot__c WHERE Id = : sample_transaction.Sample_Lot_ID__c]){

            objAccount.Quantity_left__c = Sum ;
            list_Account.add(objAccount);

        }
    }
    update list_Account;


 

All Answers

Rafael.Martins.SantosRafael.Martins.Santos
Hi Somasundaram,

In the line 8 of your code, you must change the follow code:

for(Sample_Lot__c objAccount : [SELECT Id, SELECT Id,sample_lot FROM Sample_Transaction__c),Quantity_Left__c FROM Sample_Lot__c WHERE Id IN: set_Opportunity])

to this code:
for(Sample_Lot__c objAccount : [SELECT Id,(SELECT Id,sample_lot FROM Sample_Transactions__r),Quantity_Left__c FROM Sample_Lot__c WHERE Id IN: set_Opportunity])

Note: When you make a SELECT inside of another, you must use the plural name of the object and change the __c for __r.

Best Regards
Rafael

 
Shruti SShruti S
You are using the incorrect Child Relationship Name

For example lets consider Account and Opportunity object. Here I have taken the example of "Account Name" field in Opportunity which is a lookup to Account. This means Account is a parent and Opportunity is a child. 

User-added image

Now if I want the Account and its related Opportunities in a single query, this is how you should write the SOQL - 
List<Account> accs = new List<Account>();

accs = [
    SELECT  Name
            ,(
                SELECT  Name
                FROM    Opportunites
            )
    FROM    Account
];
If it is a Custom Object, you will have to append "__r" to the Child Relationship Name.

In your case, you will have to go to the Lookup field ( Lookup field pointing to the Sample_Lot__c ) on the Sample_Transaction__c object and look on its Child Relationship Name and append "__r" to it.

Most probably it should look something like this - 
[
    SELECT Id
            ,(
                SELECT  Id
                        ,Sample_Lot__c
                FROM    Sample_Transactions__r
            )
            ,Quantity_Left__c 
    FROM    Sample_Lot__c 
    WHERE   Id IN: set_Opportunity
]
Somasundaram S 1Somasundaram S 1
this time different error interesting  some thing minor am missiing Rafael
Error: Compile Error: Variable does not exist: objAccount.Quantity_Left__c at line 21 column 45
 
trigger quantityleft on Sample_Transaction__c (after insert,after update,after delete,after undelete) {

    List<Sample_Lot__c> list_Account= new List<Sample_Lot__c>();

     set<Id> set_Opportunity = new set<Id>();

         for(Sample_Transaction__c objOpp: trigger.new){

        set_Opportunity.add(objOpp.Sample_Lot_ID__c);

    }

    Decimal Sum;

           for(Sample_Lot__c objAccount : [SELECT Id,(SELECT Id,Sample_Lot_ID__c FROM Sample_Transactions__r),Quantity_Left__c FROM Sample_Lot__c WHERE Id IN: set_Opportunity])

        Sum=0;

                         

        for(Sample_Transaction__c objOpp01: objAccount.Quantity_Left__c){

            Sum+=objOpp01.Confirmed_Quantity__c ;

        }

        objAccount.Quantity_left__c =Sum;

        list_Account.add(objAccount);
update list_Account;
    }

 
Rafael.Martins.SantosRafael.Martins.Santos
try do this:
for(Sample_Transaction__c objOpp01: Sample_Transactions__r.Quantity_Left__c){
Sum+=objOpp01.Confirmed_Quantity__c ;
}
Somasundaram S 1Somasundaram S 1
same error Rafael ,
i guess the below code has collection of sample lot created from transaction

for(Sample_Lot__c objAccount : [SELECT Id,(SELECT Id,Sample_Lot_ID__c FROM Sample_Transactions__r),Quantity_Left__c FROM Sample_Lot__c WHERE Id IN: set_Opportunity])

after this i would like to sum all the confirmed quantity from sample lot and update in quantity left field in sampel transcation obeject 
in this scenario how to get sum of all confirmed quantity by looping the below line
for(Sample_Transaction__c objOpp01: Sample_Transactions__r.Quantity_Left__c){
Sum+=objOpp01.Confirmed_Quantity__c ;
}
Somasundaram S 1Somasundaram S 1
I copied your code and followed same Rafael and feel some thing minor am missing any clue please suggest me, any specific reason for complie error 
https://developer.salesforce.com/forums/?id=906F0000000AtWEIA0
Rafael.Martins.SantosRafael.Martins.Santos
OK.
So you want to sum all the register of the child object, and show in the field Quantity__c the total, is it?
if is it that you want, you must do something like that:


    Decimal Sum;
    for(Sample_Transaction__c sample_transaction : [SELECT Id,Sample_Lot_ID__c, Confirmed_Quantity__c FROM Sample_Transaction__c        WHERE Id = : set_Opportunity]){

            Sum+= sample_transaction.Confirmed_Quantity__c;

        for(Sample_Lot__c objAccount : [SELECT Id, Quantity_Left__c FROM Sample_Lot__c WHERE Id = : sample_transaction.Sample_Lot_ID__c]){

            objAccount.Quantity_left__c = Sum ;
            list_Account.add(objAccount);

        }
    }
    update list_Account;


 
This was selected as the best answer
Rafael.Martins.SantosRafael.Martins.Santos
Only other suggestion is that exist a custom field that make the sum automatically for you called : Summary of totaling or something like that.
This field make the SUM automatically for you even you edit a child object.
Somasundaram S 1Somasundaram S 1
Raefell
  Excellent your code compiled well and mine is not master child relation ship it is lookup one

your code complied nicely and when i tested it i noticed that nothing happened , in the below code in set_opputunity i guess am not storing the id 
am using set_Opportunity.add(objOpp.Sample_Lot_ID__c) how to store the id of child 

or(Sample_Transaction__c sample_transaction : [SELECT Id,Sample_Lot_ID__c, Confirmed_Quantity__c FROM Sample_Transaction__c        WHERE Id = : set_Opportunity]){
Rafael.Martins.SantosRafael.Martins.Santos
change this:
WHERE Id = : set_Opportunity

for this:

WHERE Id = : : Trigger.newMap.keyset() limit 1

this getting the current register ID.
 
Somasundaram S 1Somasundaram S 1
Raefell
             I thought this issue is due to am not adding id in set_oppurtunity so i changed to store id i may be wrong and after changing the code as suggested this time i got 
Apex trigger quantityleft caused an unexpected exception, contact your administrator: quantityleft: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.quantityleft: line 17, column 1
 
trigger quantityleft on Sample_Transaction__c (after insert,after update,after delete,after undelete) {

    List<Sample_Lot__c> list_Account= new List<Sample_Lot__c>();

     set<Id> set_Opportunity = new set<Id>();

         for(Sample_Transaction__c objOpp: trigger.new){

        set_Opportunity.add(objOpp.Parent_id__c);

    }

        Decimal Sum;
    for(Sample_Transaction__c sample_transaction : [SELECT Id,Sample_Lot_ID__c, Transaction_Quantity__c FROM Sample_Transaction__c WHERE Id = : Trigger.newMap.keyset() limit 1
]){

            Sum+= sample_transaction.Transaction_Quantity__c;

        for(Sample_Lot__c objAccount : [SELECT Id, Quantity_Left__c FROM Sample_Lot__c WHERE Id = : sample_transaction.Sample_Lot_ID__c]){

            objAccount.Quantity_left__c = Sum ;
            list_Account.add(objAccount);

        }
    }
    update list_Account;
}

 
Somasundaram S 1Somasundaram S 1
some thing interetsing the system debug didnt print System.debug('33333333333'); and it went last line please refer the debug details 
what are we missing 
 
trigger quantityleft on Sample_Transaction__c (after insert,after update,after delete,after undelete) {
System.debug('11111111');
    List<Sample_Lot__c> list_Account= new List<Sample_Lot__c>();

     set<Id> set_Opportunity = new set<Id>();

         for(Sample_Transaction__c objOpp: trigger.new){

        set_Opportunity.add(objOpp.Parent_id__c);

    }
System.debug('22222222222222222222');
            Decimal Sum;
            Sum=0;
            System.debug('99999');
    for(Sample_Transaction__c sample_transaction : [SELECT Id,Sample_Lot_ID__c, Confirmed_Quantity__c FROM Sample_Transaction__c        WHERE Id = : set_Opportunity]){
System.debug('33333333333');
            Sum+= sample_transaction.Transaction_Quantity__c;
System.debug('SOMUUUUs: ' + Sum);
        for(Sample_Lot__c objAccount : [SELECT Id, Quantity_Left__c FROM Sample_Lot__c WHERE Id = : sample_transaction.Sample_Lot_ID__c]){
System.debug('4444444444');
            objAccount.Quantity_left__c = Sum ;
            list_Account.add(objAccount);

        }
        System.debug('555555555');
        update list_Account;

    }
    System.debug('66666666666');
        }



39.0 APEX_CODE,FINEST;APEX_PROFILING,NONE;CALLOUT,NONE;DB,NONE;SYSTEM,FINE;VALIDATION,NONE;VISUALFORCE,NONE;WAVE,INFO;WORKFLOW,FINER 12:01:19.0 (329734)|USER_INFO|[EXTERNAL]|00524000001xWet|somasundaram.s@quintiles.com.prod.training|Eastern Standard Time|GMT-04:00 12:01:19.0 (355303)|EXECUTION_STARTED 12:01:19.0 (359036)|CODE_UNIT_STARTED|[EXTERNAL]|Validation:Sample_Transaction:a0i0E0000000YDu 12:01:19.0 (1315526)|CODE_UNIT_FINISHED|Validation:Sample_Transaction:a0i0E0000000YDu 12:01:19.0 (1323814)|EXECUTION_FINISHED 12:01:19.30 (30487746)|USER_INFO|[EXTERNAL]|00524000001xWet|somasundaram.s@quintiles.com.prod.training|Eastern Standard Time|GMT-04:00 12:01:19.30 (30500389)|EXECUTION_STARTED 12:01:19.30 (30504235)|CODE_UNIT_STARTED|[EXTERNAL]|01q0E000000Cexp|quantityleft on Sample_Transaction trigger event AfterUpdate for [a0i0E0000000YDu] 12:01:19.30 (30547560)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8 12:01:19.30 (30576874)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8 12:01:19.30 (30882525)|HEAP_ALLOCATE|[72]|Bytes:3 12:01:19.30 (30917382)|HEAP_ALLOCATE|[77]|Bytes:152 12:01:19.30 (30930150)|HEAP_ALLOCATE|[342]|Bytes:408 12:01:19.30 (30944185)|HEAP_ALLOCATE|[355]|Bytes:408 12:01:19.30 (30956531)|HEAP_ALLOCATE|[467]|Bytes:48 12:01:19.30 (30981086)|HEAP_ALLOCATE|[139]|Bytes:6 12:01:19.30 (31022984)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:22 12:01:19.30 (31123631)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:16 12:01:19.30 (31132070)|VARIABLE_SCOPE_BEGIN|[1]|this|quantityleft|true|false 12:01:19.30 (31175547)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x5c1aa4b9 12:01:19.30 (31202449)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:16 12:01:19.30 (31207502)|VARIABLE_SCOPE_BEGIN|[1]|this|quantityleft|true|false 12:01:19.30 (31221496)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x5c1aa4b9 12:01:19.30 (31225430)|STATEMENT_EXECUTE|[1] 12:01:19.30 (31227333)|STATEMENT_EXECUTE|[2] 12:01:19.30 (31230600)|HEAP_ALLOCATE|[2]|Bytes:8 12:01:19.30 (31260574)|HEAP_ALLOCATE|[50]|Bytes:5 12:01:19.30 (31283753)|HEAP_ALLOCATE|[56]|Bytes:5 12:01:19.30 (31289585)|HEAP_ALLOCATE|[64]|Bytes:7 12:01:19.30 (31317887)|SYSTEM_METHOD_ENTRY|[2]|System.debug(ANY) 12:01:19.30 (31345865)|USER_DEBUG|[2]|DEBUG|11111111 12:01:19.30 (31351803)|SYSTEM_METHOD_EXIT|[2]|System.debug(ANY) 12:01:19.30 (31355915)|STATEMENT_EXECUTE|[3] 12:01:19.30 (31360992)|HEAP_ALLOCATE|[3]|Bytes:4 12:01:19.30 (31396368)|SYSTEM_CONSTRUCTOR_ENTRY|[3]|<init>() 12:01:19.30 (31412860)|SYSTEM_CONSTRUCTOR_EXIT|[3]|<init>() 12:01:19.30 (31433752)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 12:01:19.30 (31444306)|VARIABLE_ASSIGNMENT|[3]|this.list_Account|[]|0x5c1aa4b9 12:01:19.30 (31446741)|STATEMENT_EXECUTE|[5] 12:01:19.30 (31452985)|HEAP_ALLOCATE|[5]|Bytes:4 12:01:19.30 (31466138)|SYSTEM_CONSTRUCTOR_ENTRY|[5]|<init>(Integer) 12:01:19.30 (31475885)|SYSTEM_CONSTRUCTOR_EXIT|[5]|<init>(Integer) 12:01:19.30 (31503946)|VARIABLE_ASSIGNMENT|[5]|this.set_Opportunity|[]|0x5c1aa4b9 12:01:19.30 (31578663)|SYSTEM_METHOD_ENTRY|[7]|List<Sample_Transaction__c>.iterator() 12:01:19.30 (31689462)|SYSTEM_METHOD_EXIT|[7]|List<Sample_Transaction__c>.iterator() 12:01:19.30 (31707497)|SYSTEM_METHOD_ENTRY|[7]|system.ListIterator.hasNext() 12:01:19.30 (31720155)|HEAP_ALLOCATE|[7]|Bytes:5 12:01:19.30 (31726045)|SYSTEM_METHOD_EXIT|[7]|system.ListIterator.hasNext() 12:01:19.30 (31746559)|VARIABLE_SCOPE_BEGIN|[7]|objOpp|Sample_Transaction__c|true|false 12:01:19.30 (38745700)|VARIABLE_ASSIGNMENT|[7]|objOpp|{"LastModifiedDate":"2017-06-08T16:01:19.000Z","Transfer_Executed__c":false,"Eloqua_Order__c":false,"decile__c":0.00,"Account_ID__c":"0012400001837s0AAA","Name":"ST-0041918","Quantity__c":7,"CUSTOMER__c":"Reckitt","CreatedById":"00524000001xWetAAE","OwnerId":"00524000001xWetAAE","Completed_Transactio (4 more) ...":false,"Address_ID__c":"a012400000tS8vZAAS","Parent_id__c":"a0h0E0000004ZHFQA2","RecordTypeId":"01224000000kJPQAA2","Person_Account_Last_ (7 more) ...":"TEST","Product__c":"a0e24000007krpgAAA","ID_18__c":"a0i0E0000000YDuQAM","Sample_Lot_ID__c":"a0h0E0000004ZHFQA2","IsDeleted":false,"SAsln__c":"1234","Person_Account_Crede (9 more) ...":"MD","SystemModstamp":"2017-06-08T16:01:19.000Z","Reckitt_Professional (17 more) ...":false,"UPDATE__c":false,"Transaction_Quantity (3 more) ...":-7,"CreatedDate":"2017-06-08T05:32:15.000Z","Id":"a0i0E0000000YDuQAM","Status__c":"Acknowledged","LastModifiedById":"00524000001xWetAAE","Person_Account_First (8 more) ...":"ROMAN"}|0x37505a2b 12:01:19.30 (38757544)|STATEMENT_EXECUTE|[7] 12:01:19.30 (38759366)|STATEMENT_EXECUTE|[9] 12:01:19.30 (38820056)|SYSTEM_METHOD_ENTRY|[9]|Set<Id>.add(Object) 12:01:19.30 (38848789)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 12:01:19.30 (38858525)|SYSTEM_METHOD_EXIT|[9]|Set<Id>.add(Object) 12:01:19.30 (38867374)|SYSTEM_METHOD_ENTRY|[7]|system.ListIterator.hasNext() 12:01:19.30 (38874679)|HEAP_ALLOCATE|[7]|Bytes:5 12:01:19.30 (38880079)|SYSTEM_METHOD_EXIT|[7]|system.ListIterator.hasNext() 12:01:19.30 (38889982)|VARIABLE_ASSIGNMENT|[7]|objOpp|null| 12:01:19.30 (38893549)|STATEMENT_EXECUTE|[12] 12:01:19.30 (38897011)|HEAP_ALLOCATE|[12]|Bytes:20 12:01:19.30 (38907145)|SYSTEM_METHOD_ENTRY|[12]|System.debug(ANY) 12:01:19.30 (38920219)|USER_DEBUG|[12]|DEBUG|22222222222222222222 12:01:19.30 (38923863)|SYSTEM_METHOD_EXIT|[12]|System.debug(ANY) 12:01:19.30 (38926237)|STATEMENT_EXECUTE|[13] 12:01:19.30 (38938317)|VARIABLE_ASSIGNMENT|[13]|this.Sum|null|0x5c1aa4b9 12:01:19.30 (38940403)|STATEMENT_EXECUTE|[14] 12:01:19.30 (38969011)|HEAP_ALLOCATE|[14]|Bytes:28 12:01:19.30 (38971415)|HEAP_ALLOCATE|[14]|Bytes:28 12:01:19.30 (38982491)|VARIABLE_ASSIGNMENT|[14]|this.Sum|0|0x5c1aa4b9 12:01:19.30 (38984374)|STATEMENT_EXECUTE|[15] 12:01:19.30 (38986798)|HEAP_ALLOCATE|[15]|Bytes:5 12:01:19.30 (38992695)|SYSTEM_METHOD_ENTRY|[15]|System.debug(ANY) 12:01:19.30 (39000545)|USER_DEBUG|[15]|DEBUG|99999 12:01:19.30 (39003958)|SYSTEM_METHOD_EXIT|[15]|System.debug(ANY) 12:01:19.30 (39007533)|HEAP_ALLOCATE|[16]|Bytes:97 12:01:19.30 (39016226)|HEAP_ALLOCATE|[16]|Bytes:4 12:01:19.30 (39024004)|HEAP_ALLOCATE|[16]|Bytes:7 12:01:19.30 (50334404)|SYSTEM_METHOD_ENTRY|[16]|Database.QueryLocator.iterator() 12:01:19.30 (50445220)|HEAP_ALLOCATE|[16]|Bytes:49 12:01:19.30 (50452924)|HEAP_ALLOCATE|[16]|Bytes:8 12:01:19.30 (50463199)|SYSTEM_METHOD_ENTRY|[7]|QueryLocatorIterator.QueryLocatorIterator() 12:01:19.30 (50465890)|STATEMENT_EXECUTE|[7] 12:01:19.30 (50474554)|SYSTEM_METHOD_EXIT|[7]|QueryLocatorIterator 12:01:19.30 (50480373)|HEAP_ALLOCATE|[16]|Bytes:28 12:01:19.30 (50494637)|HEAP_ALLOCATE|[16]|Bytes:8 12:01:19.30 (50496572)|HEAP_ALLOCATE|[16]|Bytes:8 12:01:19.30 (50500705)|VARIABLE_SCOPE_BEGIN|[15]|this|Database.QueryLocatorIterator|true|false 12:01:19.30 (50524257)|VARIABLE_ASSIGNMENT|[15]|this|{}|0x690e2c0a 12:01:19.30 (50527356)|VARIABLE_SCOPE_BEGIN|[15]|values|List<SObject>|true|false 12:01:19.30 (50531142)|VARIABLE_ASSIGNMENT|[15]|values|null| 12:01:19.30 (50532804)|VARIABLE_SCOPE_BEGIN|[15]|ql|Database.QueryLocator|true|false 12:01:19.30 (50544654)|VARIABLE_ASSIGNMENT|[15]|ql|{"query":"SELECT Id, Sample_Lo (77 more) ..."}|0x6d1b4d01 12:01:19.30 (50546890)|VARIABLE_SCOPE_BEGIN|[15]|totalNumRecords|Integer|false|false 12:01:19.30 (50550741)|VARIABLE_ASSIGNMENT|[15]|totalNumRecords|0 12:01:19.30 (50552460)|VARIABLE_SCOPE_BEGIN|[15]|queryMoreSize|Integer|false|false 12:01:19.30 (50554658)|VARIABLE_ASSIGNMENT|[15]|queryMoreSize|50000 12:01:19.30 (50621269)|HEAP_ALLOCATE|[16]|Bytes:28 12:01:19.30 (50627217)|SYSTEM_METHOD_EXIT|[16]|Database.QueryLocator.iterator() 12:01:19.30 (50640431)|METHOD_ENTRY|[16]||Database.QueryLocatorIterator.hasNext() 12:01:19.30 (50657199)|METHOD_EXIT|[16]||Database.QueryLocatorIterator.hasNext() 12:01:19.30 (50665268)|VARIABLE_ASSIGNMENT|[16]|sample_transaction|null| 12:01:19.30 (50668244)|STATEMENT_EXECUTE|[16] 12:01:19.30 (50681946)|STATEMENT_EXECUTE|[30] 12:01:19.30 (50684804)|HEAP_ALLOCATE|[30]|Bytes:11 12:01:19.30 (50692708)|SYSTEM_METHOD_ENTRY|[30]|System.debug(ANY) 12:01:19.30 (50705299)|USER_DEBUG|[30]|DEBUG|66666666666 12:01:19.30 (50708868)|SYSTEM_METHOD_EXIT|[30]|System.debug(ANY) 12:01:19.30 (51597519)|CODE_UNIT_FINISHED|quantityleft on Sample_Transaction trigger event AfterUpdate for [a0i0E0000000YDu] 12:01:19.30 (52432397)|EXECUTION_FINISHED 12:01:19.53 (53773759)|USER_INFO|[EXTERNAL]|00524000001xWet|somasundaram.s@quintiles.com.prod.training|Eastern Standard Time|GMT-04:00 12:01:19.53 (53783692)|EXECUTION_STARTED 12:01:19.53 (53786925)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:01I24000001drLB 12:01:19.53 (61373722)|WF_RULE_EVAL_BEGIN|Workflow 12:01:19.53 (61390780)|WF_CRITERIA_BEGIN|[Sample Transaction: ST-0041918 a0i0E0000000YDu]|Process_Submitted_Sample_Transactions30124000000QTOx|01Q24000000VBjh|ON_ALL_CHANGES|0 12:01:19.53 (61462815)|WF_FORMULA|Formula:ENCODED:[treatNullAsNull]true|Values: 12:01:19.53 (61466748)|WF_CRITERIA_END|true 12:01:19.53 (62001937)|WF_CRITERIA_BEGIN|[Sample Transaction: ST-0041918 a0i0E0000000YDu]|Process_Sample_Transaction_Reckitt_Orders30124000000QWgL|01Q24000000VDWp|ON_ALL_CHANGES|0 12:01:19.53 (62035143)|WF_FORMULA|Formula:ENCODED:[treatNullAsNull]true|Values: 12:01:19.53 (62038074)|WF_CRITERIA_END|true 12:01:19.53 (62046091)|WF_CRITERIA_BEGIN|[Sample Transaction: ST-0041918 a0i0E0000000YDu]|Process_Sample_Transaction_Reckitt_Transfer30124000000DIGt|01Q24000000LRlR|ON_RECURSIVE_UPDATE|0 12:01:19.53 (62062334)|WF_FORMULA|Formula:ENCODED:[treatNullAsNull]true|Values: 12:01:19.53 (62064035)|WF_CRITERIA_END|true 12:01:19.53 (62073606)|WF_SPOOL_ACTION_BEGIN|Workflow 12:01:19.53 (62092077)|WF_ACTION| Flow Trigger: 3; 12:01:19.53 (62094159)|WF_RULE_EVAL_END 12:01:19.53 (62655833)|WF_FLOW_ACTION_BEGIN|09L240000000Mti 12:01:19.53 (62671474)|WF_FLOW_ACTION_DETAIL|09L240000000Mti|[Sample Transaction: ST-0041918 a0i0E0000000YDu]|Id=09L240000000Mti|CurrentRule:Process_Sample_Transaction_Reckitt_Transfer30124000000DIGt (Id=01Q24000000LRlR) 12:01:19.62 (62708401)|FLOW_CREATE_INTERVIEW_BEGIN|00D0E0000000PY9|30024000000PRiy|30124000000QTOx 12:01:19.62 (64011524)|FLOW_CREATE_INTERVIEW_END|94107b55bff6c64be466b65d59e15c886cf6ab-5d20|Process Submitted Sample Transactions 12:01:19.53 (64254243)|WF_FLOW_ACTION_DETAIL|Param Name: myVariable_current, Param Value: ENCODED:{![treatNullAsNull]{!ID:this}}, Evaluated Param Value: {Entity type: Sample_Transaction__c, id: a0i0E0000000YDuQAM}|Param Name: myVariable_old, Param Value: {!old}, Evaluated Param Value: {Entity type: Sample_Transaction__c, id: a0i0E0000000YDuQAM} 12:01:19.64 (64296997)|FLOW_START_INTERVIEWS_BEGIN|1 12:01:19.64 (65090375)|FLOW_START_INTERVIEW_BEGIN|94107b55bff6c64be466b65d59e15c886cf6ab-5d20|Process Submitted Sample Transactions 12:01:19.64 (65117305)|FLOW_VALUE_ASSIGNMENT|94107b55bff6c64be466b65d59e15c886cf6ab-5d20|myVariable_old|moduleapi.interaction.SObjectRowWithSavePoint@561339df 12:01:19.64 (66046237)|FLOW_VALUE_ASSIGNMENT|94107b55bff6c64be466b65d59e15c886cf6ab-5d20|myVariable_current|{Id=a0i0E0000000YDuQAM, OwnerId=00524000001xWetAAE, IsDeleted=false, Name=ST-0041918, RecordTypeId=01224000000kJPQAA2, CreatedDate=2017-06-08 05:32:15, CreatedById=00524000001xWetAAE, LastModifiedDate=2017-06-08 16:01:19, LastModifiedById=00524000001xWetAAE, SystemModstamp=2017-06-08 16:01:19, LastViewedDate=null, LastReferencedDate=null, ConnectionReceivedId=null, ConnectionReceivedDate=null, ConnectionSentId=null, ConnectionSentDate=null, Account_Credentials__c=null, Account_Email__c=null, Account_First_Name__c=null, Account_ID__c=0012400001837s0AAA, Account_Last_Name__c=null, Account_Middle_Name__c=null, Account_NPI__c=null, Account_Phone__c=null, Account_Salutation__c=null, Address_Address_Line_1__c=null, Address_Address_Line_2__c=null, Address_City__c=null, SAsln__c=1234, Address_ID__c=a012400000tS8vZAAS, Address_State__c=null, Call_Date__c=null, Address_ZIP__c=null, CUSTOMER__c=Reckitt, Call_ID__c=null, Call_Submitted_Date__c=null, Comments__c=null, Confirmed_Quantity__c=null, Eloqua_Order__c=false, Person_Account_Credentials__c=MD, Person_Account_First_Name__c=ROMAN, Person_Account_Last_Name__c=TEST, Person_Account_Middle_Name__c=null, Person_Account_NPI__c=null, Person_Account_Phone__c=null, Person_Account_Salutation__c=null, Product_Name__c=null, Product__c=a0e24000007krpgAAA, Quantity__c=7.0, Return_Reason__c=null, Sample_Lot_ID__c=a0h0E0000004ZHFQA2, Status__c=Acknowledged, Tracking_Number__c=null, Transaction_Date__c=null, Transaction_Quantity__c=-7.0, Transfer_Executed__c=false, Transfer_To__c=null, Transferred_From__c=null, Account_SLN__c=null, Sample_Lot_for_Promotional_Product__c=null, Product_Description__c=null, ID_18__c=a0i0E0000000YDuQAM, UPDATE__c=false, Completed_Transaction__c=false, Reckitt_Professional_Website_Order__c=false, AMA__c=null, decile__c=0.0, Parent_id__c=a0h0E0000004ZHFQA2} 12:01:19.64 (66109230)|FLOW_ELEMENT_BEGIN|94107b55bff6c64be466b65d59e15c886cf6ab-5d20|FlowAssignment|myVariable_waitStartTimeAssignment 12:01:19.64 (66163161)|FLOW_ASSIGNMENT_DETAIL|94107b55bff6c64be466b65d59e15c886cf6ab-5d20|myVariable_waitStartTimeVariable|ASSIGN|6/8/2017 12:01 PM 12:01:19.64 (66173207)|FLOW_VALUE_ASSIGNMENT|94107b55bff6c64be466b65d59e15c886cf6ab-5d20|myVariable_waitStartTimeVariable|2017-06-08T16:01:19Z 12:01:19.64 (66180494)|FLOW_ELEMENT_END|94107b55bff6c64be466b65d59e15c886cf6ab-5d20|FlowAssignment|myVariable_waitStartTimeAssignment 12:01:19.64 (66193050)|FLOW_ELEMENT_BEGIN|94107b55bff6c64be466b65d59e15c886cf6ab-5d20|FlowDecision|myDecision 12:01:19.64 (66412086)|FLOW_RULE_DETAIL|94107b55bff6c64be466b65d59e15c886cf6ab-5d20|myRule_1|false 12:01:19.64 (66421571)|FLOW_VALUE_ASSIGNMENT|94107b55bff6c64be466b65d59e15c886cf6ab-5d20|myRule_1|false 12:01:19.64 (66428726)|FLOW_ELEMENT_END|94107b55bff6c64be466b65d59e15c886cf6ab-5d20|FlowDecision|myDecision 12:01:19.64 (66517218)|FLOW_START_INTERVIEW_END|94107b55bff6c64be466b65d59e15c886cf6ab-5d20|Process Submitted Sample Transactions 12:01:19.64 (66527465)|FLOW_START_INTERVIEWS_END|1 12:01:19.53 (67174704)|WF_FLOW_ACTION_END|09L240000000Mti 12:01:19.53 (67186527)|WF_FLOW_ACTION_BEGIN|09L24000000LAZO 12:01:19.53 (67198199)|WF_FLOW_ACTION_DETAIL|09L24000000LAZO|[Sample Transaction: ST-0041918 a0i0E0000000YDu]|Id=09L24000000LAZO|CurrentRule:Process_Sample_Transaction_Reckitt_Transfer30124000000DIGt (Id=01Q24000000LRlR) 12:01:19.67 (67227591)|FLOW_CREATE_INTERVIEW_BEGIN|00D0E0000000PY9|30024000000PRiw|30124000000DIGt 12:01:19.67 (68393264)|FLOW_CREATE_INTERVIEW_END|94207b55bff6c64be466b65d59e15c886cf6ab-5d1f|Process Sample Transaction: Reckitt Transfer 12:01:19.53 (68541500)|WF_FLOW_ACTION_DETAIL|Param Name: myVariable_current, Param Value: ENCODED:{![treatNullAsNull]{!ID:this}}, Evaluated Param Value: {Entity type: Sample_Transaction__c, id: a0i0E0000000YDuQAM}|Param Name: RecursiveCountVariable, Param Value: {!triggerRecurseCount}, Evaluated Param Value: 0|Param Name: myVariable_old, Param Value: {!old}, Evaluated Param Value: {Entity type: Sample_Transaction__c, id: a0i0E0000000YDuQAM} 12:01:19.68 (68579575)|FLOW_START_INTERVIEWS_BEGIN|1 12:01:19.68 (69193856)|FLOW_START_INTERVIEW_BEGIN|94207b55bff6c64be466b65d59e15c886cf6ab-5d1f|Process Sample Transaction: Reckitt Transfer 12:01:19.68 (69218650)|FLOW_VALUE_ASSIGNMENT|94207b55bff6c64be466b65d59e15c886cf6ab-5d1f|myVariable_old|moduleapi.interaction.SObjectRowWithSavePoint@6748044e 12:01:19.68 (70035371)|FLOW_VALUE_ASSIGNMENT|94207b55bff6c64be466b65d59e15c886cf6ab-5d1f|myVariable_current|{Id=a0i0E0000000YDuQAM, OwnerId=00524000001xWetAAE, IsDeleted=false, Name=ST-0041918, RecordTypeId=01224000000kJPQAA2, CreatedDate=2017-06-08 05:32:15, CreatedById=00524000001xWetAAE, LastModifiedDate=2017-06-08 16:01:19, LastModifiedById=00524000001xWetAAE, SystemModstamp=2017-06-08 16:01:19, LastViewedDate=null, LastReferencedDate=null, ConnectionReceivedId=null, ConnectionReceivedDate=null, ConnectionSentId=null, ConnectionSentDate=null, Account_Credentials__c=null, Account_Email__c=null, Account_First_Name__c=null, Account_ID__c=0012400001837s0AAA, Account_Last_Name__c=null, Account_Middle_Name__c=null, Account_NPI__c=null, Account_Phone__c=null, Account_Salutation__c=null, Address_Address_Line_1__c=null, Address_Address_Line_2__c=null, Address_City__c=null, SAsln__c=1234, Address_ID__c=a012400000tS8vZAAS, Address_State__c=null, Call_Date__c=null, Address_ZIP__c=null, CUSTOMER__c=Reckitt, Call_ID__c=null, Call_Submitted_Date__c=null, Comments__c=null, Confirmed_Quantity__c=null, Eloqua_Order__c=false, Person_Account_Credentials__c=MD, Person_Account_First_Name__c=ROMAN, Person_Account_Last_Name__c=TEST, Person_Account_Middle_Name__c=null, Person_Account_NPI__c=null, Person_Account_Phone__c=null, Person_Account_Salutation__c=null, Product_Name__c=null, Product__c=a0e24000007krpgAAA, Quantity__c=7.0, Return_Reason__c=null, Sample_Lot_ID__c=a0h0E0000004ZHFQA2, Status__c=Acknowledged, Tracking_Number__c=null, Transaction_Date__c=null, Transaction_Quantity__c=-7.0, Transfer_Executed__c=false, Transfer_To__c=null, Transferred_From__c=null, Account_SLN__c=null, Sample_Lot_for_Promotional_Product__c=null, Product_Description__c=null, ID_18__c=a0i0E0000000YDuQAM, UPDATE__c=false, Completed_Transaction__c=false, Reckitt_Professional_Website_Order__c=false, AMA__c=null, decile__c=0.0, Parent_id__c=a0h0E0000004ZHFQA2} 12:01:19.68 (70046220)|FLOW_VALUE_ASSIGNMENT|94207b55bff6c64be466b65d59e15c886cf6ab-5d1f|RecursiveCountVariable|0 12:01:19.68 (70081805)|FLOW_ELEMENT_BEGIN|94207b55bff6c64be466b65d59e15c886cf6ab-5d1f|FlowAssignment|myVariable_waitStartTimeAssignment 12:01:19.68 (70100208)|FLOW_ASSIGNMENT_DETAIL|94207b55bff6c64be466b65d59e15c886cf6ab-5d1f|myVariable_waitStartTimeVariable|ASSIGN|6/8/2017 12:01 PM 12:01:19.68 (70107239)|FLOW_VALUE_ASSIGNMENT|94207b55bff6c64be466b65d59e15c886cf6ab-5d1f|myVariable_waitStartTimeVariable|2017-06-08T16:01:19Z 12:01:19.68 (70111293)|FLOW_ELEMENT_END|94207b55bff6c64be466b65d59e15c886cf6ab-5d1f|FlowAssignment|myVariable_waitStartTimeAssignment 12:01:19.68 (70116544)|FLOW_ELEMENT_BEGIN|94207b55bff6c64be466b65d59e15c886cf6ab-5d1f|FlowDecision|myDecision 12:01:19.68 (82128969)|FLOW_RULE_DETAIL|94207b55bff6c64be466b65d59e15c886cf6ab-5d1f|myRule_1|false 12:01:19.68 (82137015)|FLOW_VALUE_ASSIGNMENT|94207b55bff6c64be466b65d59e15c886cf6ab-5d1f|myRule_1|false 12:01:19.68 (82143529)|FLOW_ELEMENT_END|94207b55bff6c64be466b65d59e15c886cf6ab-5d1f|FlowDecision|myDecision 12:01:19.68 (82205880)|FLOW_START_INTERVIEW_END|94207b55bff6c64be466b65d59e15c886cf6ab-5d1f|Process Sample Transaction: Reckitt Transfer 12:01:19.68 (82216146)|FLOW_START_INTERVIEWS_END|1 12:01:19.53 (82691566)|WF_FLOW_ACTION_END|09L24000000LAZO 12:01:19.53 (82707201)|WF_FLOW_ACTION_BEGIN|09L240000000NnY 12:01:19.53 (82723049)|WF_FLOW_ACTION_DETAIL|09L240000000NnY|[Sample Transaction: ST-0041918 a0i0E0000000YDu]|Id=09L240000000NnY|CurrentRule:Process_Sample_Transaction_Reckitt_Transfer30124000000DIGt (Id=01Q24000000LRlR) 12:01:19.82 (82754512)|FLOW_CREATE_INTERVIEW_BEGIN|00D0E0000000PY9|30024000000PRiv|30124000000QWgL 12:01:19.82 (84677324)|FLOW_CREATE_INTERVIEW_END|94307b55bff6c64be466b65d59e15c886cf6ab-5d1e|Process Sample Transaction: Reckitt Orders 12:01:19.53 (84945981)|WF_FLOW_ACTION_DETAIL|Param Name: myVariable_current, Param Value: ENCODED:{![treatNullAsNull]{!ID:this}}, Evaluated Param Value: {Entity type: Sample_Transaction__c, id: a0i0E0000000YDuQAM}|Param Name: myVariable_old, Param Value: {!old}, Evaluated Param Value: {Entity type: Sample_Transaction__c, id: a0i0E0000000YDuQAM} 12:01:19.85 (85003610)|FLOW_START_INTERVIEWS_BEGIN|1 12:01:19.85 (85935992)|FLOW_START_INTERVIEW_BEGIN|94307b55bff6c64be466b65d59e15c886cf6ab-5d1e|Process Sample Transaction: Reckitt Orders 12:01:19.85 (85956174)|FLOW_VALUE_ASSIGNMENT|94307b55bff6c64be466b65d59e15c886cf6ab-5d1e|myVariable_old|moduleapi.interaction.SObjectRowWithSavePoint@7d6cd02c 12:01:19.85 (87530116)|FLOW_VALUE_ASSIGNMENT|94307b55bff6c64be466b65d59e15c886cf6ab-5d1e|myVariable_current|{Id=a0i0E0000000YDuQAM, OwnerId=00524000001xWetAAE, IsDeleted=false, Name=ST-0041918, RecordTypeId=01224000000kJPQAA2, CreatedDate=2017-06-08 05:32:15, CreatedById=00524000001xWetAAE, LastModifiedDate=2017-06-08 16:01:19, LastModifiedById=00524000001xWetAAE, SystemModstamp=2017-06-08 16:01:19, LastViewedDate=null, LastReferencedDate=null, ConnectionReceivedId=null, ConnectionReceivedDate=null, ConnectionSentId=null, ConnectionSentDate=null, Account_Credentials__c=null, Account_Email__c=null, Account_First_Name__c=null, Account_ID__c=0012400001837s0AAA, Account_Last_Name__c=null, Account_Middle_Name__c=null, Account_NPI__c=null, Account_Phone__c=null, Account_Salutation__c=null, Address_Address_Line_1__c=null, Address_Address_Line_2__c=null, Address_City__c=null, SAsln__c=1234, Address_ID__c=a012400000tS8vZAAS, Address_State__c=null, Call_Date__c=null, Address_ZIP__c=null, CUSTOMER__c=Reckitt, Call_ID__c=null, Call_Submitted_Date__c=null, Comments__c=null, Confirmed_Quantity__c=null, Eloqua_Order__c=false, Person_Account_Credentials__c=MD, Person_Account_First_Name__c=ROMAN, Person_Account_Last_Name__c=TEST, Person_Account_Middle_Name__c=null, Person_Account_NPI__c=null, Person_Account_Phone__c=null, Person_Account_Salutation__c=null, Product_Name__c=null, Product__c=a0e24000007krpgAAA, Quantity__c=7.0, Return_Reason__c=null, Sample_Lot_ID__c=a0h0E0000004ZHFQA2, Status__c=Acknowledged, Tracking_Number__c=null, Transaction_Date__c=null, Transaction_Quantity__c=-7.0, Transfer_Executed__c=false, Transfer_To__c=null, Transferred_From__c=null, Account_SLN__c=null, Sample_Lot_for_Promotional_Product__c=null, Product_Description__c=null, ID_18__c=a0i0E0000000YDuQAM, UPDATE__c=false, Completed_Transaction__c=false, Reckitt_Professional_Website_Order__c=false, AMA__c=null, decile__c=0.0, Parent_id__c=a0h0E0000004ZHFQA2} 12:01:19.85 (87598837)|FLOW_ELEMENT_BEGIN|94307b55bff6c64be466b65d59e15c886cf6ab-5d1e|FlowAssignment|myVariable_waitStartTimeAssignment 12:01:19.85 (87629000)|FLOW_ASSIGNMENT_DETAIL|94307b55bff6c64be466b65d59e15c886cf6ab-5d1e|myVariable_waitStartTimeVariable|ASSIGN|6/8/2017 12:01 PM 12:01:19.85 (87641764)|FLOW_VALUE_ASSIGNMENT|94307b55bff6c64be466b65d59e15c886cf6ab-5d1e|myVariable_waitStartTimeVariable|2017-06-08T16:01:19Z 12:01:19.85 (87649752)|FLOW_ELEMENT_END|94307b55bff6c64be466b65d59e15c886cf6ab-5d1e|FlowAssignment|myVariable_waitStartTimeAssignment 12:01:19.85 (87659277)|FLOW_ELEMENT_BEGIN|94307b55bff6c64be466b65d59e15c886cf6ab-5d1e|FlowDecision|myDecision 12:01:19.85 (88096939)|FLOW_RULE_DETAIL|94307b55bff6c64be466b65d59e15c886cf6ab-5d1e|myRule_1|false 12:01:19.85 (88106310)|FLOW_VALUE_ASSIGNMENT|94307b55bff6c64be466b65d59e15c886cf6ab-5d1e|myRule_1|false 12:01:19.85 (88114079)|FLOW_ELEMENT_END|94307b55bff6c64be466b65d59e15c886cf6ab-5d1e|FlowDecision|myDecision 12:01:19.85 (88195998)|FLOW_START_INTERVIEW_END|94307b55bff6c64be466b65d59e15c886cf6ab-5d1e|Process Sample Transaction: Reckitt Orders 12:01:19.85 (88210934)|FLOW_START_INTERVIEWS_END|1 12:01:19.53 (88699788)|WF_FLOW_ACTION_END|09L240000000NnY 12:01:19.53 (88771927)|WF_ACTIONS_END| Flow Trigger: 3; 12:01:19.53 (88777123)|CODE_UNIT_FINISHED|Workflow:01I24000001drLB 12:01:19.53 (89620399)|EXECUTION_FINISHED
Somasundaram S 1Somasundaram S 1
I didnt write the test class is it the reason the code didnt run , please advise Rafeal deve
Rafael.Martins.SantosRafael.Martins.Santos
The error that occurs is because de field is empty

Apex trigger quantityleft caused an unexpected exception, contact your administrator: quantityleft: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.quantityleft: line 17, column 1

probally the field "Transaction_Quantity__c" is empty. put some value in this field like "0".



 
Rafael.Martins.SantosRafael.Martins.Santos
Try debug the values too.
For example:
System.debug('Trasaction value: ' + sample_transaction.Transaction_Quantity__c);

So that you will see what is going on and what is missing.
Somasundaram S 1Somasundaram S 1
its printing value of transcation quanitty 
but when the line start line no 16 its not executing after that it goes last line i mena what ever i mentioned system debug its not printing 

for(Sample_Transaction__c sample_transaction : [SELECT Id,Sample_Lot_ID__c, 
Rafael.Martins.SantosRafael.Martins.Santos
Are you using this code?

for(Sample_Transaction__c sample_transaction : [SELECT Id,Sample_Lot_ID__c, Confirmed_Quantity__c FROM Sample_Transaction__c        WHERE Id = : set_Opportunity]){

if yes, try print the value of "set_Opportunity", before getting in this for.
I think this for is not executed because in the query don't found any register.
 
Somasundaram S 1Somasundaram S 1
Raefell Good news :) i fail to assign sum value i set to 0 and finally the value is updating but it update only the last value of child transaction

i think we limit to 1 
for(Sample_Transaction__c sample_transaction : [SELECT Id,Sample_Lot_ID__c, Transaction_Quantity__c FROM Sample_Transaction__c WHERE Id = : Trigger.newMap.keyset() limit 1 if i replace it with set_oppurtunity no result is coming 
trigger quantityleft on Sample_Transaction__c (after insert,after update,after delete,after undelete) {

 

    List<Sample_Lot__c> list_Account= new List<Sample_Lot__c>();

 

     set<Id> set_Opportunity = new set<Id>();

 

         for(Sample_Transaction__c objOpp: trigger.new){

 

        set_Opportunity.add(objOpp.Parent_id__c);

 

    }

 

        Decimal Sum;
       Sum=0;

  for(Sample_Transaction__c sample_transaction : [SELECT Id,Sample_Lot_ID__c, Transaction_Quantity__c FROM Sample_Transaction__c WHERE Id = : Trigger.newMap.keyset() limit 1

]){


 

            Sum+= sample_transaction.Transaction_Quantity__c;
System.debug('SOMUUUUs: ' + Sum);
 

        for(Sample_Lot__c objAccount : [SELECT Id, Quantity_Left__c FROM Sample_Lot__c WHERE Id = : sample_transaction.Sample_Lot_ID__c]){

 

            objAccount.Quantity_left__c = Sum ;

            list_Account.add(objAccount);
System.debug('middle: ' + Sum);
 

        }

    }
System.debug('last: ' + Sum);
    update list_Account;

}




 
Rafael.Martins.SantosRafael.Martins.Santos
The "Trigger.newMap.keyset() limit 1" is getting the current ID of the register.
You have to get another brothers of this register.

So you have a "for" tha get the current child data, and a second for that is getting the data of the parent of this register.

You can create now another "for" inside of the parent "for" to read all the child register.
Doing that you use the SUM in the last "for" to store the values of all child register, and after that you can update the parent field with the total SUM of these registers.
Somasundaram S 1Somasundaram S 1
Ton of thanks to Rafael atlast i achived it thanks once again for your support and patient.
now i will write test case , so i need all the record to create sample and sample lot, if sample is refered account in lookup i have to create account am i right  
trigger quantityleft on Sample_Transaction__c (after insert,after update,after delete,after undelete) {
    List<Sample_Lot__c> list_Account= new List<Sample_Lot__c>();
    set<Id> set_Opportunity = new set<Id>();
         for(Sample_Transaction__c objOpp: trigger.new){
         set_Opportunity.add(objOpp.Parent_id__c);
   }
        Decimal Sum;
       Sum=0;

  for(Sample_Transaction__c sample_transaction : [SELECT id,Sample_Lot_ID__c, Transaction_Quantity__c FROM Sample_Transaction__c WHERE Parent_id__c = : set_Opportunity

]){
            
            Sum+= sample_transaction.Transaction_Quantity__c;
  
    
    for(Sample_Lot__c objAccount : [SELECT Id, Quantity_Left__c FROM Sample_Lot__c WHERE Id = : sample_transaction.Sample_Lot_ID__c]){

 

            objAccount.Quantity_left__c = Sum ;

            list_Account.add(objAccount);

 update objAccount ;

        }
        
 }


    
}

 
Rafael.Martins.SantosRafael.Martins.Santos
Hi,
It's a pleasure to help.
I don't understand well what you mean about create account.
If your Sample and Sample lot are child of account, depending what you want do, you don't need use account. But if you have to bring a list of account, so you will need make a call from object Account too.
Somasundaram S 1Somasundaram S 1
ya i changed it as per your suggestion and very curious to know about the governor limit , will governor limit affect the below code, some time child record may have more than 400 records, how to handle it in my code Rafael
 
rigger quantityleft on Sample_Transaction__c (after insert,after update,after delete,after undelete) {
    List<Sample_Lot__c> list_lot= new List<Sample_Lot__c>();
    set<Id> set_Opportunity = new set<Id>();
     if(Trigger.isInsert || Trigger.isUndelete){
        for(Sample_Transaction__c objOpp: trigger.new){
        
        if(objOpp.Completed_Transaction__c == true){
         set_Opportunity.add(objOpp.Parent_id__c);
           }
    }
    }
    
    if(Trigger.isUpdate){
        for(Sample_Transaction__c objOpp: trigger.new){
        
        if(objOpp.Completed_Transaction__c == true){
         set_Opportunity.add(objOpp.Parent_id__c);
           }
  }
    
    
    }
    
    if(Trigger.isdelete){
        for(Sample_Transaction__c objOpp: trigger.old){
        
        if(objOpp.Completed_Transaction__c == true){
         set_Opportunity.add(objOpp.Parent_id__c);
           }
  
    }
    
    }

    if(!set_Opportunity.isEmpty()){
    
    
    
                 Decimal Sum;
       Sum=0;

  for(Sample_Transaction__c sample_transaction : [SELECT id,Sample_Lot_ID__c, Transaction_Quantity__c FROM Sample_Transaction__c WHERE Parent_id__c = : set_Opportunity

]){
            
            Sum+= sample_transaction.Transaction_Quantity__c;
  
    
    for(Sample_Lot__c objLot: [SELECT Id, Quantity_Left__c FROM Sample_Lot__c WHERE Id = : sample_transaction.Sample_Lot_ID__c]){

 

            objlot.Quantity_left__c = Sum ;

            list_lot.add(objlot);

 update objLot;

        }
        
 }
}

    
}

 
Rafael.Martins.SantosRafael.Martins.Santos
Does it occurs some message?
The limit depends of what you want.
For example:
You need especify the record Id in the SELECT on "for". So will return a list of records that you want.
If for example, you have a record of Sample Lot with Id "ABC123", and this record have many other records making realationship with him, so will bring all records. You can filter more if you pull a condition in the query.
Somasundaram S 1Somasundaram S 1
how many soql governer will allow i guess my case it may go 1000+  i cant filter further also and how many update i can do in this scenario how to handle 
Rafael.Martins.SantosRafael.Martins.Santos
I think the limit is 10000, I'm not shure.
About the update, I think you can do as many as you can, but you need implement a validation to not getting a loop pn your updates.

Create another class e call this method to update only one time each record.

public Class checkRecursive{
   
    private static boolean run = true;
   
    public static boolean runOnce(){
       
    if(run){
     run=false;
     return true;  
    }
    else{
        return run;
    }
       
    }
}
Somasundaram S 1Somasundaram S 1
the below is my final code and it covered governor limit also tested and its working fine
now my question is this code has after insert , update,delete, undelte  i have given my test code too , i just inserted the required record in test code and it shows 82% code coverage. My test code ddint check anything ,if i move this code in production will it work
and also advise me how to cover after insert,update,delete undelte in my test code 

 
trigger quantityleft1 on Sample_Transaction__c ( after INSERT, after UPDATE, after DELETE, after UNDELETE ) {
   List<Sample_Lot__c> list_lot= new List<Sample_Lot__c>();
     Set<Id> set_transaction = new Set<Id>();
     Decimal Sum = 0;
     if( Trigger.isInsert || Trigger.isUndelete ){
        for( Sample_Transaction__c objOpp: Trigger.new ) {
            if( objOpp.Completed_Transaction__c == TRUE ){
                set_transaction.add( objOpp.Parent_id__c );
            }
        }
    }
 

    if( Trigger.isUpdate ) {

        for( Sample_Transaction__c objOpp: Trigger.new ) {

            if( objOpp.Completed_Transaction__c == TRUE ) {

                set_transaction.add( objOpp.Parent_id__c );

            }

        }

    }
 

    if( Trigger.isdelete ) {

       for( Sample_Transaction__c objOpp: Trigger.old ) {

            if( objOpp.Completed_Transaction__c == TRUE ) {

                set_transaction.add( objOpp.Parent_id__c );
            }
          }
    }
    List<Sample_Transaction__c> sampleTransactionRecords = new List<Sample_Transaction__c>();
    sampleTransactionRecords = [
        SELECT  Id
                ,Sample_Lot_ID__c

                ,Transaction_Quantity__c,
                Completed_Transaction__c

        FROM    Sample_Transaction__c

        WHERE   Parent_id__c = :set_transaction

    ];
    List<String> sampleLotIds = new List<String>();
    for( Sample_Transaction__c sampleTransaction : sampleTransactionRecords ) {
        sampleLotIds.add( sampleTransaction.Sample_Lot_ID__c );
    }
    List<Sample_Lot__c> sampleLotRecords = new List<Sample_Lot__c>();
    sampleLotRecords = [
        SELECT  Id
                ,Name
                ,Quantity_Left__c
        FROM    Sample_Lot__c
        WHERE   Id IN :set_transaction

    ];


    Map<String,Sample_Lot__c> mapOfSampleLots = new Map<String,Sample_Lot__c>();

    for( Sample_Lot__c sampleLot : sampleLotRecords ) {

        mapOfSampleLots.put( sampleLot.Id, sampleLot );

    }
    

    if( !set_transaction.isEmpty() ) {

        List<Sample_Lot__c> sampleLotsToUpdate = new List<Sample_Lot__c>();

        
        for( Sample_Transaction__c sample_transaction : sampleTransactionRecords ) {
         if( sample_transaction.Completed_Transaction__c == TRUE ) {

            Sum += sample_transaction.Transaction_Quantity__c;

 
}
        }
}
 for( Sample_Lot__c samplelots : sampleLotRecords ) {

         samplelots.Quantity_left__c=Sum;


    }


        UPDATE sampleLotRecords;

    

}

test code

 
@isTest 
public class testquantityleft{
static testmethod void triggerTest(){
// create account
Account accountOb= new Account();
accountOb.Firstname='name';
accountOb.LastName='1';
accountOb.Credentials__c='MD';
accountOb.SLN__c='123';
accountOb.Specialty_1__c='heart';
Map <String,Schema.RecordTypeInfo> recordTypeMap = Account.sObjectType.getDescribe().getRecordTypeInfosByName();
//Account accountObj = new Account();
//accountObj.name = 'Test Record Type';
if(recordTypeMap.containsKey('Reckitt Person Account')) {
 accountOb.RecordTypeId= recordTypeMap.get('Reckitt Person Account').getRecordTypeId();
}
insert accountOb;
// create address
Address__c address = new Address__c();
address.Account__c = accountOb.id;
//Map <String,Schema.RecordTypeInfo> recordTypeMap1 = Address__c.sObjectType.getDescribe().getRecordTypeInfosByName();
//if(recordTypeMap1.containsKey('Reckitt Address')) {
 //address .RecordTypeId= recordTypeMap.get('Reckitt Address').getRecordTypeId();
//}
address.Name='somaaddress';
insert address;

// create product
Product__c prod = new Product__c();
prod.name ='test';
insert prod;
Sample_Lot__c lot=new Sample_Lot__c();
lot.Name='test lot';
lot.Product_ID__c=prod.id;
lot.Confirmed_Quantity__c=600;
lot.Quantity_Left__c=0;
insert lot;

// create sample transaction

Sample_Transaction__c stransac = new Sample_Transaction__c();
stransac.Account_ID__c = accountob.id;
stransac .Address_ID__c=address.id;
stransac .Quantity__c =100;
stransac .Product__c=prod.id;
stransac .Sample_Lot_ID__c=lot.id;
insert stransac ;
Sample_Transaction__c stransac1 = new Sample_Transaction__c();
stransac1.Account_ID__c = accountob.id;
stransac1 .Address_ID__c=address.id;
stransac1 .Quantity__c =100;
stransac1 .Product__c=prod.id;
stransac1 .Sample_Lot_ID__c=lot.id;
Map <String,Schema.RecordTypeInfo> recordTypeMap2 = Sample_Transaction__c.sObjectType.getDescribe().getRecordTypeInfosByName();
if(recordTypeMap2.containsKey('Reckitt Receipt')) {
 stransac1 .RecordTypeId= recordTypeMap2.get('Reckitt Receipt').getRecordTypeId();
}





insert stransac1 ;





}
}


 
Rafael.Martins.SantosRafael.Martins.Santos
Hi,
To check what the test code is checking, go to Developer Console, click on the test button in your Test Class. After doing that, in the bottom you will see some tabs, search for a tab called Tests, and you will see in the bottom on the right side a list of classes, find your Class you want to Test in this list and make a double click in this class.
Now you can see your class with a clolor in RED. This color will show to you the code that don't was tested.
You must cover all the code to be 100%.

But I see in your code that you make a UPDATE and DELETE trigger too, so you must create a TEST that make an UPDATE and DELETE record too.
 
Somasundaram S 1Somasundaram S 1
i will do it but how come the code coverage shows 82% its enough to move in production right
Rafael.Martins.SantosRafael.Martins.Santos
Yes, it is enough, but could occur some error in the prod in the part that is not tested.
I advise you that you try cover the maximum of your code.
For example, I created I code and a Test class that cover 98% of my code, but in the production are occured an error in another classes that I not covered 100% before.
So, if you not fix this now, maybe you must fix your code later.

But if you feelling safe to publish, go on.