• Patrick Yang@SG
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Dears,

I want to realize a function similar with "vlookup" with Apex and I tried coding like following but not correct.

Background:
I have 2 standard object: opportunities and quotes (lookup relationship, opportunities is father). In opportunities I have a picklist field named "City".

And I create a customized filed called "Destination information". For each value in the picklist "city", I have a record in "Destination information", named with the same picklist value.

Now I created a lookup function between Quotes and  "Destination information". I want to get the ID of that City in "Destination information" to Quotes.

For example: In opportunity there is 1 record with the "City" field value "Paris", and the record named "Paris" in "Destination information" has a ID 0062800000P6Dg6. I want to get that ID and give it to the lookup field "Destination_information" in the Quote records related to that opportunity.

Here following my code (not correct)
trigger GetDestinationID on Quotes (before insert, before update) {
for (Quotes quotes : Trigger.new) {
    String desid = Quotes.Destination_information__c;
    List<Destination_information__c> Destination_information = [SELECT Id FROM Destination_information WHERE Quotes__r.Opportunity.City__c = :desid];
    if(Destination_information != null)
             Quotes.Destination_information__c = Destination_information[0].Id;
    }
}
Thanks in advance!
 
Dears,

I want to realize a function similar with "vlookup" with Apex and I tried coding like following but not correct.

Background:
I have 2 standard object: opportunities and quotes (lookup relationship, opportunities is father). In opportunities I have a picklist field named "City".

And I create a customized filed called "Destination information". For each value in the picklist "city", I have a record in "Destination information", named with the same picklist value.

Now I created a lookup function between Quotes and  "Destination information". I want to get the ID of that City in "Destination information" to Quotes.

For example: In opportunity there is 1 record with the "City" field value "Paris", and the record named "Paris" in "Destination information" has a ID 0062800000P6Dg6. I want to get that ID and give it to the lookup field "Destination_information" in the Quote records related to that opportunity.

Here following my code (not correct)
trigger GetDestinationID on Quotes (before insert, before update) {
for (Quotes quotes : Trigger.new) {
    String desid = Quotes.Destination_information__c;
    List<Destination_information__c> Destination_information = [SELECT Id FROM Destination_information WHERE Quotes__r.Opportunity.City__c = :desid];
    if(Destination_information != null)
             Quotes.Destination_information__c = Destination_information[0].Id;
    }
}
Thanks in advance!