0

I am trying to update all RelatedInventory__c(Child) in the System using the Opportunity(parent) field value from a custom button on the opportunity page,

Suppose I enter an amount 500 and save it and click custom button on that opportunity page, all the RelatedInventory__c records custom filed oppAmount__c in the system will be updated.

here is my code

global class BatchUpdateAllRelatedInv implements Database.Batchable<sObject> {
    public Opportunity opp{ get; set; }
    String query;

    global BatchUpdateAllRelatedInv() {

    }

    global Database.QueryLocator start(Database.BatchableContext BC) {
        this.query = 'Select Id, OppAmount__c from  RelatedInventory__c';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List< RelatedInventory__c> scope) {
        this.opp = [Select Id, Name, Amount, StageName, Probability from Opportunity
                            where Id =: ----"how I will get the Id of that specific Opp"];
        for( RelatedInventory__c rINV :scope){
            rINV.OppAmount__c = opp.Amount;
        }
        update scope;
    }

    global void finish(Database.BatchableContext BC) {

    }

}

From Custom Button this is my code

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")} var
scr="BatchUpdateAllRelatedInv c = new BatchUpdateAllRelatedInv (); " + 
" Database.executeBatch(c, 5); "; var result =
sforce.apex.executeAnonymous(scr); alert("The Batch is Running"); 
  • Do you want oppAmount__c to be populated after the opportunity saved with entering an amount 500 or do you want click of another custom button to populate the amount. if it is first trigger or process builder is good to do if you want the custom button you need to use connect api and do a query and get all the child records a then populate the value and insert it. – Suresh Jul 14 '15 at 18:11
  • I am new to apex, i dont want to use the trigger, but I want to use custom button on opportunity page. When I will click the button the amount of that opportunity will be the Oppamount__c of Related inventory. – Sush Jul 14 '15 at 18:29
  • from Custom Button this is my code {!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")} var scr="BatchUpdateAllRelatedInv c = new BatchUpdateAllRelatedInv (); " + " Database.executeBatch(c, 5); "; var result = sforce.apex.executeAnonymous(scr); alert("The Batch is Running"); – Sush Jul 14 '15 at 18:30
  • How many related records are there, why do you want to use Batch? – Jenny B Jul 14 '15 at 19:01
  • related records only 10 but i want to update all records in the system , approx 100,000 Inventory. – Sush Jul 14 '15 at 19:09
1

You could use visual flow to create a button to update the records. Here is a blog post that walks you through a similar concept with updating Quote Line Items.

  • 1
    And for a shameless plug, here is my blog on something similar from the Opportunity object - wp.me/p6nfKp-2q – Jenny B Jul 14 '15 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.