0

I'm working on creating a button on the lead object that will insert the fields into a custom object.

I can't get my trigger to work, and I'm not sure what I'm doing wrong. Here is the code for the button:

{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/31.0/apex.js")}
//Calling Class and Method
var result = sforce.apex.execute(
"LeadConversion",
"copyAppDetails",
{
LeadId : "{! Lead.Id }"
}
);
if(result=="Error")
{
window.alert("Copy Failed! Error has occurred");

}

else
{
window.alert(
'The Application details have been successfully copied to the Contact'
);

}

Here is my trigger:

global class LeadConversion{
    webService
    static String copyAppDetails(Id LeadId){
        List<Lead> lstLead =
            [
                SELECT 
                    Id, 
                    Name

                FROM 
                    Lead
                WHERE 
                    Id = :LeadId
            ];


       List <Application__c> lstApp = new List<Application__c>();

        for(Lead app : lstLead){

                    lstApp.add(
                        new Application__c(

                            Id = app.Id,
                            Name = app.Name

                        )
                    );
               }     
      try{   
            INSERT lstApp;
            return lstApp[0].Id; 
        }
        catch (Exception ex){

            return('Error');
        }     
}
}

Here is my error message when I click the button:

{faultcode:'soapenv:Client', faultstring:'System.TypeException: Invalid id value for this SObject type: 00Q7A00000259eJUAQ

Class.LeadConversion.copyAppDetails: line 22, column 1', }

jeremy
  • 29
  • 3

1 Answers1

2

The reason it's throwing that error is because the ID of one object type is not transferable into another type. You can read over this link to understand a bit more why, and what the ID's are composed of: What are Salesforce ID's composed of?

What I would suggest if you want to record the original ID of the Lead is to make a custom field called "Original Lead ID" or something similar, and then insert the ID into there. Salesforce will create an ID for the new custom object record itself.

PocketSpider
  • 358
  • 1
  • 7