function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Adam C.Adam C. 

updating a custom field based on matching custom fields from two different custom objects

hello,

 

Iam an admin but have been dropped into the "Make it work" category and can't figure out the code that is needed.

The code would need to fire everytime the agreement is created, or updated and whenever a matching record is created

 

There is one agreement per unlimited Records.

 

I have two custom objetcs; 

Agreement__c

Matched_Records__c

 

Criteria

Agreement.combined_Provider_Client__c = Matched_Records.Provider_client_SFID__c

 

Action

Copy the value of Agreement.Status__c over to Matched_Records.Matched_Agreement_Status__c

 

Agreement.Status__C is a picklist if that matters

Rahul_sgRahul_sg
Take a look @this link bro :http://login.salesforce.com/help/doc/en/creating_workflow_rules.htm

you need to create a WF on Agreement__c and use formula to evaluate criteria.
Adam C.Adam C.

How would the workflow work with that?

 

We are creating hundreds of matching records daily and the matching records do not have a relationship with their agreements.  

 

The matching records are created through apex code.

 

Everytime a match record is created we need it to go find the agreement record where the "Agreement.combined_Provider_Client__c = Matched_Records.Provider_client_SFID__c" and then paste the "Status__c" from the agreement object into the Matched Record object field "Matched_Agreement_Status__c"

 

Since there is no relationship set up I wouldn't know how to set up a workflow with update.  Also, wouldn't know how to create a relationship that could be automated since we are creating matching records with batches.

 

 

:) :) :) :) :):) :) :) :) :)

Can you specify the relationship between two objects, please? I can help you from there. 

 

If there is not any relationship between them then you defiantly need trigger workflow won't work across multiple objects if no relationship present.

Adam C.Adam C.

there is no relationship for these two objects.

 

an agreement has two different account records that are combined into one field: 01qZ00000000GCX01qZ00000000GCX (red being one account and black the other).

an agreement also has the status field

 

each account can have multiple agreements so that is why there is a combined field to tell which one we need.

 

a matched record also has a formula that combines the two accounts on it just like the agreement: 01qZ00000000GCX01qZ00000000GCX

 

we need a trigger that pulls all account object records and all matched records then when the two formula fields match, copy the status field in the account object and paste into the matched record field.

 

Do aplologize if this is confusing as im trying to explain it to my best ability.

 

Adam

 

 

:) :) :) :) :):) :) :) :) :)

As per my understanding to your question. I come up with the following Trigger code. It might not perfect but you can play around with it and change it according to your needs.

 

Trigger Agreement__c on Agreement__c(after update, after insert) {

 

 List<Matched_Records__c > matchR = new  List<Matched_Records__c >();

 

 for(Agreement__c  Agg : Trigger.new) {

 

      matchR = [select id, Matched_Agreement_Status__c from Matched_Records__c where Provider_client_SFID__c =: Agg.combined_Provider_Client__c ];

 

       for(Matched_Records__c mRecords : matchR) {

 

           mRecords.Matched_Agreement_Status__c = Agg.Status__c;

       }

     update matchR;

   }

}

Vinit_KumarVinit_Kumar

Adam,

 

You can create a before insert or before update Trigger on Agreement__c and thten update the values.

 

If you could let me know how these objects are related ,I can help you out.

Vinit_KumarVinit_Kumar

Ketan,

 

I am afraid the Trigger is not going to work as there is no relationship between two objects.

Amit TripathiAmit Tripathi

Adam,

If my understanding is correct.this should get you going on.

Collect the Provider id and based on this collection query agreement and status.

using map and provider id as key get the value of status from the map and update the agreement.

 

trigger(before insert)

{

set <id> providerid=new set<id>();

list<,Matched_Records__C> MatchedRecords=new list<Matched_Records__C>();

for(Matched_Records__C matched:trigger.new)

{

providerid.add(matched.Provider_client_SFID__c);

MatchedRecords.put(matched.Provider_client_SFID__c,matched);

}

Map<Agreement> agreementList=new Map<Agreement>(Select combined_Provider_Client__c,Status__c from Agreement__c where combined_Provider_Client__c IN:providerid);

for(Matched_Records__C matched:trigger.new)

{

matched.Matched_Agreement_Status__c=agreementList.get(matched.Provider_client_SFID__c);

MatchedRecords.add(matched);

}

update matchedrecords;

 

}

 

you might not required the update if its just on insert.

 

Let me know if this helps.

Adam C.Adam C.

Thank you so much for this as it does update from the agreements to the matched record when an agreement is updated.

 

How would I then have a matched record grab this automatically upon creation?

 

right now they are created with the Matched_Agreement_Status__C = blank and then not populated unless i go and save the agreement record.

 

anyway of having the matching record get updated when its created?

Adam C.Adam C.

hello,

 

everything worked with this through sandbox but during the validation in production the following error code was received.

 

Failure Message: "System.LimitException: Too many SOQL queries: 101", Failure Stack Trace: "Trigger.MatchedRecordCurrentStatuss: line 8, column 1"

 

any ideas?