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
Chris FisherChris Fisher 

Cross Refence Custom object with custom lead field.

Hi All,

I am trying to work out a way to populate a customer lead field by cross reference with a customer object, i will be doing this in APEX code as i believe its not possible as a workflow. 

Situation

We are using apex classes to create lead records using information sent into salesforce from a webserver.

SO, the apex code populates the below custom field
lead.abicode__c = 1234567


We then want lead.abigroup to update. As there is a large number of abicodes we have created a custom object ( abigroups16062014) and this contains around 30000 records showing two bits of information:

EG:
abigroups16062014.code = 1234567
abigroups16062014.group = 27

What i want to happen is when the lead is created, i would like a piece of code that will allow lead.abigroup__c to be populated with the corresponding group number from the custom object. 

so

find abigroup16062014.code where .code = l.abicode__c
then
l.abigroup__c = abigroup16062014.group

Does this make sense?


Best Answer chosen by Chris Fisher
Shyam BhundiaShyam Bhundia
Hi,

Why not create a lookup field to the abigroups object on the lead object? This can be populated at the same time as the lead is created. You can do a soql query to find the relevant abigroup.

This will then create a relationship between the two records and If you still need the code and group on the lead you can create formula fields and grab is from the related abigroup.

By using this approach, if the group or code changed, the values in the lead record will be updated automatically


Something like:
  
   
//the grpCode in the query below is the one from your webserver
   abigroup__c matchingGroup = [SELECT id FROM abigroup__c WHERE code__c = : grpCode  LIMIT 1];
     
   //check if one got returned
   if(null != matchingGroup){
    //assign found group to the lead - abiGroup__c is the new lookup field on lead.
    theLead.abiGroup__c = matchingGroup
   }
   else{
    //do something else
    //Something to think about is what do you want to happen is the matching abigroup is not found?
   }

All Answers

Shyam BhundiaShyam Bhundia
Hi,

Why not create a lookup field to the abigroups object on the lead object? This can be populated at the same time as the lead is created. You can do a soql query to find the relevant abigroup.

This will then create a relationship between the two records and If you still need the code and group on the lead you can create formula fields and grab is from the related abigroup.

By using this approach, if the group or code changed, the values in the lead record will be updated automatically


Something like:
  
   
//the grpCode in the query below is the one from your webserver
   abigroup__c matchingGroup = [SELECT id FROM abigroup__c WHERE code__c = : grpCode  LIMIT 1];
     
   //check if one got returned
   if(null != matchingGroup){
    //assign found group to the lead - abiGroup__c is the new lookup field on lead.
    theLead.abiGroup__c = matchingGroup
   }
   else{
    //do something else
    //Something to think about is what do you want to happen is the matching abigroup is not found?
   }

This was selected as the best answer
Chris FisherChris Fisher
Thank you so much. I will give that a go tomorrow - i just put that in the APEX code i take it?
Shyam BhundiaShyam Bhundia
you will need to modify it slightly with the correct field names....other then that it should work.

Let me know how you get on.
Chris FisherChris Fisher
Right so, I got:



l.Abi_Group__c matchingGroup = [SELECT id FROM ABIGroups16072014  WHERE abicode__c = : vehicle.getChildElement('ABICode', null).getText()  LIMIT 1];
   
    if(null != matchingGroup){
   
     the l.Abi_Group__C = matchingGroup
      }
    else{
// Else do nothing  

      }



Where l.Abi_group__C = lookup relation with the custom object. 


Does that look correct?
Shyam BhundiaShyam Bhundia
looks good...but you dont need  "the" on line 5 and you need a  ";" at the end of line 5...something I missed in my example code. 
Chris FisherChris Fisher
Had a couple of errors come up. 

l.Abi_Group__c matchingGroup = [SELECT id FROM AbiGroups16072014__C WHERE abicode__c = : l.abicode__c  LIMIT 1];
   
    if(null != matchingGroup){
    
     l.abicode__c = matchingGroup;
      }
    else{
   
      }

"Invalid type l.abi_group__c" would i be right in thinking thats decause its a text box and the abicode__c is a number box?


Shyam BhundiaShyam Bhundia
very close....
AbiGroups16072014__C matchingGroup = [SELECT id FROM AbiGroups16072014__C WHERE abicode__c = : l.abicode__c  LIMIT 1];
  
    if(null != matchingGroup){
   
     l.abicode__c = matchingGroup;
    }
    else{
  
      }

Note the change on line 1, you were giving the matchingGroup variable an invalid type
Chris FisherChris Fisher
Shyam,

Got there in the end:

AbiGroups16072014__C matchingGroup = [SELECT id, car_group__c FROM AbiGroups16072014__C WHERE abicode__c = : Decimal.valueOf(vehicle.getChildElement('ABICode', null).gettext()) LIMIT 1]; 

if(null != matchingGroup){ 

l.abigroup__c = matchingGroup.car_group__c; 
} 
else{ 

}