• Joy___
  • NEWBIE
  • 20 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies
Hi ! 

Can anyone tell me how to write trigger for this scenario, please ?

On Order object, I have 2 custom fields : 
   - Net_amount__c : is total amount without shipment cost
   - Shipment_cost__c

I would like to update Net_amount__c field. 


My trigger is bellow, but there are many problems :

1) When an order is created :
Total amount : 0
Net_amount__c : 
Shipment_cost__c : 100

2) When an order is updated :
Total amount : 100
Net_amount__c : -50
Shipment_cost__c : 50



trigger CalculMontantTrigger on Order (before update) {
   
    for (Order o: Trigger.new) {
        o.Net_amount__c = o.TotalAmount - o.Shipment_cost__c;
    }
}
  • March 30, 2021
  • Like
  • 0
Hi, 

I have pretty complicated situation here! 

I'm trying to write a trigger, I have to update a custom field : Annual_Sales__C ( currency type ) on Account object when an order is updating 

This is my code below : 

trigger UpdateAnnualSalesTrigger on Order (after update) {
   
    set <String> accID = New Set <String> (); 
    for (Order o: Trigger.new) { 
        if (o.AccountId != Null ) { 
            accID.add (o.AccountId); 
        } 
    } 
    for( Order o: Trigger.new){
       
        if (accID.size ()> 0) { 
            List <Account> upAccList = new List <Account> (); 
            for (Account ac:  [SELECT Id, Annual_Sales__c FROM Account WHERE id in: AccID]) { 
                ac.Annual_Sales__c = ac.Annual_Sales__c + o.TotalAmout;
                UpAccList.add (ac); 
            } 
            if (upAccList.size ()> 0) 
                update upAccList; 
        } 
    }  
}

When I update an order, an error message is display :
caused by: System.NullPointerException: Attempt to de-reference a null object :  ac.Annual_Sales__c = ac.Annual_Sales__c + o.TotalAmout;

Thanks :)
 
  • March 26, 2021
  • Like
  • 0
Hi ! 

Can anyone tell me how to write trigger for this scenario, please ?

On Order object, I have 2 custom fields : 
   - Net_amount__c : is total amount without shipment cost
   - Shipment_cost__c

I would like to update Net_amount__c field. 


My trigger is bellow, but there are many problems :

1) When an order is created :
Total amount : 0
Net_amount__c : 
Shipment_cost__c : 100

2) When an order is updated :
Total amount : 100
Net_amount__c : -50
Shipment_cost__c : 50



trigger CalculMontantTrigger on Order (before update) {
   
    for (Order o: Trigger.new) {
        o.Net_amount__c = o.TotalAmount - o.Shipment_cost__c;
    }
}
  • March 30, 2021
  • Like
  • 0
Hi, 

I have pretty complicated situation here! 

I'm trying to write a trigger, I have to update a custom field : Annual_Sales__C ( currency type ) on Account object when an order is updating 

This is my code below : 

trigger UpdateAnnualSalesTrigger on Order (after update) {
   
    set <String> accID = New Set <String> (); 
    for (Order o: Trigger.new) { 
        if (o.AccountId != Null ) { 
            accID.add (o.AccountId); 
        } 
    } 
    for( Order o: Trigger.new){
       
        if (accID.size ()> 0) { 
            List <Account> upAccList = new List <Account> (); 
            for (Account ac:  [SELECT Id, Annual_Sales__c FROM Account WHERE id in: AccID]) { 
                ac.Annual_Sales__c = ac.Annual_Sales__c + o.TotalAmout;
                UpAccList.add (ac); 
            } 
            if (upAccList.size ()> 0) 
                update upAccList; 
        } 
    }  
}

When I update an order, an error message is display :
caused by: System.NullPointerException: Attempt to de-reference a null object :  ac.Annual_Sales__c = ac.Annual_Sales__c + o.TotalAmout;

Thanks :)
 
  • March 26, 2021
  • Like
  • 0