+ Start a Discussion
surya7628@gmail.com Ksurya7628@gmail.com K 

Button code is not working

Hello All,
I am trying to attach the csv file to attachments by clicking on the button.In button i have wrote the java script.But it is not working. Actully my requirement is i need to attach the csv file to attachment(standard salesforce attachment) for particular object,that record is i am getting from controller.

See the below code for Button:

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
alert("This is {!Order.Id}");
if("{!Order.Id}"!=null){
alert("The order is {!Order.Id}");
{
    var OrderId="{!Order.Id}";
    var result = sforce.apex.execute("ntcsvController","csvGenerator",{ordid : OrderId});

   
}
}



Controller code:

public class ntcsvController {

    public id orderid{get;set;}
    public list<NTCSV__c> ntcsvrecordslist {get;set;}
    public string csvstring{get;set;}
    
    /*public ntcsvController (id ordid) {
        csvstring='';       
        csvGenerator();
    }*/
    
    public void csvGenerator(id ordid){
        orderid =ordid;
        
        list<NTCSV__c> ntcsvrecordslist =[SELECT ActPayAmt_Rs__c,ActPayDate__c,Actual_Pay_Mode__c,Carried_forward_bal_from_Previous_Montth__c,
        City__c,ConnectionConsumerName__c,ConnectionConsumerUniqueNumber__c,Connection_City__c,Connection_Country__c,Connection_Pincode__c,
        Connection_Since__c,Connection_State__c,Connection_Street__c,Country__c,CreatedById,CreatedDate,Current_Month_OS_Amount_Due_Rs__c,
        DataProvided__c,DueDate__c,FFOrderNumber__c,Id,IsDeleted,LastModifiedById,LastModifiedDate,LastReferencedDate,LastViewedDate,
        Loan_Applicant_City__c,Loan_Applicant_Country__c,Loan_Applicant_Email__c,Loan_Applicant_Mobile__c,Loan_Applicant_Name__c,
        Loan_Applicant_PAN__c,Loan_Applicant_Pin_Code__c,Loan_Applicant_State__c,Loan_Applicant_Street__c,MandatoryPeriod__c,MonthsBack__c,
        Name,Name_Of_Utility_Providing_Connection__c,Order__c,OwnerId,Penalty_Amt_levied_in_Cur_Month_for_dela__c,Period__c,Pincode__c,
        Relationship_with_Borrower__c,Service_Discontinuity_Note_Negative__c,State__c,Street__c,Subsidy_Deduction_Amount_Rs__c,SystemModstamp,
        Total_Amount_Due_Rs__c,Type_Of_Utility__c FROM NTCSV__c where Order__r.id = :ordid];
        
        
        string csvhearder='ActPayAmt_Rs__c,ActPayDate__c,Actual_Pay_Mode__c,Carried_forward_bal_from_Previous_Montth__c,';
        csvhearder=csvhearder+'City__c,ConnectionConsumerName__c,ConnectionConsumerUniqueNumber__c,Connection_City__c,Connection_Country__c,Connection_Pincode__c,';
        csvhearder=csvhearder+'Connection_Since__c,Connection_State__c,Connection_Street__c,Country__c,CreatedById,CreatedDate,Current_Month_OS_Amount_Due_Rs__c,';
        csvhearder=csvhearder+'DataProvided__c,DueDate__c,FFOrderNumber__c,Id,IsDeleted,LastModifiedById,LastModifiedDate,LastReferencedDate,LastViewedDate,';
        csvhearder=csvhearder+'Loan_Applicant_City__c,Loan_Applicant_Country__c,Loan_Applicant_Email__c,Loan_Applicant_Mobile__c,Loan_Applicant_Name__c,';
        csvhearder=csvhearder+'Loan_Applicant_PAN__c,Loan_Applicant_Pin_Code__c,Loan_Applicant_State__c,Loan_Applicant_Street__c,MandatoryPeriod__c,MonthsBack__c,';
        csvhearder=csvhearder+'Name,Name_Of_Utility_Providing_Connection__c,Order__c,OwnerId,Penalty_Amt_levied_in_Cur_Month_for_dela__c,Period__c,Pincode__c,';
        csvhearder=csvhearder+'Relationship_with_Borrower__c,Service_Discontinuity_Note_Negative__c,State__c,Street__c,Subsidy_Deduction_Amount_Rs__c,SystemModstamp,';
        csvhearder=csvhearder+'Total_Amount_Due_Rs__c,Type_Of_Utility__c\n';

        string finalstr = csvhearder ;
        for(NTCSV__c nt:ntcsvrecordslist ){
            
            string recordString='"'+nt.ActPayAmt_Rs__c+'","'+nt.ActPayDate__c+'","'+nt.Actual_Pay_Mode__c+'","'+nt.Carried_forward_bal_from_Previous_Montth__c+'","';
            recordString=recordString+ nt.City__c+'","'+nt.ConnectionConsumerName__c+'","'+nt.ConnectionConsumerUniqueNumber__c+'","'+nt.Connection_City__c+'","'+nt.Connection_Country__c+'","'+nt.Connection_Pincode__c+'","';
            recordString=recordString+ nt.Connection_Since__c+'","'+nt.Connection_State__c+'","'+nt.Connection_Street__c+'","'+nt.Country__c+'","'+nt.CreatedById+'","'+nt.CreatedDate+'","'+nt.Current_Month_OS_Amount_Due_Rs__c+'","';
            recordString=recordString+ nt.DataProvided__c+'","'+nt.DueDate__c+'","'+nt.FFOrderNumber__c+'","'+nt.Id+'","'+nt.IsDeleted+'","'+nt.LastModifiedById+'","'+nt.LastModifiedDate+'","'+nt.LastReferencedDate+'","'+nt.LastViewedDate+'","';
            recordString=recordString+ nt.Loan_Applicant_City__c+'","'+nt.Loan_Applicant_Country__c+'","'+nt.Loan_Applicant_Email__c+'","'+nt.Loan_Applicant_Mobile__c+'","'+nt.Loan_Applicant_Name__c+'","';
            recordString=recordString+ nt.Loan_Applicant_PAN__c+'","'+nt.Loan_Applicant_Pin_Code__c+'","'+nt.Loan_Applicant_State__c+'","'+nt.Loan_Applicant_Street__c+'","'+nt.MandatoryPeriod__c+'","'+nt.MonthsBack__c+'","';
            recordString=recordString+ nt.Name+'","'+nt.Name_Of_Utility_Providing_Connection__c+'","'+nt.Order__c+'","'+nt.OwnerId+'","'+nt.Penalty_Amt_levied_in_Cur_Month_for_dela__c+'","'+nt.Period__c+'","'+nt.Pincode__c+'","';
            recordString=recordString+ nt.Relationship_with_Borrower__c+'","'+nt.Service_Discontinuity_Note_Negative__c+'","'+nt.State__c+'","'+nt.Street__c+'","'+nt.Subsidy_Deduction_Amount_Rs__c+'","'+nt.SystemModstamp+'","';
            recordString=recordString+ nt.Total_Amount_Due_Rs__c+'","'+nt.Type_Of_Utility__c+'"\n';
            finalstr = finalstr + recordString;
        }
        
        Attachment attachment = new Attachment();
        attachment.Body = Blob.valueOf(finalstr);
        attachment.Name = 'csvFile.csv';
        attachment.ParentId =orderid;
        insert attachment;    
    }
}
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Surya, 

Things to remember while calling a class from Custom button - 

1)  Class needs to be a Global class 
2)  and the method you intend to call from the javascript must be a Webservice Method

Please go throug the following links - 
http://blog.shivanathd.com/2014/07/call-apex-class-from-custom-button-salesforce.html
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_and_ajax.htm


Hope, this will help you. 

Thanks,
Sumit Kumar Singh
Dushyant SonwarDushyant Sonwar

Hi Surya,

Checking your OrderId with null in javascript is a wrong way to do this.
 

if("{!Order.Id}"!=null)
i think should check this something like this for an empty string:
if("{!Order.Id}"!='')
for more info you can also check out below url:
http://stackoverflow.com/questions/6003884/how-do-i-check-for-null-values-in-javascript

Hope this helps.
Thanks
 

 

 

surya7628@gmail.com Ksurya7628@gmail.com K
Thank u Sumith 
 now my button is working
Sumit Kumar Singh 9Sumit Kumar Singh 9
Welcome! Glad to know that!

Thanks,
Sumit Kumar Singh