+ Start a Discussion
Samantha ChuaSamantha Chua 

Button to map fields stop working if field value = 0

Hi guys,

I have got a button which works perfectly find when generating a contract from Opporutnity.

However, if my opportunity MRR or opportunity Contract month (terms) equats to 0, it does not work. Any idea if there is a workaround?

 

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

var status = "{!Opportunity.StageName}";

var newRecords = [];

var caseRecordType = sforce.connection.query("Select Id from RecordType where SobjectType = 'Contract' and DeveloperName = 'contract'");

var records = caseRecordType.getArray("records"); 

if (status == "Closed Won") 
{ 
    var c = new sforce.SObject("Contract");
    c.RecordTypeId = records[0].Id;
    c.AccountId = "{!Opportunity.AccountId}";
    c.CompanySignedId = "{!Opportunity.Account_ManagerId__c}";
    c.Opportunity__c = "{!Opportunity.Id}";
    c.Contract_Type__c = "{!Opportunity.Contract_Type__c}";
    c.ContractTerm = "{!Opportunity.Contract_Term_months__c}";
    c.Product__c = "{!Opportunity.Product__c}";
    c.MRR__c = "{!VALUE(TEXT(Opportunity.New_MRR__c))}";
    c.CustomerSignedId = "{!Opportunity.Sold_To_ContactId__c}"; 

    newRecords.push(c); 
    sforce.connection.create(newRecords, 
                             { 
                                 onSuccess : function(result){ 
                                     console.log(result[0]); 
                                     console.log('Success '+result.id); 
                                     alert ('Service contract generated. Please refresh page to view latest case in Related Contract list.'); 
                                     
                                 }, 
                                     
                                     onFailure : function(){ 
                                         console.log('Error '+result[0]); 
                                     } 
                                 
                             }); 
    
} 
else 
{ 
    alert ('Opportunity must be closed won before a service contract can be generated.'); 
}

Thanks!

Regards,
Samantha
 

Best Answer chosen by Samantha Chua
ManojSankaranManojSankaran
Hi Samantha,

Contract Term field in mandatory and should be greater than 0 (this is a standard salesforce validation). So you wont be able to disable this validation rule.

Simple Workaround.

1. Remove the contract term field from page layout 
2. Create a formula field to show the contract term value (if not zero)
3. Also add an if Condition
if('{!Opportunity.Contract_Term_months__c}' > 0)
        c.ContractTerm = "{!Opportunity.Contract_Term_months__c}";


Thanks
Manoj S

All Answers

ManojSankaranManojSankaran
Hi Samantha,

Contract Term field in mandatory and should be greater than 0 (this is a standard salesforce validation). So you wont be able to disable this validation rule.

Simple Workaround.

1. Remove the contract term field from page layout 
2. Create a formula field to show the contract term value (if not zero)
3. Also add an if Condition
if('{!Opportunity.Contract_Term_months__c}' > 0)
        c.ContractTerm = "{!Opportunity.Contract_Term_months__c}";


Thanks
Manoj S
This was selected as the best answer
Samantha ChuaSamantha Chua
Hi guys, Thanks for your input. Added these codes in and it worked. Yay
 
if ({!Opportunity.Contract_Term_months__c} > 0) { 
c.ContractTerm = "{!Opportunity.Contract_Term_months__c}"; 
} 

if ({!VALUE(TEXT(Opportunity.New_MRR__c))} > 0.00) { 
c.MRR__c = "{!VALUE(TEXT(Opportunity.New_MRR__c))}"; 
}