+ Start a Discussion
DustinLHDustinLH 

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;

}

vhanson222vhanson222

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.

Shashikant SharmaShashikant Sharma

Hi 

Modify your trigger like this

 

trigger LeadToReferralStatusUpdate on Lead (after insert, after update)
{
  set<Id> rIds = new set<Id>();
  Map<id , Lead> mapID_Lead = new Map<ID,Lead>();
  for(Lead l:trigger.new)
  {
    if(l.Referral_ID__c != null)
    {
      rIds.add(l.Referral_ID__c);
      mapID_Lead.put(l.Referral_ID__c , l);
    }
  }
 
  Referral__c[] updateReferrals = new Referral__c[0];
 
  for(Referral__c r:[select id, status__c from Referral__c where Id in:rIds])
  {
    if(r.status__c == 'Converted')
    {
    mapID_Lead.get(id).addError('Already Converted');
    }
    else
    {
    r.status__c = 'Converted';
    updateReferrals.add(r);
    }
  }
 
  update updateReferrals;

}

 I have not compiled the code so if any small errors please rectify that

 

 

Shashikant SharmaShashikant Sharma

Change in earlier trigger

mapID_Lead.get(id).addError('Already Converted');

 to

 

mapID_Lead.get(r.id).addError('Already Converted');

 

 

 

DustinLHDustinLH

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}

vhanson222vhanson222

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.

DustinLHDustinLH

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!

 

 


Shashikant Sharma wrote:

Change in earlier trigger

mapID_Lead.get(id).addError('Already Converted');

 to

 

mapID_Lead.get(r.id).addError('Already Converted');

 

 

 


 

DustinLHDustinLH

Would this be cleaner? I do not know how to do that but I could research it as I have done the other things.

 

 


vhanson222 wrote:

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.


 

vhanson222vhanson222

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:

 

 

    {!REQUIRESCRIPT("/soap/ajax/21.0/connection.js")}
    {!REQUIRESCRIPT("/soap/ajax/21.0/apex.js")}

    if ("{!Referral__c.Status__c}" == "Converted")
    {
        alert("Referral already converted!");
    } else
    {
        var lead = new sforce.SObject("Lead");
        lead.Name = 'NewLead';
        lead.Referral__c = "{!Referral.Id}";

        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]);
    }
}

 

 

DustinLHDustinLH

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]);
    }
}

vhanson222vhanson222

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...

 

 

{!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]);
            
            // redirect to newly created lead
            var redirectLocation = '/' + refResult[0].id;
            window.location = redirectLocation;
    } else {
        alert("Failed to convert Referral. " + refResult[0]);
    }
}

 

 

DustinLHDustinLH

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

 

 


vhanson222 wrote:

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...

 

 

{!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]);
            
            // redirect to newly created lead
            var redirectLocation = '/' + refResult[0].id;
            window.location = redirectLocation;
    } else {
        alert("Failed to convert Referral. " + refResult[0]);
    }
}

 

 


 

vhanson222vhanson222

 

{!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]);
            
            // redirect to newly created lead
            var redirectLocation = refResult[0].id;
            window.location = redirectLocation;
    } else {
        alert("Failed to convert Referral. " + refResult[0]);
    }
}

 try the code above.  I don't think i should have included the '/' before the lead id in the redirectLocation.

DustinLHDustinLH

Sorry, I still get the same error about an unterminated string. I appreciate your help on this! So close...

 

 


vhanson222 wrote:

 

{!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]);
            
            // redirect to newly created lead
            var redirectLocation = refResult[0].id;
            window.location = redirectLocation;
    } else {
        alert("Failed to convert Referral. " + refResult[0]);
    }
}

 try the code above.  I don't think i should have included the '/' before the lead id in the redirectLocation.


 

Shashikant SharmaShashikant Sharma

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

 

 

DustinLHDustinLH

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!

 

 


Shashikant Sharma wrote:

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

 

 


 

vhanson222vhanson222

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...

Shashikant SharmaShashikant Sharma

I just created object referral with picklist field Status having two options "Convrted" , "New".I used the trigger I gave to you

 

 

trigger LeadToReferralStatusUpdate on Lead (after insert, after update)
{
  set<Id> rIds = new set<Id>();
  Map<id , Lead> mapID_Lead = new Map<ID,Lead>();
  for(Lead l:trigger.new)
  {
    if(l.Referral_ID__c != null)
    {
      rIds.add(l.Referral_ID__c);
      mapID_Lead.put(l.Referral_ID__c , l);
    }
  }
 
  Referral__c[] updateReferrals = new Referral__c[0];
 
  for(Referral__c r:[select id, status__c from Referral__c where Id in:rIds])
  {
    if(r.status__c == 'Converted')
    {
    mapID_Lead.get(r.id).addError('Already Converted');
    }
    else
    {
    r.status__c = 'Converted';
    updateReferrals.add(r);
    }
  }
 
  update updateReferrals;

}

 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.

 

 

DustinLHDustinLH

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!

 

 


vhanson222 wrote:

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...


 

DustinLHDustinLH

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...

 

 


Shashikant Sharma wrote:

I just created object referral with picklist field Status having two options "Convrted" , "New".I used the trigger I gave to you

 

 

trigger LeadToReferralStatusUpdate on Lead (after insert, after update)
{
  set<Id> rIds = new set<Id>();
  Map<id , Lead> mapID_Lead = new Map<ID,Lead>();
  for(Lead l:trigger.new)
  {
    if(l.Referral_ID__c != null)
    {
      rIds.add(l.Referral_ID__c);
      mapID_Lead.put(l.Referral_ID__c , l);
    }
  }
 
  Referral__c[] updateReferrals = new Referral__c[0];
 
  for(Referral__c r:[select id, status__c from Referral__c where Id in:rIds])
  {
    if(r.status__c == 'Converted')
    {
    mapID_Lead.get(r.id).addError('Already Converted');
    }
    else
    {
    r.status__c = 'Converted';
    updateReferrals.add(r);
    }
  }
 
  update updateReferrals;

}

 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.

 

 


 

vhanson222vhanson222

Have you tried using JSENCODE on all of your text variables as shown below:

 

 

Lead.Referrer_Branch__c = "{!JSENCODE(Referral__c.Branch_Referred_By__c)}";

 

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 (').

 

DustinLHDustinLH

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!

 

 


vhanson222 wrote:

Have you tried using JSENCODE on all of your text variables as shown below:

 

 

Lead.Referrer_Branch__c = "{!JSENCODE(Referral__c.Branch_Referred_By__c)}";

 

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 (').

 


 

Suraj Tripathi 47Suraj Tripathi 47

Hi DustinLH

I am a Writing program 

<aura:component
    implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction"
    controller="Add_plaintiff_controller" access='global'>

    <aura:attribute name="status" type="String" />
    <aura:attribute name="plaintiff_Id" type="Id"  description="store plaintiff rec Id"/>
    <aura:attribute name="law_firm_Id" type="Id"  description="store law firm Id rec Id"/>
    <aura:attribute name="attorney_Id" type="Id"  description="store attorney rec Id"/>
     <aura:attribute name="plaintiff_RecId" type="Id"  description="store plaintiff rec Id"/>
    <aura:attribute name="law_firm_RecId" type="Id"  description="store law firm Id rec Id"/>
    <aura:attribute name="attorney_RecId" type="Id"  description="store attorney rec Id"/>
    <aura:attribute name="plaintiffRecords" type="List"  description="store plaintiff records"/>
    <aura:attribute name="lawfirmRecords" type="List"  description="store law firm records"/>
    <aura:attribute name="attorneyRecords" type="List"  description="store attorney records"/>
    <aura:attribute name="description" type="String"  description="store description values"/>
    <aura:attribute name="matterId" type="String" description="Record to which the files should be attached" />
<aura:attribute name="plaintiffRecordType1" type="String" description="to store plaintiffRecordType of other parties's record" />
     <aura:attribute name="AddPlaintiff1" type="boolean" description="add Plaintiff " default="false" />
 <aura:handler name='init' action="{!c.doInit}" value="{!this}" />


   <section role="dialog" tabindex="-1" aria-labelledby="modal-heading-01" aria-modal="true"
        aria-describedby="modal-content-id-1" class="slds-modal slds-backdrop slds-backdrop_open slds-fade-in-open">
        <div class="slds-modal__container">

            <aura:if isTrue="{!v.showSpinner}">
                <div class="exampleHolder slds-show">
                    <lightning:spinner alternativeText="Loading" size="large" class="showSpinnerOnEdit"
                        variant="brand" />
                </div>
            </aura:if> 
            <header class="slds-modal__header">
                <lightning:buttonIcon iconName="utility:close" onclick="{!c.closeModel}" alternativeText="close"
                    variant="bare-inverse" class="slds-modal__close" />
                <h2 class="inlineTitle slds-p-top--large slds-p-horizontal--medium slds-p-bottom--medium slds-text-heading--medium"
                    data-aura-rendered-by="2:3033;a" style="margin-bottom: -18px;">ADD PLAINTIFF</h2>
            </header>

            <div class="slds-grid slds-wrap slds-gutters setBackGroundColorOfPopup">
                <div class="slds-col slds-size--5-of-12 slds-m-left_xx-large slds-m-bottom_small">
                    <lightning:select name="select1" label="status" value="{!v.status}">
                        <option value="">choose one...</option>
                        <option value="Pending Contact">Pending Contact</option>
                        <option value="Statement Requested">Statement Requested</option>
                     <option value="Statement Received">Statement Received</option>
                        <option value="Unresponsive">Unresponsive</option>
                        <option value="Uncooperative">Uncooperative</option>
                        <option value="Pursuing Claim">Pursuing Claim</option>
                        <option value="	Settled Claim">	Settled Claim</option>
                        <option value="Waived Claim">Waived Claim</option>
                        <option value="Not Relevant to Case">Not Relevant to Case</option>
                        <option value="	Deposition Complete">Deposition Complete</option>
                        <option value="	Deposition Pending">Deposition Pending</option>
                        <option value="Trial Witness">Trial Witness</option>
                         </lightning:select>

                </div>
                 <div class="slds-col slds-size--5-of-12 slds-m-left_xx-large slds-m-bottom_small">
                    <lightning:select label="plaintiff" value="{!v.plaintiff_Id}">
                        <option value="" label="" />
                        <aura:iteration items="{!v.plaintiffRecords}" var="cmpVal">
                            <option value="{!cmpVal.Id}" label="{!cmpVal.Name}" />
                        </aura:iteration>
                    </lightning:select>
                    &nbsp;
                    <div class="companyDiv" style="padding-left: 114px;
                    margin-top: -13px;">
                        <lightning:button variant="brand" label="Add" title="Add" onclick="{!c.addplaintiffRec}" />
                        <lightning:button variant="brand" label="Edit" title="Edit" onclick="{!c.editplaintiffRec}"
                            value="{!v.plaintiff_Id}" disabled="{!empty(v.plaintiff_Id)}" />
                    </div>

                </div>

                 <div class="slds-col slds-size--5-of-12 slds-m-left_xx-large slds-m-bottom_small">
                    <lightning:select label="law firm" value="{!v.law_firm_Id}">
                        <option value="" label="" />
                        <aura:iteration items="{!v.lawfirmRecords}" var="cmpVal">
                            <option value="{!cmpVal.Id}" label="{!cmpVal.Name}" />
                        </aura:iteration>
                    </lightning:select>
                    &nbsp;
                    <div class="companyDiv" style="padding-left: 114px;
                    margin-top: -13px;">
                        <lightning:button variant="brand" label="Add" title="Add" onclick="{!c.addlawfirmfRec}" />
                        <lightning:button variant="brand" label="Edit" title="Edit" onclick="{!c.editlawfirmRec}"
                            value="{!v.law_firm_Id}" disabled="{!empty(v.law_firm_Id)}" />
                    </div>

                </div>
                 <div class="slds-col slds-size--5-of-12 slds-m-left_xx-large slds-m-bottom_small">
                    <lightning:select label="attorney" value="{!v.attorney_Id}">
                        <option value="" label="" />
                        <aura:iteration items="{!v.attorneyRecords}" var="cmpVal">
                            <option value="{!cmpVal.Id}" label="{!cmpVal.Name}" />
                        </aura:iteration>
                    </lightning:select>
                    &nbsp;
                    <div class="companyDiv" style="padding-left: 114px;
                    margin-top: -13px;">
                        <lightning:button variant="brand" label="Add" title="Add" onclick="{!c.addattorneyRec}" />
                        <lightning:button variant="brand" label="Edit" title="Edit" onclick="{!c.editattorneyRec}"
                            value="{!v.attorney_Id}" disabled="{!empty(v.attorney_Id)}" />
                    </div>

                </div>
                  <div class="slds-col slds-size--10-of-12 slds-m-left_xx-large slds-m-bottom_small">
                    <lightning:input type="text" label="description" value="{!v.description}" />

                </div>
                    
                 
            </div>



            <!--###### MODAL BOX FOOTER Part Start ######-->
            <footer class="slds-modal__footer">
                <lightning:button variant="destructive" label="Cancel" title="Cancel" onclick="{!c.closeModel}" />
                <lightning:button variant="success" label="Save" title="save" onclick="{!c.SaveAddPlaintiff}"
                     />

            </footer>
        </div>
    </section>
</aura:component>
Please mark it as Best Answer so that it can help others in the future.
Thanks 
Suraj Tripathi