+ Start a Discussion
LALALALALALA 

populate lookup field(account name) on opportunity upon custom object(Orders) created

trigger CreateOppFromOrder on Orders__c (after insert) {
Set<String> orderNames = new Set<String>();
    
for (Orders__c order: Trigger.new) {
    orderNames.add(order.Name); 
}
    
Map<String, OrderFromWeb__c> orderFromWebMap = new Map<String, OrderFromWeb__c>();
    
for (OrderFromWeb__c ofw: [
    select OrderNumber__c,
    PO_Number__c,
    PaymentStatus__c,
        PaymentMethod__c
    from OrderFromWeb__c
    where OrderNumber__c in :orderNames]) {
    orderFromWebMap.put(ofw.OrderNumber__c, ofw);
}

 List<Opportunity> Opps = new List<Opportunity>();

for (Orders__c order : Trigger.new) {
    OrderFromWeb__c ofw = orderFromWebMap.get(order.Name);
    Opportunity op = new Opportunity(AccountId = order.Contacts__r.Account.Name);
    if (
        ofw != null &&
        ofw.PaymentMethod__c == 'PayPal Standard') {
        op.Name = date.today() + '_W:' + Order.Name;
    } else {
        op.Name = date.today() + '_:PO' + Order.Name;
    }
    
    //PO Number
        if(ofw.PaymentMethod__c == 'PayPal Standard' && ofw.OrderNumber__c == order.Name)
        {
            op.PO_Number__c = Order.Name;
        }
    else
    {
        op.PO_Number__c = ofw.PO_Number__c;
    }
    
    //Account Name
        op.AccountId = ???????    what i write is : op.AccountId = order.Contacts__r.Account.Name   but isn't work.......

        //Stage
        op.StageName = 'Closed Won';
        
        //Close Date
        op.CloseDate = date.today();
        
    opps.add(op);
    
}
    
insert opps;
}

(see bold font in code, Please)
Because Account Name field on Opportunity is lookup field, So how can I  populate lookup data to opportunity??   thanks :)
LALALALALALA
this look up field which name is Account Name, there has a relationship between account and contact,,,, and in opportunity have a field name is Account Name but no API, which means I cannot get Like op.AccountName,,,only have AccountId,  So how can I populate Account Name from Account to fill this field on opportunity
Mudasir WaniMudasir Wani
Hello,

f you already have contact associated to the order and contact is associated to an account.
Now you want to create an opportunity and want to associate with the same account as contact.
Then you should write it as
//But remember Trigger.new can't give you relationship fields so you need to fetch the contact related to the order
Contact con = [Select id,name,AccountId from Contact where Id =: order.ContractId;
//Then you can use the below code
​op.AccountId = con.AccountId;

Don't forget to select best answer to make our efforts visible in the developer forum.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help.
LALALALALALA
@Mudasir Wani

still have problems : it shows Entity is org-accessible
LALALALALALA
for now, i know how to populate account name on opportunity, but still have a small problme which is it shows Entity is org-accessible
Account acct = [select name from account where id in: order.id];
insert acct;
ID acctID = acct.ID;
Mudasir WaniMudasir Wani
Use the below code instead
 
Account acct = [select name from account where id =: order.AccountId];

 
LALALALALALA
@Mudaslr Wani
still same problems: entity is not org-accessible

by the way, order don't have accountId......only opportunity have....

oh my god,,why i can't fix it....
Mudasir WaniMudasir Wani
We have fetched  accountId in Contact so you can use that here 
Account acct = [select name from account where id =: con.AccountId];

 
LALALALALALA
@Mudasir Wani
System.QueryException: List has more than 1 row for assignment to SObject: Trigger.CreateOppFromOrder: line 15, column 1
LALALALALALA

@Mudasir Wani

Contact con = [Select id,name,AccountId from Contact];
    Account acct = [select name from account where id =: con.AccountId];
    insert acct;
    ID acctID = acct.Id;

is that contact that what i typed are correct? i feel still alittle bit wrong..... sorry, you are so nice:)  please help me 
Mudasir WaniMudasir Wani

Below query  should give error as yo are fetching a list of contacts and trying to put them in a single contact 
Contact con = [Select id,name,AccountId from Contact];


You can use the limit 1 and the query should be like
 
Contact con = [Select id,name,AccountId from Contact where Id =: order.ContractId limit 1];


Don't forget to select best answer to make our efforts visible in the developer forum.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help.
LALALALALALA
@ Mudasir Wani

sorry, still doesn't work that because in 'order' doesn't has ContactId,,so id can't =: order.ContactId 

is that another way to fix this problems ?

sorry, I am too new,,,, I don't know how to do it...Thanks
Mudasir WaniMudasir Wani
Hello,

Just let me know the relationship between your 

Order__c , Opportunity and Account 
And I will help you with the code