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

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

1 Answer 1

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}"
    }
  );
}
1
  • You are the Guru for me thank you for sharing the knowledge I have updated the parameters but I forgot about case sensitive Commented Apr 20, 2019 at 15:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.