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
Silpi roy 16Silpi roy 16 

Trigger for Updation

Hi All,

With thebelow code i am trying to update Billing Item Qty=0 ,Based on Document Type in Billing document in salsforce .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
trigger UpdateQty on BillingDoc__c (after insert,after update) {
Set<id> setOfParentId = new Set<Id>();
for(BillingDoc__c BD:trigger.new){
List<BillingItem__c> listBI=new List<BillingItem__c>([Select Id,BillingDocumentId__c,Quantity__c from BillingItem__c where BillingDocumentId__c in: setOfParentId]);

if ( listBI.size() > 0 )
  for(BillingItem__c BI : listBI){
   if(BD.DocumentTypeCode__c=='ZG2')
   {
   BI.Quantity__c = 0;
   }
   listBI.add(BI);
  }
 }
 

}

But it is not updating the Quatity=0.

please suggest me if it need some updation.

Thanks,
Silpi
Best Answer chosen by Silpi roy 16
PawanKumarPawanKumar
If you are using seteven code then you can try below oterwise you can adjust the highlighted code in your code. Thanks.

trigger UpdateQty on BillingDoc__c (after insert,after update) {

    List<BillingItem__c> listBI=[Select Id,BillingDocumentId__c, BillingDocumentId__r.DocumentTypeCode__c, Quantity__c from BillingItem__c where BillingDocumentId__c in: trigger.newMap.keyset()];

    List<BillingItem__c> updateList = new List<BillingItem__c>();
    
    if ( listBI.size() > 0 ) {
        for(BillingItem__c BI : listBI){
            if(BI.BillingDocumentId__r.DocumentTypeCode__c=='ZG2')
            {
              BI.Quantity__c = 0;
                updateList.add(BI);
            }
        }
        update updateList;
    }
}

All Answers

Silpi roy 16Silpi roy 16
Hi Steven,

I am trying to update child object field Quatity based on parent object.

Thanks,
Silpi
PawanKumarPawanKumar
i do not see any value in 'setOfParentId' so your query is returning null and not going to inside second for loop. Please add the value 'setOfParentId' before your first loop.

Select Id,BillingDocumentId__c,Quantity__c from BillingItem__c where BillingDocumentId__c in: setOfParentId
Steven NsubugaSteven Nsubuga
trigger UpdateQty on BillingDoc__c (after insert,after update) {

    List<BillingItem__c> listBI=[Select Id,BillingDocumentId__c, BillingDocumentId__r.DocumentTypeCode__c, Quantity__c from BillingItem__c where BillingDocumentId__c in: trigger.newMap.keyset()];

    if ( listBI.size() > 0 ) {
        for(BillingItem__c BI : listBI){
            if(BI.BillingDocumentId__r.DocumentTypeCode__c=='ZG2')
            {
              BI.Quantity__c = 0;
            }
        }
        update listBI;
    }
}
PawanKumarPawanKumar
Please post the error.
Silpi roy 16Silpi roy 16
Getting the below error:
User-added image
 
PawanKumarPawanKumar
If you are using seteven code then you can try below oterwise you can adjust the highlighted code in your code. Thanks.

trigger UpdateQty on BillingDoc__c (after insert,after update) {

    List<BillingItem__c> listBI=[Select Id,BillingDocumentId__c, BillingDocumentId__r.DocumentTypeCode__c, Quantity__c from BillingItem__c where BillingDocumentId__c in: trigger.newMap.keyset()];

    List<BillingItem__c> updateList = new List<BillingItem__c>();
    
    if ( listBI.size() > 0 ) {
        for(BillingItem__c BI : listBI){
            if(BI.BillingDocumentId__r.DocumentTypeCode__c=='ZG2')
            {
              BI.Quantity__c = 0;
                updateList.add(BI);
            }
        }
        update updateList;
    }
}
This was selected as the best answer
PawanKumarPawanKumar
for your code.

trigger UpdateQty on BillingDoc__c (after insert,after update) {
Set<id> setOfParentId = new Set<Id>();
    List<BillingItem__c> updateList = new List<BillingItem__c>();
    List<BillingItem__c> listBI=new List<BillingItem__c>([Select Id,BillingDocumentId__c,Quantity__c from BillingItem__c where BillingDocumentId__c in: setOfParentId]);
for(BillingDoc__c BD:trigger.new){
if ( listBI.size() > 0 )
  for(BillingItem__c BI : listBI){
   if(BD.DocumentTypeCode__c=='ZG2')
   {
   BI.Quantity__c = 0;
    updateList.add(BI);
   }
   //listBI.add(BI);
  }
 }
 
 update updateList;
 

}
 
Silpi roy 16Silpi roy 16
Is it not possible to acheive through Workflow.
PawanKumarPawanKumar
As per my understanding, you will not be able to query billing document and you can not make decisions based on that. That's why the trigger is helping you to overcome this limitation of WF.
Silpi roy 16Silpi roy 16
Hi ,
In case i want to convert the trigger to a helper class,How can I write the helper class and the Trigger using the helper class.