Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mshanemc/058ad62ba08220c8fafb to your computer and use it in GitHub Desktop.
Save mshanemc/058ad62ba08220c8fafb to your computer and use it in GitHub Desktop.
Salesforce1 custom action visualforce redirect page
<apex:page showHeader="false" sidebar="false" standardController="Inspection_Step__c">
<!-- Remote Objects definition to set accessible sObjects and fields -->
<apex:remoteObjects >
<apex:remoteObjectModel name="Inspection_Step__c" jsShorthand="step"
fields="Name,Id,Inspection__c,Step_Number__c,Rating__c">
</apex:remoteObjectModel>
</apex:remoteObjects>
<script type="text/javascript">
//create a js variable for the remoteObjects
var step = new SObjectModel.step();
// find steps for the matching inspection, where there is no status
step.retrieve({
where: {
//same parent as the context object
Inspection__c: {eq: '{!Inspection_Step__c.Inspection__c}'},
//hasn't been rated yet
Rating__c: {eq: ''},
},
//in ascending order
orderby: [{Step_Number__c: 'ASC'}],
}, function(err, records, event){
if(err) {
alert(err.message);
}
else {
//no records left, navigate back to inspection
if (records.length===0){
alert('all steps are complete!');
navigate('{!Inspection_Step__c.Inspection__c}');
} else if (records[records.length-1].get('Step_Number__c') < {!Inspection_Step__c.Step_Number__c} ) {
//the higest step is lower number than context step; go back to the lowest step
navigate(records[0].get("Id"));
} else {
//loop through records
for (var i=0; i<records.length; i++) {
if (records[i].get('Step_Number__c') > {!Inspection_Step__c.Step_Number__c} ){
console.log('navigate to next: ' + records[i].get('Step_Number__c'));
navigate(records[i].get("Id"));
break;
}
};
}
}
});
//universal navigation that works in SF1 or in web desktop UI
function navigate (recordId){
if (typeof sforce !== 'undefined'){
sforce.one.navigateToSObject(recordId);
} else {
//.top to deal with the fact that publisher actions in chatter feed are iframe
window.top.location.href= "/" + recordId;
}
}
</script>
</apex:page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment