1

I am working on detail javascript button in the lead object. But now I am facing the below issue.

Javascript code:

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}
var parent = "{!Lead.Parent_LeadId__c}";
If(parent === "")
{
alert("pop up"); 
}
else
{
sforce.apex.execute("SwapRecords","ChangeMaster",{ID:"{!Lead.Id}",ID:"{!Lead.Parent_LeadId__c}"});
}

Apex class:

global class SwapRecords {
    webservice static void ChangeMaster(Id changetomaster,Id CurrentMaster)
    {
        List<Lead> UpdateLeads = new List<Lead>();
        for(Lead ls:[Select id,Parent_Lead__c from lead where Parent_Lead__c=:CurrentMaster OR id=:CurrentMaster ])
        {
            If(ls.Id == changetomaster)
            {
                ls.Parent_Lead__c='';
                UpdateLeads.add(ls);
            }
            else
            {
                ls.Parent_Lead__c=changetomaster;
                UpdateLeads.add(ls);
            }
        }
        If(UpdateLeads !=null && !UpdateLeads.isEmpty() )
        {
            update UpdateLeads;
        }
    }
}

enter image description here

Please help where I did wrong

  • What is the data type of Parent_Lead__c? I suspect it's a String and contains a quotation mark. – David Reed Apr 19 at 17:55
  • Sorry, @DavidReed parent_Laed__c is lookup relation to the lead. I have updated my question now. Please have a look – Pavan tej Apr 19 at 18:46
3

JavaScript is cAsE sEnSiTiVe. In this case, you used If instead of if, and so caused a parsing error later on at the else keyword. You also are not passing the parameters correctly; you need to use the variable name in the Apex method, not its "type".

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}
var parent = "{!Lead.Parent_LeadId__c}";
if(parent === "") {
  alert("pop up"); 
} else {
  sforce.apex.execute(
    "SwapRecords",
    "ChangeMaster",
    { changetomaster:"{!Lead.Id}",
      CurrentMaster:"{!Lead.Parent_LeadId__c}"
    }
  );
}
  • You are the Guru for me thank you for sharing the knowledge I have updated the parameters but I forgot about case sensitive – Pavan tej Apr 20 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.