+ Start a Discussion
apex code shareapex code share 

Please help me writing a trigger for creating unique id's based on Contact Type.

Hi,

 

i have an custom object INDIVIDUAL CUSTOMER:-

In this object i have a picklist field Customer Type: 1> mechanic 2> Reborer 3> Retailer.

Now based on this Customer type there should be a unique field having the unique id say contact id.

 

For Eg:-  If the customer type is MECHANIC then the unique id field should be like {MEC-0000}

                 If the customer type is REBRER then the unique id field should be like {REB-0000}

                  If the customer type is RETAILER then the unique id field should be like {RET-0000}

 

Also, for each next Record created for mechanic it should be MEC-0000 then MEC-0001,MEC-0002 in the uniqueid field(contact id).

Simmilarly, 

                     for each next Record created for Rebrorer it should be REB-0000 then REB-0001,REB-0002 in the uniqueid field(contact id).

                     for each next Record created for RETAILER it should be RET-0000 then RET-0001,RET-0002 in the uniqueid field(contact id).


 

Best Answer chosen by Admin (Salesforce Developers) 
BussBuss

Hi,

 

write before trigger on INDIVIDUAL CUSTOMER Object

 

for(INDIVIDUAL CUSTOMER cust:Trigger.New){
    System.debug(' Chk here to know Customer type'+cust.Customer Type);
    if(cust.Customer Type =='mechanic'){
        Integer Count = [select Count() From INDIVIDUAL CUSTOMER where Customer Type =:'mechanic'];
        // assign Unique value here
        Unique Id ='{MEC-000'+(Count+1)+'0}';
    }else if(cust.Customer Type =='Reborer'){
        Same as above but where Customer type = Reborer'
    }else{
        //For Retailer
        Same as above
    }
}

 

hope this will help you friend.

 

Reds,

Buss

All Answers

BussBuss

Hi,

 

write before trigger on INDIVIDUAL CUSTOMER Object

 

for(INDIVIDUAL CUSTOMER cust:Trigger.New){
    System.debug(' Chk here to know Customer type'+cust.Customer Type);
    if(cust.Customer Type =='mechanic'){
        Integer Count = [select Count() From INDIVIDUAL CUSTOMER where Customer Type =:'mechanic'];
        // assign Unique value here
        Unique Id ='{MEC-000'+(Count+1)+'0}';
    }else if(cust.Customer Type =='Reborer'){
        Same as above but where Customer type = Reborer'
    }else{
        //For Retailer
        Same as above
    }
}

 

hope this will help you friend.

 

Reds,

Buss

This was selected as the best answer
apex code shareapex code share

Thanks friend   :-)

It worked perfectly!!!