0

I have created an onClick javascript button "Complete Job" on my Custom object "Job"

On the click of that button a picklist field "Job Status" should be updated to the value "Complete"

I am able to achieve all the functionalities with the below given code:

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

sforce.connection.sessionId= '{!$Api.Session_ID}';

 function getBaseURL() { 
 var url = location.href; // entire url including querystring - also: 
 window.location.href; 
 var baseURL = url.substring(0, url.indexOf('/', 14)); 


 if (baseURL.indexOf('http://localhost') != -1) 
 { 
 // Base Url for localhost 
 var url = location.href; // window.location.href; 
 var pathname = location.pathname; // window.location.pathname; 
 var index1 = url.indexOf(pathname); 
 var index2 = url.indexOf("/", index1 + 1); 
 var baseLocalUrl = url.substr(0, index2); 

 return baseLocalUrl + "/"; 
 } 
 else { 
 // Root Url for domain name 
 return baseURL + "/"; 
 } 
 } 
if("{!$Profile.Name}"=="System Administrator") 
{ 
var JobstatusCheck = sforce.connection.query("SELECT sked__Job_Status__c 
FROM sked__Job__c WHERE Id ='{!sked__Job__c.Id}' AND 
sked__Job_Status__c<>'Complete'"); 
var records = JobstatusCheck.getArray("records"); 
//alert(records); 
if(records.length>0) 
{ 
//alert(records); 
var base = getBaseURL(); 
//alert(base); 
var url = base +encodeURI('/flow/Complete_Job_and_JA?JobId=
{!sked__Job__c.Id}&retURL=/{!sked__Job__c.Id}'); 
window.open(url); 
//window.location.href; 
} 
else 
{ 
alert('Job status is already Complete.Please check with your system 
administrator for more information on this'); 
} 
} 
else 
{ 
alert('You are not authorized to Complete the Job. Please check with your 
 System administrator for more information'); 
}

The functionality is working perfectly but I have to click the Complete Profile button more than once to achieve this .While debugging in the console view I got the error

Refused to set unsafe header "User-Agent"

I didn't find much help regarding this issue .

My requirement is to achieve the task with just one click of the "Complete Job " button .How will I achieve this .Is there something wrong with my code?please assist me.

Here ,

Custom Object is Job(sked__Job__c) and its field (which is to be updated ) is Job Status( sked__Job_Status__c)

1 Answer 1

0

I know this is not a solution for JS button, but i would advise using aura component button instead, it will work for Lightning and should be faster. Although it might not suit you as it will not be available for classic page layouts.

Component:

<aura:component controller="auraController" implements="force:hasRecordId,force:apphostable,forceCommunity:availableForAllPageTypes,force:lightningQuickAction">
<aura:attribute name="recordId" type="Id" access="public"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
</aura:component>

Aura Controller:

({
doInit : function(component, event, helper)
{
    var action = component.get("c.updateRecord");

    var id = component.get("v.recordId");

     action.setParams({ 
        recordId : id
    });

    $A.enqueueAction(action);
},
)}

Apex Controller:

public class auraController {

@AuraEnabled
public static void updateRecord(string recordId)
{
    Object_API_Name__c obj = [select id,Field_Name__c from Object_API_Name__c where id:= recordId];
    obj.Field_Name__c = 'New_status';
    update obj;
}

This is sample code for updating current record in as a js button:

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

var query = "select Id, Languages__c from Contact where Id = '{!Contact.Id}' limit 1"; 

var result = sforce.connection.query(query); 
var records = result.getArray("records"); 

var myObj = records[0];

myObj.Languages__c  = 'English';

var results = sforce.connection.update([myObj]);
2
  • After creating component and controllers, just go to global actions, create lightning component action and put it on page layout of your object.
    – LinQ
    Commented May 25, 2017 at 14:19
  • Hii LinQ ,thank you for sharing this answer.But our requirement is to create the Custom button in the Standard classic detail page of the Custom object "Job".So will it be possible to use Lightniong comonent here ?Further more I think this task can also be achieved using vf page custom button instead of onclick javascript button .But the org already has way too many classes and vf pages .so we were trying to accomplish this task by using onClick javascript button only. Commented May 26, 2017 at 4:50

You must log in to answer this question.

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