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
Swamy P R NSwamy P R N 

How to compare two same fields in different objects

Hi Buddies,

I need help on small trigger, that was like i have two objects i.e, Inventory and Order.
Both Order,Inventory having Quantity(Integer),Product(Lookup) fields.
Now whenver order is created i need to compare Order Quantity,product  with inventory Quantity,product.
If Order quantity is available i.e, it is smaller than Inventory quantity -->Ok, but
If Order quantity is greater than Inventory quantity, than i need to show a error like " Given quantity is not available for selected Product".

For this how can i compare the fields based on selected product. Plz suggest me. is it by Map?.If yes, plz suggest how to achieve.
Best Answer chosen by Swamy P R N
Mikola SenykMikola Senyk
In code of trigger for the Order__c first of all you have to find related Inventory objects for Trigger.new scope.
Something like this:
Set<Id> prodIds = new Set<Id>();
for (Order__c o: Trigger.new) {
    prodIds.add(o.Product__c);
} 
List<Inventory__c> invList = [SELECT Quantity__c, Product__c FROM Inventory__c WHERE Product__c IN :prodIds];
Build the map for inventories where key is equals to ID of product and value related Inventory__c:
Map<Id,Inventory__c> prodInvMap = new Map<Id,Inventory__c>();
for (Inventory__c inv: invList) {
    prodInvMap.put(inv.Product__c, inv);
}
After that you can compare quantities and add error if needed:
for (Order__c o: Trigger.new) {
    Inventory__c inv = prodInvMap.get(o.Product__c);
    if ( inv == null ) {
        o.addError('There is no related Inventory for selected Product');
    } else if ( o.Quantity__c > inv.Quantity__c ) {
        o.addError('Given quantity is not available for selected Product');
    }
}
However, logic of trigger should be slightly more complex. You have to be able manage case of couple Orders with the same Product__c value.
I mean i.e. order1.quantity = 5, order2.quantity = 4 and inventory.quantity = 8.

All Answers

Swagato RaySwagato Ray
Hi there,

you can do this

trigger QuantityTrigger on Invoice__c (before insert) {
// your code
		if(Invoice__c.Quantity__c >PDFDomain__c.quantity__c){
			
		}else{
			
		}

}


Swagato RaySwagato Ray
Yes it is better to use map in program.
Mikola SenykMikola Senyk
In code of trigger for the Order__c first of all you have to find related Inventory objects for Trigger.new scope.
Something like this:
Set<Id> prodIds = new Set<Id>();
for (Order__c o: Trigger.new) {
    prodIds.add(o.Product__c);
} 
List<Inventory__c> invList = [SELECT Quantity__c, Product__c FROM Inventory__c WHERE Product__c IN :prodIds];
Build the map for inventories where key is equals to ID of product and value related Inventory__c:
Map<Id,Inventory__c> prodInvMap = new Map<Id,Inventory__c>();
for (Inventory__c inv: invList) {
    prodInvMap.put(inv.Product__c, inv);
}
After that you can compare quantities and add error if needed:
for (Order__c o: Trigger.new) {
    Inventory__c inv = prodInvMap.get(o.Product__c);
    if ( inv == null ) {
        o.addError('There is no related Inventory for selected Product');
    } else if ( o.Quantity__c > inv.Quantity__c ) {
        o.addError('Given quantity is not available for selected Product');
    }
}
However, logic of trigger should be slightly more complex. You have to be able manage case of couple Orders with the same Product__c value.
I mean i.e. order1.quantity = 5, order2.quantity = 4 and inventory.quantity = 8.
This was selected as the best answer
Swamy P R NSwamy P R N
Thank you Mr. Mikola sanyak,really its helped me alot.