0

We replaced standard Workflow button(submit for Approval) on Lead Object with custom button that validates the fields with in Lead and gives a pop up Message if the fields are not populated.

Was able to code it, but not working as it supposed to. It is going to submitting process if though the fields are empty!.Not Sure what's wrong.

Below is my script for the custom button.

{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")} 
var poId = '{!Lead.Id}';
    if(poId == ''){
        poId = window.location.pathname.replace("/","");
    }
    var records = sforce.connection.query("SELECT Id,Title,Email,Phone,Company,City,Street,State,PostalCode FROM Lead WHERE Id ='{!Lead.Id}'");

    var programRecs = records.getArray('records');
    //var po = results.records;
    if(programRecs[0] != null || programRecs[1] != null ||programRecs[2] != null|| programRecs[3] != null || programRecs[4]!= null || programRecs[5] != null ||programRecs[6] != null || programRecs[7] != null){
        // Part based on standard Submit for approval button
        if ((Modal.confirm && Modal.confirm('Once you submit this record for approval, you might not be able to edit it or recall it from the approval process depending on your settings. Continue?')) || (!Modal.confirm && window.confirm('Once you submit this record for approval, you might not be able to edit it or recall it from the approval process depending on your settings. Continue?'))) 
            navigateToUrl('/p/process/Submit?retURL=%2F' + poId + '&id=' + poId,'DETAIL','submit');
    } else {
        var msg = 'This Lead cannot be sent for approval before following errors are fixed: \r\n\r\n';
        if(po.Title == null)
                msg += '"Title" is blank\r\n';
        if(po.Email == null)
                msg += '"Email" is blank\r\n';
        if(po.Company == null)
                msg += '"Company" is blank\r\n';
        if(po.City != null)
                msg += '"City" is blank\r\n';
        if(po.Street== '')
                msg += '"Street" is blank\r\n';
        if(po.State == '""')
                msg += '"State" is blank\r\n';
        if(po.PostalCode == '""')
                msg += '"Zip Code" is blank\r\n';
            alert(msg);
} 

1 Answer 1

1

Change this

if(programRecs[0] != null || programRecs[1] != null ||programRecs[2] != null|| programRecs[3] != null || programRecs[4]!= null || programRecs[5] != null ||programRecs[6] != null || programRecs[7] != null){

to

if(programRecs[0].Id != null || programRecs[0].Title != null || programRecs[0].Email != null|| programRecs[0].Phone != null || programRecs[0].Company != null || programRecs[0].City != null || programRecs[0].Street != null || programRecs[0].State != null || programRecs[0].PostalCode != null){

Reason: records.getArray('records'); will return a list of records. You were trying to check whether any of the record is null instead of checking fields on one particular record.

Hope this helps.

0

You must log in to answer this question.

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