+ Start a Discussion
Parth SrivastavaParth Srivastava 

please help in trigger

I need help with creating a field that auto-populates the “Standard Price” from the Product object into a field called “Base Price” in the Transactions custom object.
 
Use Case:
 
When a Transaction record is created, the Item Number should be looked up in the Product object "Product Code", then the "Standard Price" should be returned in the "Base Price" field
 
Example:
 
The transaction is created with item number 2200, we should look up the Product object and find the Product with Product code 2200.  Then we should return the standard price of $80.00 into the “Base Price” field.
Murali MattaMurali Matta
Hi Srivastava,

Use below code
 
trigger BasePrice on Transaction (before insert, before update, after insert, after update) {

List<ID> transIds = New List<ID>();
for(Transaction trans : Trigger.new){
 transIds.add(trans.Id);
}
if(transIds!=null && transIds.size()>0){  
List<Product> product = new List<Product>();
for(Product pro : [SELECT id, StandardPrice FROM Product WHERE ProductCode =:             transIds.ItemNumber__c]) {
             transIds.BasePrice__c = pro.StandardPrice;
             transIds.add(transIds);
           }
        if(transIds!=null && transIds.size()>0)
            update transIds;
    }
}

​​​​​​​Let me know if you have any confusion.

Kindly mark this as solved if the reply was helpful.

Thanks,
Murali
Parth SrivastavaParth Srivastava
Please Suggest as i want this trigger to be on before insert event Also here how I can compare transaction item number and the product objects product code before putting transIds.BasePrice__c = pro.StandardPrice;
Murali MattaMurali Matta
Hi Srivastava,

In query you are passing " ProductCode =:transIds.ItemNumber__c".
This checks if transIds.ItemNumber__c is equal to ProductCode.
Thanks,
Murali
Parth SrivastavaParth Srivastava
 ProductCode =:transIds.ItemNumber__c gives error as Variable does not exist: Item_Number__c
Abhishek Raj 23Abhishek Raj 23
Hi,

I did some modification in Murali's code. Please try this and let me know if it works
trigger BasePrice on Transaction (before insert, before update, after insert, after update) {

Map<String,Transaction> transIds = New Map<String,Transaction>();
list<Transaction> transrec = new List<Transaction>();
for(Transaction trans : Trigger.new){
 transIds.put(trans.ItemNumber__c,trans);
}
if(transIds!=null){  
List<Product> product = new List<Product>();
for(Product pro : [SELECT id, StandardPrice FROM Product WHERE ProductCode in:transIds.keySet()]) {
             transIds.get(pro.ProductCode).BasePrice__c = pro.StandardPrice;
             transrec.add(transIds.get(pro.ProductCode));
           }
        if(transrec!=null && transrec.size()>0)
            update transrec;
    }
}