+ Start a Discussion
Garrett MillerGarrett Miller 

Create Account field with same value as Custom Object field based on Opportunity

Hi All,
I am trying to create a new Account field, Top_Pain_Points__c, that takes the same text value as a field, Top_Pain_Points__c on a custom object, Hands_Off_Form__c. The custom object is tied to a certain Opportunity and the value should copy over from the Custom Object to Account when the Opportunity is moved to closed won. 

I have created a workflow rule to perform this task, the rule is Opportunity Closed Won EQUALS True. 

The evaluation criteria is: Evaluate the rule when a record is created, and any time it's edited to subsequently meet criteria

The field update updates Account: Top Pain Points ( Data Type: Text Area )

The formula is: Formula Value (Text) = Top_Pain_Point__c 

I have done a few workflow rules before so I thought that I was doing everything right, but can someone see any flaws in my logic or have a better suggestion of how to go about this?

Thanks!

Garrett

Best Answer chosen by Garrett Miller
Gaurav singh 84Gaurav singh 84
Hello Garrett Miller,
 Please see this following solution

 
trigger updateCustomObjectFieldOnOpportunityUpdate on Opportunity (after update)
{
    string ids;
    for(Opportunity op:Trigger.new)
    {
        ids=op.id;
    }
    opportunity opp=[select StageName,AccountId from opportunity where ID=:ids];
    
    if(opp.StageName=='Closed Won')
    {
       Hands_Off_Form__c obj=[select Account__c,Top_Pain_Points__c from Hands_Off_Form__c where   Opportunity__c=:ids];
        account  ac=[Select Top_Pain_Points__c from account where id=:obj.Account__c Limit 1];  
        ac.Top_Pain_Points__c=obj.Top_Pain_Points__c;
        update ac;  
    }
}
Please let me know if any issue faced .

Mark as Best Answer if your problem solve.
 

All Answers

Bharat.SharmaBharat.Sharma
Hello Miller,
Yes! you can achive this scenerio by the Apex code also.
please confrm me few things 
  • what is your custom object name (is that Hands_Off_Form__c )??
  • describe this : The custom object is tied to a certain Opportunity
Garrett MillerGarrett Miller

Hi @Bharat.Sharma

Yes the custom object name is Hands_Off_Form__c. 
 

To your second question: The custom object, Hands_Off_Form__c, is an object that is within one Opportunity per Account, that is filled out whenever that Opportunity is Closed. Therefore, it's Master Object is both Opportunity and Account. 

 

Gaurav singh 84Gaurav singh 84
Hello Garrett Miller,
 Please see this following solution

 
trigger updateCustomObjectFieldOnOpportunityUpdate on Opportunity (after update)
{
    string ids;
    for(Opportunity op:Trigger.new)
    {
        ids=op.id;
    }
    opportunity opp=[select StageName,AccountId from opportunity where ID=:ids];
    
    if(opp.StageName=='Closed Won')
    {
       Hands_Off_Form__c obj=[select Account__c,Top_Pain_Points__c from Hands_Off_Form__c where   Opportunity__c=:ids];
        account  ac=[Select Top_Pain_Points__c from account where id=:obj.Account__c Limit 1];  
        ac.Top_Pain_Points__c=obj.Top_Pain_Points__c;
        update ac;  
    }
}
Please let me know if any issue faced .

Mark as Best Answer if your problem solve.
 
This was selected as the best answer
Garrett MillerGarrett Miller

Hi thanks for the help, just ended up doing a workflow rule as it was simple enough. Will try your trigger when I have the time. 
 

Best, 

Garrett