• Kim Adil
  • NEWBIE
  • 20 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 3
    Replies
I have a button that route users to seperate URL links based on a checkbox selection. I would like to remove the hard coded URL and use relative instead. Thanks
 
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/25.0/apex.js")}


if ({!CustomObject__c.checkbox__c}== false)
{
var URL1 = "https://c.cs11.visual.force.com/apex/ObjectCustom?id=" + '{!CustomObject_A__c.Id}';
window.open(URL1);

}
else
{
var URL2 = "https://c.cs11.visual.force.com/apex/ObjectCustom?id=" + '{!CustomObject_B__c.Id}';
window.open(URL2);
}

 
I have a requirement to remove the validation rule and include the logic in the Javascript button. Here is the details

I have a field called 'account__c'   (lookup field to account)
I have 3 multi-picklist fields, called:
multi-picklist A: values are: A1-A2-A3-A4
multi-picklist B: values are: B1-B-B3-B4
multi-picklist C: values are: C1-C2-C3-C4

if account selected is A, then ONLY 'multi-picklist A' field can be populated, the other two should NOT be populated. Same applies if account name is B or C.

I was actually using validation rule, and now I am asked to use Javascript button. We already have one in place that checks certain fields are not blank; it displays an alert if one field is blank.
I am asked to use the same logic I have in the validation rule, and add it to the button. I explained what needs to happen in my question above. Here is the code sample I have now:
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/25.0/apex.js")}

var invoiceID = '{!invoice__c.Id}'; //Get the inovice id

var agree = confirm("Are you sure you want to submit for approval?");

if(agree) 
{

if  ({!ISNULL(invoice__c.Field1__c)} ||
     {!ISNULL(invoice__c.Field2__c)} ||
     {!ISNULL(invoice__c.Field3__c)} ||
     {!ISNULL(TEXT(invoice__c.Field4__c))}) 

  {
     alert('Check Required Fields are populated ' );
  }
else
  {
    var result = sforce.apex.execute("ApexClassName","MethodName",{ InvoiceID : invoiceID });
    document.location.reload(true);
  }
 }

I appreciate the help out there. Thank you
I am new to Javascript language. The code in Bold below is triggered by on-click javascript button. It's working fine; however, I was asked to tweak it to only update the records where the lookup field " agreement " is equal to 'Agr3'. We have 3 agreements (Agr1 - Agr2 -  Agr3). Right now, the Javascript button is updating all the records in the "Request__c" object, but I want to be able to have the code filter and ONLY update the records where Request__c.agreement__r.name = 'Agr3'

{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} 
var url = parent.location.href; 
var records = {!GETRECORDIDS($ObjectType.Request__c)};
var updateRecords = []; 
if (records[0] == null) { 
alert("Please select at least one record to update."); 
} else { 
for (var a=0; a<records.length; a++) { 
var update_Request__c = new sforce.SObject("Request__c"); 
update_Request__c.Id = records[a]; 
update_Request__c.Due_Date__c = new Date(); 
updateRecords.push(update_Request__c ); 

result = sforce.connection.update(updateRecords); 
parent.location.href = url; 
}


I tried using SOQL query to get only the ids for the records that meets the criteria. 

{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} 
var url = parent.location.href; 
var records = {!GETRECORDIDS($ObjectType.Request__c)}; 
var Agr3Records= sforce.connection.query("SELECT id, Agreement__c  FROM records WHERE Agreement__r.Name = 'Agr3' ");
records = Agr3Records.getArray("records");
for(var i=0; i<records.length; i++)
    {
    var update_Request__c = new sforce.SObject("Request__c"); 
    update_Request__c.Id = records[i]; 
    update_Request__c.Due_Date__c = new Date(); 
    updateRecords.push(update_Request__c ); 
    }
result = sforce.connection.update(updateRecords); 
parent.location.href = url; 
}
Can you please help out. I can't see to figure out the get the code to work without getting errors. Thanks
Here is the test class
global class OppPropExtension {
    WebService static string CreateProposal(string OppId) {
    
    string retVal = '';
    

    
    Opportunity Opp = new Opportunity();
    Opp = [select Proposal_Revision_Reason__c, Approved_by_Practice_Area__c, Manager_Approved__c, B_P_Resources_Required__c,
                       Opportunity_ID__c,  
                       Bid_Due__c,
                       Deal_Type__c,
                       Id,
                       LOE_Required__c,     
                       BOM_Required__c,  
                       Subcontractor__c,
                       Contract_Vehicle__c,   
                       OwnerID,
                       Practice_Area_Reported__c 
                       from Opportunity where id =:OppId] ;

    List <ProposalRequest__c> PropToInsert = new List <ProposalRequest__c> ();
    
    //for (Opportunity o : Opps) {
        
        if(
            //(Opp.Proposal_Revision_Reason__c.length() > 0 ) &&
            !String.isEmpty(Opp.Proposal_Revision_Reason__c) &&
            (Opp.Approved_by_Practice_Area__c == true)     &&
            (Opp.Manager_Approved__c == true )             
            )
            {
         
                    // check if opportunity that is being inserted meets the criteria
                    //if(Opp.B_P_Resources_Required__c == true) {
                        
                        ProposalRequest__c p = new ProposalRequest__c (); //instantiate the object to put values for future record
                        // now map opportunity fields to new proposal request
                        p.Name = Opp.Opportunity_ID__c + ':' + Opp.Deal_Type__c; 
                        p.Bid_Due__c = Opp.Bid_Due__c;
                        p.Deal_Type__c = Opp.Deal_Type__c;
                        p.Opportunity__c = Opp.Id;
                        p.LOE_Required__c = Opp.LOE_Required__c;     
                        p.BOM_Required__c = Opp.BOM_Required__c;  
                        p.Subcontractor__c = Opp.Subcontractor__c;
                        p.Contract_Vehicle__c = Opp.Contract_Vehicle__c;   
                        p.OwnerID = Opp.OwnerID;
                        p.Proposal_Revision_Reason__c = Opp.Proposal_Revision_Reason__c;
                        p.Technology_PA__c = Opp.Practice_Area_Reported__c;                      
                         //add this new object to the list that will be inserted.       
                        PropToInsert.add(p);       
                   // }
                   
                    // dml operations might cause an error, so you need to catch it with try/catch block.
                    try {
                        insert PropToInsert; 
                    } catch (system.Dmlexception e) {
                        system.debug (e);
                        return retVal;
                    }
                    
                     retVal = 'Proposal Request created successfully'; 
                     return retVal;
            
            }
            else
            {
                 retVal = 'Opportunity must be approved and revision reason selected before requesting a proposal revision'; 
                 return retVal;
            
            }      
   }  
 }

And here is the trigger 

trigger OppCreateProposalReq on Opportunity (after update) { 
    List <ProposalRequest__c> configToInsert = new List <ProposalRequest__c> ();
    
    for (Opportunity o : Trigger.new) {
        
        if(
            (o.Approved_by_Practice_Area__c == true && trigger.oldMap.get(o.id).Approved_by_Practice_Area__c == false) 
            &&
            (o.Manager_Approved__c == true && trigger.oldMap.get(o.id).Manager_Approved__c == false)
            )
            {
         
                    // check if opportunity that is being inserted meets the criteria
                    if(o.B_P_Resources_Required__c == true) {
                        
                        ProposalRequest__c p = new ProposalRequest__c (); //instantiate the object to put values for future record
                        // now map opportunity fields to new proposal request
                        p.Name = o.Opportunity_ID__c + ':' + o.Deal_Type__c; 
                        p.Bid_Due__c = o.Bid_Due__c;
                        p.Deal_Type__c = o.Deal_Type__c;
                        p.Opportunity__c = o.Id;
                        p.LOE_Required__c = o.LOE_Required__c;     
                        p.BOM_Required__c = o.BOM_Required__c;  
                        p.Subcontractor__c = o.Subcontractor__c;
                        p.Contract_Vehicle__c = o.Contract_Vehicle__c;   
                        p.OwnerID = o.OwnerID;
                        p.Proposal_Revision_Reason__c = o.Proposal_Revision_Reason__c;
                        p.Technology_PA__c = o.Practice_Area_Reported__c;                      
                         //add this new object to the list that will be inserted.       
                        configToInsert.add(p);       
                    }
            
            }
    
    //once loop is done, you need to insert new records in SF
    // dml operations might cause an error, so you need to catch it with try/catch block.
            try {
                insert configToInsert; 
            } catch (system.Dmlexception e) {
                system.debug (e);
            }
    }
}

Any help will be appreciated. Thanks
I am new to Javascript language. The code in Bold below is triggered by on-click javascript button. It's working fine; however, I was asked to tweak it to only update the records where the lookup field " agreement " is equal to 'Agr3'. We have 3 agreements (Agr1 - Agr2 -  Agr3). Right now, the Javascript button is updating all the records in the "Request__c" object, but I want to be able to have the code filter and ONLY update the records where Request__c.agreement__r.name = 'Agr3'

{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} 
var url = parent.location.href; 
var records = {!GETRECORDIDS($ObjectType.Request__c)};
var updateRecords = []; 
if (records[0] == null) { 
alert("Please select at least one record to update."); 
} else { 
for (var a=0; a<records.length; a++) { 
var update_Request__c = new sforce.SObject("Request__c"); 
update_Request__c.Id = records[a]; 
update_Request__c.Due_Date__c = new Date(); 
updateRecords.push(update_Request__c ); 

result = sforce.connection.update(updateRecords); 
parent.location.href = url; 
}


I tried using SOQL query to get only the ids for the records that meets the criteria. 

{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} 
var url = parent.location.href; 
var records = {!GETRECORDIDS($ObjectType.Request__c)}; 
var Agr3Records= sforce.connection.query("SELECT id, Agreement__c  FROM records WHERE Agreement__r.Name = 'Agr3' ");
records = Agr3Records.getArray("records");
for(var i=0; i<records.length; i++)
    {
    var update_Request__c = new sforce.SObject("Request__c"); 
    update_Request__c.Id = records[i]; 
    update_Request__c.Due_Date__c = new Date(); 
    updateRecords.push(update_Request__c ); 
    }
result = sforce.connection.update(updateRecords); 
parent.location.href = url; 
}
Can you please help out. I can't see to figure out the get the code to work without getting errors. Thanks