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
SAHG-SFDCSAHG-SFDC 

Copy custom Field Values from one custom object to Opportunity

HI I am trying to copy the field values from one custom object to Opportunity standard object

I have 6 fields on Custom Object
Address, City, State, Zip Code, County and Country
I created these fields on opportunities as well
How do I copy the values?  (My requirement is when users create new opportunity the system should give them option if you want to create this duplicate opportunity as the address is same (These 6 fiekds are same) and ask them if they want to create and if yes create a record
Always ThinkinAlways Thinkin
What is the relationship between your Custom Object and Opportunity? And you say it's a duplicate Opportunity, so is it coming from the Clone button or from the New Opportunity button?

Since you are saying that you want to give user the option to create the Opportunity duplicating the address, you're getting into a more complex process than automatically copying the field values. A Flow is the standard process to prompt Users to make a decision. If that is acceptable, you would be able to start the Flow from the existing Opportunity and present the Users with a page that displays the original Opportunity's fields' values so that they can make that choice.
SAHG-SFDCSAHG-SFDC
HI Always Thinkin, 

Thanks for replying

Its a look up relationship, I cannot use workflows here

Duplicate opportunities are cretaed by users, If they save the new opportunity it should check for the address and if that matches it should ask them "Do you want to create new one?" or skip the creation
Always ThinkinAlways Thinkin
Which is the Parent and which is the Child? And is your process always starting from an existing Opportunity or perhaps the Account it's associated with? In other words, what will allow you to relate the Custom Object record at the time of record creation to the New Opportunity? That relationship has to come from somewhere if you want the existing Address fields to be displayed to the user.
SAHG-SFDCSAHG-SFDC
User-added image

This is the relationship and this is the trigger 

trigger OpportunityTrigger on opportunity (after insert, after update) {

 // Written by shehla: To copy down the values from CP Project to Opportunity
 List< ConstructionPts__CP_Project__c > listMember2ObjToInsert = new List< ConstructionPts__CP_Project__c >();
 for (opportunity Oppty : Trigger.new)
     {
     ConstructionPts__CP_Project__c member2Obj = new ConstructionPts__CP_Project__c();
    member2Obj.ConstructionPts__Address__c = Oppty.AddressContruction__c;
    member2Obj.ConstructionPts__City__c = Oppty.City__c;
    member2Obj.ConstructionPts__State__c = Oppty.State__c;
    member2Obj.ConstructionPts__Zipcode__c = Oppty.Zip_Code__c;
    member2Obj.ConstructionPts__County__c = Oppty.County__c;
    member2Obj.ConstructionPts__Country__c = Oppty.Country__c;
    listMember2ObjToInsert.add(member2Obj);
    }
    insert listMember2ObjToInsert;
}
Always ThinkinAlways Thinkin
I can see that this trigger is going to create a new ConstructionPts__CP_Project__c record based on the values on the new (or updated) Opportunity and will do this for every new Opportunity and every time the Opportunity is updated (which seems odd - do you really want to insert the same ConstructionPts__CP_Project__c over and over?).

But what you keep saying is that you want to copy the values FROM ConstructionPts__CP_Project__c and TO the Opportunity. Yet this trigger is doing the opposite. Can you clarify the goal? I thought I understood your goal, but the Trigger seems to do the opposite of what you said!
 
SAHG-SFDCSAHG-SFDC
Thanks for looking into it, What I type is correct, My trigger might be wrong (Very first one I wrote) If you could tell me what sould I do to corrrect it?

 
SAHG-SFDCSAHG-SFDC
I was thinking that I might have to link the objects as well , I am not sure how
Always ThinkinAlways Thinkin
Hi,
Just to rule out the obvious here, could you use Formula Fields on Opportunity? For example, for the City__c field on Opportunity, you could have a text formula with ConstructionPts__CP_Project__c.ConstructionPts__City__c as the value. You would still need to populate a lookup on Opportunities to the ConstructionPts__CP_Project__c records.

It's getting late here in NYC but I'll put up some sample code tomorrow morning if the formula idea is no good.
Always ThinkinAlways Thinkin
Before I get started on the code (or perhaps a flow), there are important questions to clear up: when creating a new Opportunity, where would the preferred ConstructionPts__CP_Project__c record come from? Where would the user start? Would it be on an existing Opportunity that you want to clone? Would it be on the Account associated with the Opportunity? Both of those would likely allow you to reference an existing associated ConstructionPts__CP_Project__c record. If you are starting on a brand new Opportunity, how would the user know which ConstructionPts__CP_Project__c to pick?
Always ThinkinAlways Thinkin
BTW, I think a Flow is going to be the best method of delivering this solution because you said that the user should have the option of accepting the existing values from a ConstructionPts__CP_Project__c record. Flows are designed to allow that decision to be made while pulling in data from other records and writing it to the new Opportunity record.
SAHG-SFDCSAHG-SFDC
HI, Thanks a lot for looking into it

The opportunities are created directly by the user as new opportuity and the system should look for the address in the "ConstructionPts__CP_Project__c " with those 6 address fields if there is a match then let user decide if new opportunity should be created or skip creation