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
Gattam Rakesh (SFDC)Gattam Rakesh (SFDC) 

How to Create a same task on other Object

Hai all 
Can any one Explain me how to create a same task on the Other Custom Object automatically??
Here is my Requirement 
I have one custom Object Retailer__c and custom Button on Retailer Object Take Order once we click on it A task should be created on retailer and same task should be created on Account for me now task is creating on both account and retailer but fileds are not mapping can any 1 help me out here is my code:
  Public PageReference Save()
        {
         odd.Distributor__c=acc.Id;
         odd.Retailer__c=ret.id;
         Insert odd;
        task1.Subject='Order/Visit' ;
        task1.Status__c='Order Taken';
        task1.Whatid=ret.id;
        task1.Date__c=odd.Order_Date__c;
        task1.Comments__c=ret.Comments__c;
        task1.Country__c=ret.Billing_Country__c;
        task1.State__c= ret.Billing_State__c;
        task1.City__c=ret.Billing_City__c;
        task1.Street__c=ret.Billing_Street__c;
        task1.Zip_Postal_Code__c=ret.Billing_Zip_Postal_Code__c;
        task1.Province__c=ret.Billing_Province__c;
        Insert task1;
        task2.Whatid=acc.id;
        
        Insert task2;
 
pconpcon
The problem is that you are not actually setting the fields on task2.
 
public PageReference Save() {
        odd.Distributor__c=acc.Id;
        odd.Retailer__c=ret.id;
        insert odd;

        List<Task> tasks = new List<Task>();

        tasks.add(new Task(
            Subject = 'Order/Visit',
            Status__c = 'Order Taken',
            Whatid = ret.id,
            Date__c = odd.Order_Date__c,
            Comments__c = ret.Comments__c,
            Country__c = ret.Billing_Country__c,
            State__c =  ret.Billing_State__c,
            City__c = ret.Billing_City__c,
            Street__c = ret.Billing_Street__c,
            Zip_Postal_Code__c = ret.Billing_Zip_Postal_Code__c,
            Province__c = ret.Billing_Province__c
        ));

        tasks.add(new Task(
            Subject = 'Order/Visit',
            Status__c = 'Order Taken',
            Whatid = acc.Id,
            Date__c = odd.Order_Date__c,
            Comments__c = ret.Comments__c,
            Country__c = ret.Billing_Country__c,
            State__c =  ret.Billing_State__c,
            City__c = ret.Billing_City__c,
            Street__c = ret.Billing_Street__c,
            Zip_Postal_Code__c = ret.Billing_Zip_Postal_Code__c,
            Province__c = ret.Billing_Province__c
        ));

        insert tasks;
}

This code is not ideal but it will do what you want.  What you should really have is a utility method that takes in a subject, status, Id, an odd and a ret (whatever those are) and generates a task.  That would turn your code into this.
 
public class TaskUtils {
    public static Task createTask(String subject, String status, Id id, Odd__c odd, Ret__c ret) {
        return new Task(
            Subject = subject
            Status__c = status,
            Whatid = id,
            Date__c = odd.Order_Date__c,
            Comments__c = ret.Comments__c,
            Country__c = ret.Billing_Country__c,
            State__c =  ret.Billing_State__c,
            City__c = ret.Billing_City__c,
            Street__c = ret.Billing_Street__c,
            Zip_Postal_Code__c = ret.Billing_Zip_Postal_Code__c,
            Province__c = ret.Billing_Province__c
        );
    }
}
 
public PageReference Save() {
        odd.Distributor__c=acc.Id;
        odd.Retailer__c=ret.id;
        insert odd;

        List<Task> tasks = new List<Task>{
            TaskUtils.createTask('Order/Visit', 'Order Taken', ret.Id, odd, ret),
            TaskUtils.createTask('Order/Visit', 'Order Taken', acc.Id, odd, ret)
        };

        insert tasks;
}

You'll need to update Odd__c and Ret__c to be whatever they really are.

NOTE: This code has not been tested and may contain typographical or logical errors.