function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ranveer singh 8ranveer singh 8 

Return pagereference from a webservice method.

Hi guys i have an webservice method i want thr return type to be page reference as i mentioned in code but i am not able to save it  i am getting error as Error: Invalid return type: System.PageReference at line 2 column 37 .how do i need to achieve this guys any help would be appriciated

global class MyWebService {
    webService static pagereference makeContact(String lastName) {
        Contact c = new Contact(lastName = lastName);
        insert c;
        
        pagereference pga = new pagereference('https://ap1.salesforce.com/?id='+c.id);
        return pga;
    }
}
 
SandhyaSandhya (Salesforce Developers) 
Hi,

Please try the code below.
global class MyWebService {
    webService static string makeContact(String lastName) {
        Contact c = new Contact(lastName = lastName);
        insert c;
return String.valueOf( new PageReference('/'+C.Id).getUrl());        
    }
}
Also, refer below link.

http://salesforcepunk.blogspot.sg/2015/11/How-to-Return-PageReference-from-a-webservice-method.html

​Hope this helps you!

Thanks and Regards
Sandhya