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
selva kumar 14selva kumar 14 

Apex Trigger for duplicate data from text field to another lookup field in the same lead object

Hi Everyone,

I don't have much knowledge about apex coding. I have tried few triggers using some developers sample available in discussion forums. But i can't able to acheive my goal. My requirement is given below.

There are a few fields that cannot be updated through the standard SalesForce interface, and can only be updated via Apex code.  We need to duplicate some information from a text field on the Lead and Opportunity screens, and populate it into a lookup field in the same object. Required details are given below.

Object - Lead/Opportunity
Originating field (from) - Referral Text (text)
Destination field (to)  - Referral Partner (lookup)
When to trigger - Upon update/Insert of Referral Text value


It would be much apprciate,share few sample codings. Please help on this.

Thanks in advance.
Best Answer chosen by selva kumar 14
bob_buzzardbob_buzzard
Yes - so if you have the name of the referrer in that field, your trigger needs to lookup the referrer record matching that field and write the record id into the referral partner field.

Your trigger needs to be something close to the following. Caveat emptor - I haven't compiled this. I've also assumed the field and object API names, so those may need to change:
trigger Lead_ai on AMS_Order_Line_Item__c (before insert) 
{
    // build a set of the referrer names
    Set<String> referrerNames=new Set<String>();

    // iterate the records in the trigger
    for (Lead ld : trigger.new)
    {
        referrerNames.add(ld.Referral_Text__c);
    }

    // query back all matching referrer records, and add these to a map keyed by name

    Map<String, Referral_Partner__c> refPtrsByName=new Map<String, Referral_Partner__c>();
    for (Referral_Partner__c refPtr : [select id, Name from Referral_Partner__c where name in :referrerNames)
    {
        refPtrsByName.put(refPtr.Name, refPtr);
    }

    // now iterate the records and populate the referral partner lookup
    for (Lead ld : trigger.new)
    {
        Referral_Partner__c refPtr=refPtrsByName.get(ld.Referral_Text__c);
        if (null!=refPtr)
        {
            ld.Referral_Partner__c=refPtr.id;
        }
        else
        {
             // take any action required here
        }
    }
}

 

All Answers

bob_buzzardbob_buzzard
What does Referral Text contain?  The Referral Partner field will expect the id of another Salesforce record - is that what is stored in the Referral Text field or is it the name of the partner?
selva kumar 14selva kumar 14
Hi bob_buzzard,

Actually,referral text have data that comes from web form as a lead. Through that need to populate that text data to lookup field(Referral_Partner).

 
bob_buzzardbob_buzzard
You haven't answered my question - I'm not interested in how the referral text is populated, I'm interested in what it actually contains.  There needs to be a way to identify the referral partner from that field.
selva kumar 14selva kumar 14
It has the name of the referer. Whatever the name enters in the referral Text field we need to populate it on referral_partner. It is more over like copy everything enters in text field populate it.

Am i answering your question?
bob_buzzardbob_buzzard
Yes - so if you have the name of the referrer in that field, your trigger needs to lookup the referrer record matching that field and write the record id into the referral partner field.

Your trigger needs to be something close to the following. Caveat emptor - I haven't compiled this. I've also assumed the field and object API names, so those may need to change:
trigger Lead_ai on AMS_Order_Line_Item__c (before insert) 
{
    // build a set of the referrer names
    Set<String> referrerNames=new Set<String>();

    // iterate the records in the trigger
    for (Lead ld : trigger.new)
    {
        referrerNames.add(ld.Referral_Text__c);
    }

    // query back all matching referrer records, and add these to a map keyed by name

    Map<String, Referral_Partner__c> refPtrsByName=new Map<String, Referral_Partner__c>();
    for (Referral_Partner__c refPtr : [select id, Name from Referral_Partner__c where name in :referrerNames)
    {
        refPtrsByName.put(refPtr.Name, refPtr);
    }

    // now iterate the records and populate the referral partner lookup
    for (Lead ld : trigger.new)
    {
        Referral_Partner__c refPtr=refPtrsByName.get(ld.Referral_Text__c);
        if (null!=refPtr)
        {
            ld.Referral_Partner__c=refPtr.id;
        }
        else
        {
             // take any action required here
        }
    }
}

 
This was selected as the best answer
selva kumar 14selva kumar 14
Thanks a lot bob_buzzard. I will be work it out.
selva kumar 14selva kumar 14
Its working fine... thanks  bob_buzzard//