You need to sign in to do that
Don't have an account?
Apex Modification Help Needed
All,
We currently have a trigger written that is pretty simple. It is designed to update the Status field of a custom object called Referral. We have a custom button that is designed to convert a Referral record to a Lead record. When this occurs, the Referral ID is copied to the Lead. The trigger is designed to mark the Referral record Status field "Converted" when a Lead is created with that Referral ID on it.
This works well, but it allows for unlimited conversion of Referrals to Leads. I want to add something to the trigger code that will prevent the Lead from being created if the Referral Status is already "Converted". This way, when a user tries to convert a Referral to a Lead, they will get an error or something that prevents them from completing the conversion.
Is something like this possible? I am pasting our current code below:
trigger LeadToReferralStatusUpdate on Lead (after insert, after update)
{
set<Id> rIds = new set<Id>();
for(Lead l:trigger.new)
{
if(l.Referral_ID__c != null)
{
rIds.add(l.Referral_ID__c);
}
}
Referral__c[] updateReferrals = new Referral__c[0];
for(Referral__c r:[select id, status__c from Referral__c where Id in:rIds])
{
r.status__c = 'Converted';
updateReferrals.add(r);
}
update updateReferrals;
}
Can you post the code from the cutom button? you should be able to perform a validation when the button is clicked before it actually creates a lead record.
Hi
Modify your trigger like this
I have not compiled the code so if any small errors please rectify that
Change in earlier trigger
to
The custom button is a URL code and is like such:
/00Q/e?RecordType=01280000000EtDm
&CF00N800000037jwN={!Referral__c.Name}
&name_firstlea2={!Referral__c.Referral_First_Name__c}
&name_lastlea2={!Referral__c.Referral_Last_Name__c}
&00N80000004PeRb={!Referral__c.Secondary_Name__c}
&00N80000004PeUL={!Referral__c.Secondary_Relationship__c}
&00N800000038j2L={!Referral__c.Phone__c}
&00N800000038j7e={!Referral__c.Referral_Email__c}
&00N800000037m8E={!Referral__c.Referral_Address__c}
&00N800000038jb2={!Referral__c.Product__c}
&00N800000038jk8={!Referral__c.Comments__c}
&00N800000038jjt={!Referral__c.Contact_Method__c}
&00N80000004PaFV={!Referral__c.Preferred_Time__c}
&00N800000038jtE={!Referral__c.What_brought_Referral_into_the_bank__c}
&00N80000003h8n5=Prospect
&00N8000000394Z0=Prospect
&00N80000003hMZu={!Referral__c.Branch_Referred_By__c}
&00N80000003hMYw={! Referral__c.Department_Referred_By__c}
&lea8={!Referral__c.Phone__c}
&lea11={!Referral__c.Referral_Email__c}
&00N80000003hJvv={!Referral__c.Company_Name__c}
&00N80000003hMsz={!Referral__c.Referral_Last_Name__c}
&00N80000003hMsu={!Referral__c.Referral_First_Name__c}
&00N80000003hOcB={!Referral__c.Business_Individual__c}
&00N80000003hXsR={!Referral__c.Source_of_Lead__c}
&lea5={!Referral__c.Source_of_Lead__c}
&LeadSource={!Referral__c.Source_of_Lead__c}
&00N80000003hiUY={!Referral__c.Record_Creator__c}
&00N80000003wqmp={!Referral__c.Referral_Type__c}
&00N800000049FYq={!Referral__c.Id}
&retURL=%2F{!Referral__c.Id}
I assumed you were using the Ajax toolkit in a javascript button to create the referral. However, in that case, i would go with Shashikant's suggestion.
I tried your method, but I get the error whether the Referral Status is Converted or New, so it appears to generate the error regardless of the Referral Status. I appreciate your assistance!
Would this be cleaner? I do not know how to do that but I could research it as I have done the other things.
It seems like it would be more straight forward than the really long URL that you are currently using.
I imagine the button would look something like:
vhanson -
I was able to play with what you gave me and got it working perfectly using the new custom button. The only tweak that I would need to make to this is for it to display the Lead record when the conversion happens. Currently, it sits on the Referral record (which did not refresh) so it appears to the user that nothing happened, when in fact the Lead was created.
I am posting my final code below. Can you please tell me what I need to do to get the Lead record to display? Thanks!
{!REQUIRESCRIPT("/soap/ajax/21.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/21.0/apex.js")}
if ("{!Referral__c.Status__c}" == "Converted")
{
alert("This Referral has already been converted.");
} else
{
var Lead = new sforce.SObject("Lead");
Lead.Referrer_Branch__c = "{!Referral__c.Branch_Referred_By__c}";
Lead.Business_or_Individual__c = "{!Referral__c.Business_Individual__c}";
Lead.Client_Type__c = "Prospect";
Lead.Company = "{!Referral__c.Company_Name__c}";
Lead.Referrer_Department__c = "{!Referral__c.Department_Referred_By__c}";
Lead.Email = "{!Referral__c.Referral_Email__c}";
Lead.FirstName = "{!Referral__c.Referral_First_Name__c}";
Lead.LastName = "{!Referral__c.Referral_Last_Name__c}";
Lead.LeadSource = "{!Referral__c.Source_of_Lead__c}";
Lead.Status = "Open - Not Contacted";
Lead.Phone = "{!Referral__c.Phone__c}";
Lead.RecordTypeId = "01280000000EtDm";
Lead.Referral_Address__c = "{!Referral__c.Referral_Address__c}";
Lead.Referral_Comments__c = "{!Referral__c.Comments__c}";
Lead.Referral_Company__c = "{!Referral__c.Company_Name__c}";
Lead.Referral_Contact_Method__c = "{!Referral__c.Contact_Method__c}";
Lead.Referral_Creator__c = "{!Referral__c.Record_Creator__c}";
Lead.Referral_Email__c = "{!Referral__c.Referral_Email__c}";
Lead.Referral_First_Name__c = "{!Referral__c.Referral_First_Name__c}";
Lead.Referral_ID__c = "{!Referral__c.Id}";
Lead.Referral_Last_Name__c = "{!Referral__c.Referral_Last_Name__c}";
Lead.Referral_Phone__c = "{!Referral__c.Phone__c}";
Lead.Referral_Preferred_Time__c = "{!Referral__c.Preferred_Time__c}";
Lead.Referral_Product__c = "{!Referral__c.Product__c}";
Lead.Referral_Record_Type__c = "{!Referral__c.RecordType}";
Lead.Secondary_Name__c = "{!Referral__c.Secondary_Name__c}";
Lead.Secondary_Relationship__c = "{!Referral__c.Secondary_Relationship__c}";
Lead.What_brought_Referral_into_the_bank__c = "{!Referral__c.What_brought_Referral_into_the_bank__c}";
var refResult = sforce.connection.create([Lead]);
if (refResult[0].getBoolean("success")) {
// now update the referral to reflect that it was successfully updated
var currentReferral = new sforce.SObject("Referral__c");
currentReferral.Id = "{!Referral__c.Id}";
currentReferral.Status__c = "Converted";
var result = sforce.connection.update([currentReferral]);
} else {
alert("Failed to convert Referral. " + refResult[0]);
}
}
Glad to hear it is (mostly) working for you. Check out the code below and let me know if this gets you all the way there...
I get an error when using this new code:
A problem with the OnClick JavaScript for this button or link was encountered:
unterminated string literal
try the code above. I don't think i should have included the '/' before the lead id in the redirectLocation.
Sorry, I still get the same error about an unterminated string. I appreciate your help on this! So close...
It is strange that your validation is fired every time , as we have the condition.
I would like to know two things
1)Can any referral be attached with two leads
2)Are you dealing with bulk lead insert or update
Thanks for the reply Shashikant. To answer your questions:
1) The Lead only has one lookup field for the Referral, so a Lead can only be tied to 1 Referral.
2) I am not doing anything bulk, or not that I am aware of. The trigger is supposed to locate the Referral ID that is on the Lead and update it.
I have also been working with vhanson on trying a Java custom button to handle the work of the trigger and the conversion of the Referral to a Lead all in one step. We are close to getting that to work how I need. I still appreciate your assistance in case I need to go this route, so I hope that info helps. Thanks!
So close indeed!
Have you tried using the Firebug plugin for Firefox to debug the javascript errror? that's the only problem with using javascript -- it's a bit harder to debug than apex code.
I would install the firebug plugin, activate firebug, go to the referral page, and click on the convert button... firebug should show you the exact part of the javascript that created the error...
I just created object referral with picklist field Status having two options "Convrted" , "New".I used the trigger I gave to you
Result :
When I tried to add a
1) Lead having a reffral which has Status "Converted" : Error Message Shown "Already Converted"
2) Lead having a reffral which has Status "New" : Lead record inserted succesfully.
I installed firebug, activated it for the page I am on, enabled the console and when I click the button nothing happens in the console. I am not sure if I am not using it right, but I still get the error on the screen and the console sits empty. Any suggestions? Thanks!
Shashikant,
That is odd. I just repasted the trigger code you provided under Lead triggers. I made sure that your's was the only one active. I went to a Referral, with a status of "New" and used the URL convert button. In doing so, the Lead is created (in edit mode) and when I click on Save, I get the error that it is Already Converted. I changed the Referral status to "Converted" and tried again. Same error. I am not sure why we would have different results...
Have you tried using JSENCODE on all of your text variables as shown below:
JSENCODE Encodes text and merge field values for use in JavaScript by inserting escape characters, such as a backslash (\), before unsafe JavaScript characters, such as the apostrophe (').
vhanson,
I added the JENSCODE as you stated to all of the text fields and it worked! I get the error when I try to convert a Referral that has a status of "Converted" and when I convert a Referral that has a status of "New" it takes me to the Lead that was created, with all field mappings, and marks the Referral status "Converted" for me! I think we have it nailed!
Hi DustinLH
I am a Writing program
Please mark it as Best Answer so that it can help others in the future.Thanks
Suraj Tripathi