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
Samadhan Sakhale 3Samadhan Sakhale 3 

Fetching Data from other object

Hi All,
      I have one object which contains two field,i.e. Contry_Code__c & VAT_Rate__c which contains data like
Country Code           VAT Rate
USD                             0.20
IND                              0.19
EURO                           0.15

and i make this two fields on standard object.so how can fetch the VAT Rate  while i select Contry code(It may be Runtime or While Saving Data)
So please help me to solve this assignment.
Regards,
Sam.
Best Answer chosen by Samadhan Sakhale 3
Brian FordBrian Ford
Try this. I haven't tested it, but should work
 
trigger VatRate on Account (before insert, before update) {	
    
    //create reference table for lookup value
    Map<String, Decimal> vatMap = new Map<String, Decimal>();
	
    //build reference table for VAT rates
    for (Vat_Rate__c vat : [SELECT Country_Code__c, Rate__c FROM Vat_Rate__c])
        vatMap.put(vat.Country_Code__c, vat.Rate__c);
	
    //update accounts with VAT rate
    for (Account a : trigger.new)
    {
        Decimal vatRate = vatMap.get(a.Country_Code__c);
        a.Vat__c = vatRate;
    }

}

 

All Answers

Brian FordBrian Ford
You need to provide more information.
  • What's the relationship between the 2 objects? If it's a child-parent relationship, you can use a formula field. If it's a parent-child with a master-detail relationship you can use a workflow rule. If the objects aren't related at all, you'll need to use a trigger.
Balaji BondarBalaji Bondar
Hi Samadhan,
  1. You can create a formula field and fetch the VAT Rate based on Country Code Selection instead of having a custom object to store these values.
  2. If you cannt remove the custom object then , write a after insert/update trigger to have updated VAT Rate based on Country code.

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
Samadhan Sakhale 3Samadhan Sakhale 3
No sir there is not any connection between both object

please tell me how can i write trigger for that
thanks,
Sam
Brian FordBrian Ford
Try this. I haven't tested it, but should work
 
trigger VatRate on Account (before insert, before update) {	
    
    //create reference table for lookup value
    Map<String, Decimal> vatMap = new Map<String, Decimal>();
	
    //build reference table for VAT rates
    for (Vat_Rate__c vat : [SELECT Country_Code__c, Rate__c FROM Vat_Rate__c])
        vatMap.put(vat.Country_Code__c, vat.Rate__c);
	
    //update accounts with VAT rate
    for (Account a : trigger.new)
    {
        Decimal vatRate = vatMap.get(a.Country_Code__c);
        a.Vat__c = vatRate;
    }

}

 
This was selected as the best answer
Samadhan Sakhale 3Samadhan Sakhale 3
Hi Brian Sir,
Thanks you very much sir for helping me to solve this problem
really u r very genious sir Thanks again.
Regards,
Sam