+ Start a Discussion
sdfasdsdfasd 

how to conversion lead to account,contact and custom objects.

i got new requirement from my client.  i created two custom objects i.e  Agency and Broker objects.

                  i.  Agency object is Master to Broker object.

                 ii. created Account lookup field in Agency object..

Task is  once Lead is  Converted then  company name goes to Agency object and FirstName and LastName goes to Broker object. this is the Task . I Created Trigger for this task. Comapany name goes to Agency object working properly but Broker object have Agency Requiredfield how can i get the Agency id  in Broker object.

 

My Trigger :

==========

trigger ConvertLead on Lead (after insert, after update) {
 
 if(Trigger.new.size() >0) {
    
    if(Trigger.new[0].isConverted == true && Trigger.new[0].Lead_type_picklist__c =='Broker') {
       
       List<Agency__c> agency = new List<Agency__c>();
       List<Account> acc = new List<Account>();
       List<Lead> lead = new List<Lead>();
       List<ID>   LIDs = new List<ID>();
       
       for(Lead l:Trigger.New){
           lead.add(l);
           LIDs.add(l.id);
       }
     
       if(Trigger.new[0].ConvertedAccountId != null){
          
          for(Account a :[Select a.Id,a.Name,a.Description,a.BillingStreet,a.BillingState, a.BillingPostalCode, a.BillingCountry, a.BillingCity  From Account a Where a.Id =:Trigger.new[0].ConvertedAccountId]){
              a.Description= Trigger.new[0].Name;
              acc.add(a);
              Agency__c ag = new Agency__c();
              ag.Name = a.Name;
              ag.Mailing_Address__c = a.BillingStreet;
              ag.City__c =a.BillingCity;
              ag.State__c = a.BillingState;
              ag.County__c = a.County__c;
              ag.Zip_Code__c = a.BillingPostalCode;
              ag.Account__c = Trigger.new[0].ConvertedAccountId;
              ag.Phone__c = a.Phone;
              agency .add(ag);
            }
             update acc;
             insert agency;
       }
        
           List<Contact> con = new List<Contact>();
           List<Broker__c>  brokers = new List<Broker__c>();
       
       if(Trigger.new[0].ConvertedContactId != null ) {
           for(Contact c :[Select id,Name,Description,AccountId  from Contact where  Id = :Trigger.new[0].ConvertedContactId]){
               c.Description = Trigger.new[0].Name;
               con.add(c);
             
               Broker__c  b = new Broker__c();
               b.Name = c.Name;
               b.Agency__c =
               b.Contact_ID__c = c.Id;
               brokers.add(b);
           }
              update con;
              insert brokers;
         }
       }
     }
   }

 

now i need Agency id how can solve this problem .Please help ...............      

asish1989asish1989

hi

try by writing

               b.Agency__c = b.Agency.Id;

or

b.Agency__c = b.AgencyId;

sdfasdsdfasd

Thanks for your post asish1989. i solved this problem i can use  b.Agency__c = ag.id;  Now working fine. ''

 

This Trigger only  for single Record. i want Bulk Records how can i do Bulk Process . pls help me....................