0

I have written a Javascript page to assign a record type to a record. But the javascript button will not work on salesforce1. I want to convert that to a vf page button. Kindly help me.

{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")} 

var oppObj = new sforce.SObject("Custom_Object__C"); 
oppObj.Id = '{!Custom_Object__C.Id}'; 
oppObj.RecordTypeId = '012F000000XXXXXXX'; 
var result = sforce.connection.update([oppObj]); 

if (result[0].success=='false') { 
alert(result[0].errors.message); 
} else { 
var r = confirm('Please note\n After you click the Submit Survey Button, You cannot edit this record\n Press OK to proceed and cancel to go back to previous page'); 
if(r == true){ 
location.reload(true); 
} 
}
  • what do you mean by VF page button. do you want to use this in VF page. – Tushar Sharma Dec 8 '15 at 3:43
  • @tushar-- yes. Just like how we give on click javascript, i want to use vf page. So i wanna implement the same functionality using vf page. So how can I write – PSH Dec 8 '15 at 3:46
  • In other words-- rewrite this JavaScript button to vf page button. – PSH Dec 8 '15 at 3:47
1
<apex:page>
<apex:form>
 <script src="/soap/ajax/13.0/connection.js"></script >
    <script>
sforce.connection.sessionId = "{!$Api.Session_ID}";
function execute()
{
    var oppObj = new sforce.SObject("Custom_Object__C"); 
    oppObj.Id = '{!Custom_Object__C.Id}'; 
    oppObj.RecordTypeId = '012F000000XXXXXXX'; 
    var result = sforce.connection.update([oppObj]); 

    if (result[0].success=='false') { 
    alert(result[0].errors.message); 
    } else { 
    var r = confirm('Please note\n After you click the Submit Survey Button, You cannot edit this record\n Press OK to proceed and cancel to go back to previous page'); 
    if(r == true){ 
    location.reload(true); 
    } 
    }
}
</script>
<input type="button" value="Click" onclick="execute();"/>
<apex:form>
<apex:page>
  • so the new button content should be selected as "visualforce page" ? – PSH Dec 8 '15 at 4:02
  • A problem with the OnClick JavaScript for this button or link was encountered: Unexpected token < – PSH Dec 8 '15 at 4:09
  • Ratan-- i selected new custom button. If I select the content as vf page, I am unable to get a space to paste the code. Kindly let me know the steps please. I apologize as I am new to salesforce – PSH Dec 8 '15 at 4:39
  • Not sure where to paste the code. If I select vf page content, the space to paste the code just vanishes – PSH Dec 8 '15 at 4:43
  • 1
    Thank you so much @Ratan for patiently helping me out and fixing the issue. Updated code is as below – PSH Dec 8 '15 at 7:14
1

As you want to use this in VF page there are multiple approach which you can use. For example one approach is

Create command button in VF page.

<apex:commandbutton value="Update Value" action="{!updateValue}" rerender="msg" />

Controller

public void updateValue() {
    Opportunity opp = [Select Id, Record from Opportunity WHERE ID =: oppId] //variable store oppId
 //else if you have already opportunity variable then don't query them again use old variable
opp.RecordTypeId = '012F000000XXXXXXX'; 
try {
    update opp;
    //display successfully update page message
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Record update successfully'));
} catch(Exception ex) {
     // display error message
     ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,ex.getmessage()));
}

}

Also as a Good Practices Don't Hardcoded the Id in code use custom setting instead.

  • 1
    Thank you so much. you are right. I shall use the custom setting. I appreciate your help and suggestion – PSH Dec 8 '15 at 3:54

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.