+ Start a Discussion
saraazsaraaz 

can we add elements in map

Hi i have jst added a prog in this i want to add the elements is this possible or not can any one help

 

trigger GettingPrice on Bookings__c (before insert , before update) 
{
   set<string> BookingModelSet = new set<string>();
    for(integer i=0;i<trigger.size;i++)
        {
        BookingModelSet.add(trigger.new[i].Model__c);
        }
   list<Vehicle_Rental_Price__c> PriceOfVehicles = new list<Vehicle_Rental_Price__c>();
   PriceOfVehicles = [select name , Rental_Price__c from Vehicle_Rental_Price__c where name in:BookingModelSet];
    Map<string,decimal> MappingPriceToModel = new Map<string , decimal>(); 
    for(integer i=0;i<PriceOfVehicles.size();i++)
    {   
        MappingPriceToModel.put(PriceOfVehicles[i].name , PriceOfVehicles[i].Rental_Price__c);
    }
    for(integer i=0 ; i<trigger.size ; i++)
    {
    if(MappingPriceToModel.containskey(trigger.new[i].Model__c))
            trigger.new[i].price_per_day__C = MappingPriceToModel.get(trigger.new[i].model__c);
        }
}

 

in this the model__c is a multi picklist field so wen i select a single value from model the price_per_day is automatically generated but in case of selecting multiple values from Model__C the price_per_day is empty  ???

 

can we add such type of values????

harry.freeharry.free
trigger GettingPrice on Bookings__c (before insert, before update) 
{
    Set<string> BookingModelSet = new Set<string>();
    for(Bookings__c booking : Trigger.new)
    {
    	BookingModelSet.addAll(booking.Model__c.split(';'));
    }
    
    Map<string,decimal> MappingPriceToModel = new Map<string , decimal>(); 
    for(Vehicle_Rental_Price__c price : [select name , Rental_Price__c from Vehicle_Rental_Price__c where name in:BookingModelSet])
    {
    	MappingPriceToModel.put(price.Name, price.Rental_Price__c);
    }
    
    for(Bookings__c booking : Trigger.new)
    {
    	Decimal totalPrice = 0;
    	for(String model : booking.Model__c.split(';'))
    	{
    	    totalPrice += MappingPriceToModel.get(model);
    	}
    	booking.Price_Per_Day__c = totalPrice;
    }
}

 Like this?

 

saraazsaraaz

thank you