0

I need to redirect to my inserted record when I click a custom button on custom object

Button Code

 {!REQUIRESCRIPT('/soap/ajax/41.0/connection.js')} 
    {!REQUIRESCRIPT('/soap/ajax/41.0/apex.js')} 

    if(confirm("Continue?")){
    var resultMsg = sforce.apex.execute("RM_convertProject","OppId", 
    {oID:"{!Opportunity2__c.Id}"});
    }

    if(resultMsg.proj != NULL){
    window.top.location = "/" & resultMsg.proj;
    }
    else{
    if( resultMsg != null){ 
    alert(resultMsg ); 
    document.location.reload(true)
    }
    }   

Class

WebService static Project__c redirect(List<Project__c> newProject) {
          Project__c proj = [SELECT Id, Name FROM Project__c WHERE Id = : newProject];

          return proj;

        }

I want to redirect to newProject. I think page reference is used when visualforce pages are involved, can I use thiem in this scenario?

1 Answer 1

1

As I tried to convey earlier, you might want a Response object that you'd parse as an object later:

global class Response {
  webservice Boolean success;
  webservice String message;
  webservice Id projectId;
}

...

webservice static Response OppId(Id oID) {
  Response respond = new Response();
  respond.success = false;
  ...
  CreateProject(someRecord, respond);
  ...
  return respond;
}
...
static void CreateProject(Opportunity2__c opptyList, Response respond){ 
  ...
  insert newProject;
  respond.success = true;
  respond.projectId = newProject.Id;
}

In your actual JavaScript, then:

if(confirm(" Continue?")){
  var resultMsg = sforce.apex.execute("RM_convertProject","OppId", 
  {oID:"{!Opportunity2__c.Id}"});
  if(resultMsg.success) {
    window.top.location = "/" & resultMsg.projectId;
  } else {
    alert(resultMsg.message);
  }
}

You may want to experiment with this to get a better idea of what's going on. This isn't the only viable pattern, either, but it should give you a sense of what's possible.

1
  • hi @sfdcfox kindly view the edited above
    – begginers
    Commented Jan 18, 2018 at 6:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .