+ Start a Discussion
symantecAPsymantecAP 

Trigger related Query

I have a Case Object and there are two child objects.

First child object is  Provider

Second child object is Finished goods

and there is no relation between provider and finished goods.

can i write a trigger to update a field in Provider when there is change of field in finished goods.?

Is this achievable if so how?

Best Answer chosen by Admin (Salesforce Developers) 
kbromerkbromer

Quickest answer I could think of, I'm sure there are more efficient/elegant ways.  This will only fire if there is a single Finished Goods in the trigger.  You'd need to decide on the logic of update the other fields if there were bulk inserts of Finished Goods. You'll also want to move the logic out of the trigger if it's going to be used often:

 

 

trigger FinishedGoodsTrigger on Finished_Goods__c (after insert, after update){

if ((trigger.new.size() == 1) && (trigger.new[0].UpdateField__c != trigger.old[0].UpdateField__c)){

Finished_Goods__c fg = trigger.new[0];
list<Provider__c> provsToUpdate = [select id, Update_Field__c, Case_Id__c from Provider where Case_Id__c = :fg.Case_ID__c];

for (Provider__c pv : provToUpdate)
pv.UpdateField__c = fg.UpdateField__c;

update provToUpdate;
}
}

 Consider it a proof of concept.

 

All Answers

kbromerkbromer

Quickest answer I could think of, I'm sure there are more efficient/elegant ways.  This will only fire if there is a single Finished Goods in the trigger.  You'd need to decide on the logic of update the other fields if there were bulk inserts of Finished Goods. You'll also want to move the logic out of the trigger if it's going to be used often:

 

 

trigger FinishedGoodsTrigger on Finished_Goods__c (after insert, after update){

if ((trigger.new.size() == 1) && (trigger.new[0].UpdateField__c != trigger.old[0].UpdateField__c)){

Finished_Goods__c fg = trigger.new[0];
list<Provider__c> provsToUpdate = [select id, Update_Field__c, Case_Id__c from Provider where Case_Id__c = :fg.Case_ID__c];

for (Provider__c pv : provToUpdate)
pv.UpdateField__c = fg.UpdateField__c;

update provToUpdate;
}
}

 Consider it a proof of concept.

 

This was selected as the best answer
SurekaSureka

Hi,

 

Here is the sample trigger for the below functionality:

 

 

trigger UpdateonCaseChild on Child1__c (before insert, before update)
{
for(Child1__c obj_Child1: Trigger.new)
    {
        Child2__c obj_Child2 = [select Name from Child2__c where Case__c=:obj_Child1.Case__c];
        obj_Child2.Name = obj_Child1.Name;
        update obj_Child2;        
    }
}

 where Child1 is the first child object of case and child2 is the second child object of case.

 

The above code replaces the "Name" child2 with Child1 of case object.

 

Hope this helps.

 

Thanks

 

Satya.KonaSatya.Kona
//Dint know many are working on this...I am too slow :-) //here is what I have come up with..



trigger updateProvider on Fingoods__c (after insert, after update) {

for (Fingoods__c fc: Trigger.new) {

List<Provider__c> p = [select id from Provider__C where case__C =: fc.case__c];
Integer j = [select count() from Provider__C where case__C =: fc.case__c];
for(integer i=0;i<j;){
//you can use p.fieldname = fc.fieldname
p[i].name = fc.name;
update p;
i++;
}
}

}
kbromerkbromer

Not to knock the other answers, but both of those will cause errors with insert of multiple Child__1/FinGood__c objects. Having the update inside the loop without any control over the number of inserts will eventually cause a DML error. Something to keep in mind if you ever insert bulk objects.

symantecAPsymantecAP

Thank you so much Kbromer. The code worked just fine 

trigger casechilds on Provider__c (after insert,after update) {

//if ((trigger.new.size() == 1) && (trigger.new[0].Priority__c != trigger.old[0].Priority__c)){

for(Provider__c pro :Trigger.new){
List<Appliance__c> App = [select id, status__c, Case__c from Appliance__c where Case__c = :pro.Case__c];

for (Appliance__c ap11 : app)
if (pro.priority__c=='High'){
ap11.status__c='completed';
}
//ap11.status__c = pro.priority__c;

update app;
}
}