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
faceroy123faceroy123 

How to aviod hard coding

Hi,

 

I have this trigger. I want to expand, so that upon insert or update a case will automatically populate a lookup field to a custom object:

trigger volumes on case (before Insert, before Update) {
        for(case x : Trigger.New){        
        if (x.region__c == 'south west' & x.financial_period__c == '1. January 2012'){   
       x.Cons__c = 'a0iM0000001cI6F';                                     
} 
   }

 How can i avoid hard coding the record ids, so that when I deploy to another environment the trigger will work?

 I understand I could use custom settings but I have never used these before.

 

Thanks

 

 

 

 

SRKSRK

Goto SteUp > Develop > Custom Settings >New

Create a custom setting example named as "Factory"  

after creating Custom Settings click on button or link (Manage) 

Click on New button

A new page open with a single field Name enter u r ID hear (i.e.a0iM0000001cI6F)
click on save

then go to trigger
 list<Factory__c> fc;
            map<string,Company_Details__c> fcmap;
            fcmap = Company_Details__c.getall();
            fc = fcmap.values();
            String ID  = fc[0].Name;       // As u have only one record in custom seeting u can hard code [0] hear;
Then use the ID in u trigger

faceroy123faceroy123

Thanks,

 

I want to expand on the trigger to enter multiple criteria's - hence why I do not want to hard code.

 

I'm struggling to understand the trigger. I have a custom setting called 'Volumes__c' and the field that holds the record Id is 'Id__c'

 

The lookup field is to the consignments__c custom object.

 

Do I add your snippet to my trigger?

 

Sorry this is all new to me.

 

Thanks

SRKSRK

list<Volumes__c> fc;
            map<string,Volumes__c> fcmap;
            fcmap = Volumes__c.getall();
            fc = fcmap.values();
            Id recID  = fc[0].Id__c;    

          

trigger.new[0].consignments__c = recID; 

// above line give error do this

i assume that the id in custom seeting is of xxx__c custom object

     xxx__c tempobj = [selectid,name from xxx__c where id =: recID];

           trigger.new[0].consignments__c = tempob.id;

           

          

faceroy123faceroy123

Thanks,

 

That seems to be close to what I want.

However I need to add new records in my custom setting. E.g: financial_period__c = 1. January 2012 & Region = 'South West'.

 

How would I incorporate this into my trigger?

trigger volumes on case (before Insert, before Update) {
  for(case x : Trigger.New){

  list<Volumes__c> fc;
            map<string,Volumes__c> fcmap;
            fcmap = Volumes__c.getall();
            fc = fcmap.values();
            Id recID  = fc[0].Id__c;    
if (x.region_2__c == 'south west' & x.financial_period__c == '1. January 2012'){
          
 
consignments__c tempobj = [select id,name from consignments__c where id =: recID];
trigger.new[0].consignments_volumes__c = tempobj.id;

 

faceroy123faceroy123
trigger volumes on case (before Insert, before Update) {
 for(case x : Trigger.New){


  list<Volumes__c> fc;
            map<string,Volumes__c> fcmap;
            fcmap = Volumes__c.getall();
            fc = fcmap.values();
            Id recID  = fc[0].Id__c;   
             
if (x.region_2__c == 'south west' & x.financial_period__c == '1. January 2012'){
        
     consignments__c tempobj = [select id,name from consignments__c where id =: recID ];
           trigger.new[0].consignments_volumes__c = tempobj.id;


if (x.region_2__c == 'south East' & x.financial_period__c == '1. February 2012'){

consignments__c tempobj = [select id,name from consignments__c where id =: recID ];
           trigger.new[0].consignments_volumes__c = tempobj.id;

 SRK,

 

How would I modify to cater for relating to more than 1 record? (shown above) but obviously this does not work.

 

Thanks

SRKSRK

Points i assume :-
1)   I belive that     financial_period__c   & Region __c is a field in custom seeting (Volume__c)
2)   Also as multiple record can be process in trigger & there is only one record in custom seeting so the financial_period__c & Region__c field is update to "1. January 2012"  & "south west" respactively if any one record match the condition.
 

trigger volumes on case (before Insert, before Update)
 {
  public boolean cusupdateflag;
  list<Volumes__c> fc;
            map<string,Volumes__c> fcmap;
            fcmap = Volumes__c.getall();
            fc = fcmap.values();
            Id recID  = fc[0].Id__c;    
    consignments__c tempobj = [select id,name from consignments__c where id =: recID];        
  for(case x : Trigger.New)
{
if (x.region_2__c == 'south west' & x.financial_period__c == '1. January 2012')
{
    trigger.new[0].consignments_volumes__c = tempobj.id;
    cusupdateflag = true;
}
}

if(cusupdateflag)
{
    fc[0].financial_period__c = '1. January 2012';
    fc[0].Region __c = 'south west';
    update QuoNumberFromCustomsetting[0];
}

}