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
sammy9099sammy9099 

Trigger not working with web to lead

Hey All , 

 

I have Trigger on Lead which is updating look up field . Its working fine when I create a lead through SFDC . But , when I use web to lead form to enter lead , Trigger is not updating look up field. Any suggestions what could be the reason ??

 

Regards

jbroquistjbroquist

Hmm... It should work just fine, can you post your trigger code so I can see how it works?

sammy9099sammy9099

So , I have these 3 fields :- 1.) Primary distrubutor( picklist and Independent field)

                                                  2.) Primary distriibutor systemID (18 digit ID,  Picklist , controlled by Primary Distributor )

                                                  3.) Preferred Distributor .....( look up field on accounts and equals to primary distri. systemID)

 

In the Web to lead form , Users can select the Primary distributor . But when I go back to check the SFDC there are no value update in Primary distriibutor systemID & Preferred Distributor. Below is the code.

 

trigger LeadTrigger on Lead (before insert , before update , after insert , after update) {
if(trigger.isBefore) {
    if(trigger.isInsert || trigger.IsUpdate) {
      for(Lead l : trigger.new) {
        if(l.primary_sysid__c != null) {
          l.preferred_distributor__c = l.primary_sysid__c;
        }
      }
    }
  }
}

 

jbroquistjbroquist

Yeah, you're going to have to set the Primary Distributor System ID either in the Web-To-Lead form, or within a trigger. Dependent picklists won't default a value for you on Web-To-Lead. All that is done within Salesforces interface via javascript.

sammy9099sammy9099

Hey , 

 

Can you throw little light on it because I took the Primary distrubutor systemID field in th web to lead form but when I execute the hTML code it gives ERROR. And , How can it be done interfacing with javascript ???

 

Regards

jbroquistjbroquist

What error does it give you?

sammy9099sammy9099

I am posting the code to w3schools.com ( just to check) . It only says ERROR , THATS IT ...

jbroquistjbroquist

In the web form are you setting a value for the Preferred Distributor System Id?

sammy9099sammy9099

yES ..Below is the HTML Code

 

<!-- ---------------------------------------------------------------------- -->
<!-- NOTE: Please add the following <META> element to your page <HEAD>. -->
<!-- If necessary, please modify the charset parameter to specify the -->
<!-- character set of your HTML page. -->
<!-- ---------------------------------------------------------------------- -->

<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8">

<!-- ---------------------------------------------------------------------- -->
<!-- NOTE: Please add the following <FORM> element to your page. -->
<!-- ---------------------------------------------------------------------- -->

<form action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST">

<input type=hidden name="oid" value="00Di0000000Ho9r">
<input type=hidden name="retURL" value="http://">

<!-- ---------------------------------------------------------------------- -->
<!-- NOTE: These fields are optional debugging elements. Please uncomment -->
<!-- these lines if you wish to test in debug mode. -->
<!-- <input type="hidden" name="debug" value=1> -->
<!-- <input type="hidden" name="debugEmail" -->
<!-- value="citm.tushar.27@gmail.com"> -->
<!-- ---------------------------------------------------------------------- -->

<label for="last_name">Last Name</label><input id="last_name" maxlength="80" name="last_name" size="20" type="text" /><br>

<label for="company">Company</label><input id="company" maxlength="40" name="company" size="20" type="text" /><br>

primary_sysid:<select id="00Ni0000000Tvq0" name="00Ni0000000Tvq0" title="primary_sysid"><option value="">--None--</option><option value="11111">11111</option>
<option value="22222">22222</option>
<option value="33333">33333</option>
<option value="44444">44444</option>
<option value="55555">55555</option>
<option value="66666">66666</option>
<option value="77777">77777</option>
<option value="88888">88888</option>
<option value="001i0000001wzvZ">001i0000001wzvZ</option>
<option value="00Qi0000001SIUf">00Qi0000001SIUf</option>
<option value="001i0000001wzvy">001i0000001wzvy</option>
<option value="001i0000002sObc">001i0000002sObc</option>
<option value="001i0000001xBy9">001i0000001xBy9</option>
<option value="001i0000001xC6c">001i0000001xC6c</option>
</select><br>

primary distributor:<select id="00Ni0000000Tvq5" name="00Ni0000000Tvq5" title="primary distributor"><option value="">--None--</option><option value="abc">abc</option>
<option value="def">def</option>
<option value="ghi">ghi</option>
<option value="jkl">jkl</option>
<option value="mno">mno</option>
<option value="pqr">pqr</option>
<option value="stu">stu</option>
<option value="vwx">vwx</option>
<option value="mayur">mayur</option>
<option value="Arrow">Arrow</option>
<option value="Burlington Textiles corp of America">Burlington Textiles corp of America</option>
<option value="mayur(NA)">mayur(NA)</option>
<option value="Tushar (NA)">Tushar (NA)</option>
</select><br>

<input type="submit" name="submit">

</form>

jbroquistjbroquist

Just tested your form and submitted a value without getting any error... Where exactly are you receiving this error?

sammy9099sammy9099

Hey , 

 

I sincerely apologise as I never noticed your reply . I thought you didnt reply thats why I posted again. forgive me .

 

Yeah , I am not getting any error . The error was due to some invalid fields. But it is not updating lookup field( preferrred distributor) After I click on EDIT on LEAD record then only it updates the primary distributor sysid field and preferred distributor (lookup) field. I want as soon as lead enters  its information and selects the primary distributor in web to lead form the lookup field should automatically be updated with the help of trigger. But this is not happening.

 

Regards

jbroquistjbroquist

Hrmmm... this is odd behavior. Can you do me a favor and try running this as your trigger code and tell me if you get the results you're looking for.

 

trigger LeadTrigger on Lead (after insert , before update) 
{

    Lead[] updatedLeads = new Lead[]{};

    for(Lead l : trigger.new) 
    {
        if(Trigger.isInsert && l.primary_sysid__c != null)
        {
            l.preferred_distributor__c = l.primary_sysid__c;
            updatedLeads.add(l);
        }
        //check if the value has changed
        else if(Trigger.isUpdate && l.primary_sysid__c != null && l.primary_sysid__c != Trigger.oldMap.get(l.Id).primary_sysid__c)
        {
            l.preferred_distributor__c = l.primary_sysid__c;
        }
    }

    if(updatedLeads.size() > 0) update updatedLeads;
}

 

sammy9099sammy9099

Hey , Thanks for the reply .

 

I tried this code. 

1.) when I use web to lead , no updates are taking place i.e primary distributar sysid__c and preferred distributar fields are not getting updated.

2.)second case , When I use SFDC to insert new lead it is giving me this error

 

Apex trigger LeadTrigger caused an unexpected exception, contact your administrator: LeadTrigger: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.LeadTrigger: line 10, column 1

 

3.) And , When I am using SFDC to update exsisting record it is working fine.

 

Regards

sammy9099sammy9099

Hey Jonathan , 

 

Do you have any idea how to fix the above errors in the code that you gave me ??

 

Regards