+ Start a Discussion
GYAN ANDRUSGYAN ANDRUS 

Hi Pls Anyone help....I want to assign a value for Colors,I have a Custom object called item order,In that object i have field called Color__c,I want to assign the code in another custom field for each color

if Color__c = Pink
Color Code =780,

Like this i have  50 Color,How to acheive this
Best Answer chosen by GYAN ANDRUS
GYAN ANDRUSGYAN ANDRUS
Thanks  david.catindoy,

trigger ColorCodes on PBSI__PBSI_Item__c (before insert, before update) {
    
     Map<String, Item_Master_Color__c> mapCodes = Item_Master_Color__c.getAll();
     
     for(PBSI__PBSI_Item__c item : Trigger.new){
         if(item.Color__c != null && mapCodes.containsKey(item.Color__c)){
             item.Color_Code__c = mapCodes.get(item.Color__c).Color_Code__c;
         }
     }
}

ERROR:loop variable must be sobject..Please help me

All Answers

Mahesh DMahesh D
Hi Gyan,

You can retrieve the values from Custom Object and store them in a Map.
 
Map<String, Integer> colourCodeMap = new Map<String, Integer>();
for(Custom_Object__c co: [Select Id, Name, Colour__c, Colour_Code__c from Custom_Object__c]) {
	colourCodeMap.put(co.Colour__c, co.Colour_Code__c);
}

This you can write it in a Utility Class with a static variable so that you don't need to retrieve every time.

Please do let me know if it helps you.

Regards,
Mahesh
GYAN ANDRUSGYAN ANDRUS
Thanks Mahesh,

But the color code is not entering anywhere,I need to create tthe Code
Mahesh DMahesh D
I Gyan,

If the Colour code is not entered anywhere then how can we get the color codes?

If you can explain more about requirement, it will be easy to provide the solution.

Regards,
Mahesh
GYAN ANDRUSGYAN ANDRUS
I need to enter that in Using MAP key value pair,Using map i need to get the Color code and update in record,But i dont know how to acheive this...
If map key contains value,i need to take that value and using trigger i need to update the code in the filed,pls help
David Catindoy 101David Catindoy 101

Use custom list settings and try try something like this:

trigger ColorCodes on PBSI__PBSI_Item__c (before insert, before update) {
    
     Map<String, Item_Master_Color__c> mapCodes = Item_Master_Color__c.getAll();
     
     for(PBSI__PBSI_Item__c item : Trigger.new){
         if(item.Color__c != null && mapCodes.containsKey(item.Color__c)){
             item.Color_Code__c = mapCodes.get(item.Color__c).Color_Code__c;
         }
     }
}
GYAN ANDRUSGYAN ANDRUS
Thanks  david.catindoy,

trigger ColorCodes on PBSI__PBSI_Item__c (before insert, before update) {
    
     Map<String, Item_Master_Color__c> mapCodes = Item_Master_Color__c.getAll();
     
     for(PBSI__PBSI_Item__c item : Trigger.new){
         if(item.Color__c != null && mapCodes.containsKey(item.Color__c)){
             item.Color_Code__c = mapCodes.get(item.Color__c).Color_Code__c;
         }
     }
}

ERROR:loop variable must be sobject..Please help me
This was selected as the best answer
David Catindoy 101David Catindoy 101
Is PBSI__PBSI_Item__c an SObject? Try changing the API Name since there's a double underscore on it. Notice the highlighted part: PBSI__PBSI_Item__c to PBSI_PBSI_Item__c
GYAN ANDRUSGYAN ANDRUS
Thank you so much David----Its Working now
David Catindoy 101David Catindoy 101
Your welcome.
GYAN ANDRUSGYAN ANDRUS
I just want to know,Is this the Correct way or we can use the MAp key value pair,Because i have 150 color like tiz

Map<Integer, String> m = new Map<Integer, String>{5 => 'Jon', 6 => 'Quinton', 1 => 'Reid'}; ---Like tiz
David Catindoy 101David Catindoy 101
Yes. That's correct.
GYAN ANDRUSGYAN ANDRUS
instead of custom setting,I can use this map right?
 
David Catindoy 101David Catindoy 101
Yeah, you can. But it's not that dynamic and robust compare to using custom settings.
David Catindoy 101David Catindoy 101
When you use map, the tendency would be if your client decided to change the color code for a specific color, he will still need to ask someone (a developer) to change the code so the changes will take effect. Unlike when you use custom settings, every change is configurable. Your user won't bother asking someone to change the code just to change the color code for a single color. Hope this make sense.